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})) :name name}))
{} {}
group-contacts)) 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) :else users)
{:keys [name preferred-name public-key]} {:keys [name preferred-name public-key]}

View File

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

View File

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

View File

@ -79,15 +79,7 @@
:accessibility-label :send-message-button :accessibility-label :send-message-button
:color (styles/send-icon-color)}]]]) :color (styles/send-icon-color)}]]])
(defn selection [cursor] (defn on-selection-change [timeout-id last-text-change mentionable-users args]
;; 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]
(let [selection (.-selection ^js (.-nativeEvent ^js args)) (let [selection (.-selection ^js (.-nativeEvent ^js args))
start (.-start selection) start (.-start selection)
end (.-end selection)] end (.-end selection)]
@ -113,11 +105,7 @@
(re-frame/dispatch [::mentions/on-selection-change (re-frame/dispatch [::mentions/on-selection-change
{:start start {:start start
:end end} :end end}
mentionable-users])) 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]))))
(defn on-change [on-text-change last-text-change timeout-id mentionable-users args] (defn on-change [on-text-change last-text-change timeout-id mentionable-users args]
(let [text (.-text ^js (.-nativeEvent ^js args))] (let [text (.-text ^js (.-nativeEvent ^js args))]
@ -155,8 +143,7 @@
(defn text-input (defn text-input
[{:keys [cooldown-enabled? input-with-mentions on-text-change set-active-panel text-input-ref]}] [{:keys [cooldown-enabled? input-with-mentions on-text-change set-active-panel text-input-ref]}]
(let [cursor @(re-frame/subscribe [:chat/cursor]) (let [mentionable-users @(re-frame/subscribe [:chats/mentionable-users])
mentionable-users @(re-frame/subscribe [:chats/mentionable-users])
timeout-id (atom nil) timeout-id (atom nil)
last-text-change (atom nil)] last-text-change (atom nil)]
[rn/view {:style (styles/text-input-wrapper)} [rn/view {:style (styles/text-input-wrapper)}
@ -178,25 +165,18 @@
(i18n/label :t/type-a-message)) (i18n/label :t/type-a-message))
:underline-color-android :transparent :underline-color-android :transparent
:auto-capitalize :sentences :auto-capitalize :sentences
:selection (selection cursor) :on-selection-change (partial on-selection-change timeout-id last-text-change mentionable-users)
:on-selection-change (partial on-selection-change
cursor timeout-id last-text-change mentionable-users)
:on-change (partial on-change :on-change (partial on-change
on-text-change last-text-change timeout-id mentionable-users) on-text-change last-text-change timeout-id mentionable-users)
:on-text-input (partial on-text-input mentionable-users)} :on-text-input (partial on-text-input mentionable-users)}
;; NOTE(rasom): reduce was used instead of for here because although (for [[idx [type text]] (map-indexed
;; each text component was given a unique id it still would mess with (fn [idx item]
;; colors on Android. In case if entire component is built without lists [idx item])
;; inside it works just fine on both platforms. input-with-mentions)]
(reduce ^{:key (str idx "_" type "_" text)}
(fn [acc [type text]] [rn/text (when (= type :mention)
(conj {:style {:color "#0DA4C9"}})
acc text])]]))
[rn/text (when (= type :mention)
{:style {:color "#0DA4C9"}})
text]))
[:<>]
input-with-mentions)]]))
(defn mention-item (defn mention-item
[[public-key {:keys [alias name nickname] :as user}] _ _ text-input-ref] [[public-key {:keys [alias name nickname] :as user}] _ _ text-input-ref]