Don't rely on contacst for one-to-one mentions, and remove cursor

[Fixes: #11670]
[Fixes: #11671]

This commit fixes 2 issues.

One issue was due to removing tribute to talk, which added in the
`contacts` map any user you open a chat with, and some code relied on
this behavior.

The second issue was likely due to a react native upgrade which might
have fixed a bug but caused a different issue, which resulted in the
text disappearing when cursor was set.

I have removed that (it was only used on android) and everything seems
to be working correctly, but not so sure about this one.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2021-02-01 17:26:43 +01:00
parent d946d473c6
commit 0867b972c3
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
4 changed files with 32 additions and 51 deletions

View File

@ -195,6 +195,10 @@
:name name}))
{}
group-contacts))
(= chat-type constants/one-to-one-chat-type)
(assoc users
current-chat-id
(contact.db/public-key->contact contacts current-chat-id))
:else users)
{:keys [name preferred-name public-key]}

View File

@ -18,7 +18,7 @@
(defn public-key->contact
[contacts public-key]
(when public-key
(get contacts public-key
(or (get contacts public-key)
(public-key->new-contact public-key))))
(defn- contact-by-address [[addr contact] address]

View File

@ -121,7 +121,6 @@
(reg-root-key-sub :camera-roll-photos :camera-roll-photos)
(reg-root-key-sub :group-chat/invitations :group-chat/invitations)
(reg-root-key-sub :chats/mention-suggestions :chats/mention-suggestions)
(reg-root-key-sub :chats/cursor :chats/cursor)
(reg-root-key-sub :chat/inputs-with-mentions :chat/inputs-with-mentions)
(reg-root-key-sub :inactive-chat-id :inactive-chat-id)
;;browser
@ -1065,15 +1064,20 @@
:<- [:chats/mentionable-contacts]
:<- [:contacts/blocked-set]
:<- [:multiaccount]
(fn [[{:keys [users]} contacts blocked {:keys [name preferred-name public-key]}]]
(fn [[{:keys [chat-id users chat-type]} contacts blocked {:keys [name preferred-name public-key]}]]
(let [contacts-with-one-to-one (if (= chat-type constants/one-to-one-chat-type)
(assoc contacts chat-id (get contacts chat-id (-> chat-id
contact.db/public-key->new-contact
contact.db/enrich-contact)))
contacts)]
(apply dissoc
(-> users
(merge contacts)
(merge contacts-with-one-to-one)
(assoc public-key (mentions/add-searchable-phrases
{:alias name
:name (or preferred-name name)
:public-key public-key})))
blocked)))
blocked))))
(re-frame/reg-sub
:chat/mention-suggestions
@ -1082,13 +1086,6 @@
(fn [[chat-id mentions]]
(take 15 (get mentions chat-id))))
(re-frame/reg-sub
:chat/cursor
:<- [:chats/current-chat-id]
:<- [:chats/cursor]
(fn [[chat-id cursor]]
(get cursor chat-id)))
(re-frame/reg-sub
:chat/input-with-mentions
:<- [:chats/current-chat-id]

View File

@ -79,15 +79,7 @@
:accessibility-label :send-message-button
:color (styles/send-icon-color)}]]])
(defn selection [cursor]
;; NOTE(rasom): In case if mention is added on pressing suggestion and
;; it is placed inside some text we have to specify `:selection` on
;; Android to ensure that cursor is added after the mention, not after
;; the last char in input. On iOS it works that way without this code
(when (and cursor platform/android?)
(clj->js {:start cursor :end cursor})))
(defn on-selection-change [cursor timeout-id last-text-change mentionable-users args]
(defn on-selection-change [timeout-id last-text-change mentionable-users args]
(let [selection (.-selection ^js (.-nativeEvent ^js args))
start (.-start selection)
end (.-end selection)]
@ -113,11 +105,7 @@
(re-frame/dispatch [::mentions/on-selection-change
{:start start
:end end}
mentionable-users]))
;; NOTE(rasom): we have to reset `cursor` value when user starts using
;; text-input because otherwise cursor will stay in the same position
(when (and cursor platform/android?)
(re-frame/dispatch [::mentions/clear-cursor]))))
mentionable-users]))))
(defn on-change [on-text-change last-text-change timeout-id mentionable-users args]
(let [text (.-text ^js (.-nativeEvent ^js args))]
@ -155,8 +143,7 @@
(defn text-input
[{:keys [cooldown-enabled? input-with-mentions on-text-change set-active-panel text-input-ref]}]
(let [cursor @(re-frame/subscribe [:chat/cursor])
mentionable-users @(re-frame/subscribe [:chats/mentionable-users])
(let [mentionable-users @(re-frame/subscribe [:chats/mentionable-users])
timeout-id (atom nil)
last-text-change (atom nil)]
[rn/view {:style (styles/text-input-wrapper)}
@ -178,25 +165,18 @@
(i18n/label :t/type-a-message))
:underline-color-android :transparent
:auto-capitalize :sentences
:selection (selection cursor)
:on-selection-change (partial on-selection-change
cursor timeout-id last-text-change mentionable-users)
:on-selection-change (partial on-selection-change timeout-id last-text-change mentionable-users)
:on-change (partial on-change
on-text-change last-text-change timeout-id mentionable-users)
:on-text-input (partial on-text-input mentionable-users)}
;; NOTE(rasom): reduce was used instead of for here because although
;; each text component was given a unique id it still would mess with
;; colors on Android. In case if entire component is built without lists
;; inside it works just fine on both platforms.
(reduce
(fn [acc [type text]]
(conj
acc
(for [[idx [type text]] (map-indexed
(fn [idx item]
[idx item])
input-with-mentions)]
^{:key (str idx "_" type "_" text)}
[rn/text (when (= type :mention)
{:style {:color "#0DA4C9"}})
text]))
[:<>]
input-with-mentions)]]))
text])]]))
(defn mention-item
[[public-key {:keys [alias name nickname] :as user}] _ _ text-input-ref]