From f6e480344d1eb4c63bdf6c819b287be9ff372282 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 12 Apr 2021 11:25:52 +0200 Subject: [PATCH] do not render not active screen Signed-off-by: andrey --- src/status_im/subs.cljs | 89 +++++++++++++++---- src/status_im/ui/screens/home/views.cljs | 64 ++++++------- .../ui/screens/profile/contact/views.cljs | 2 +- 3 files changed, 104 insertions(+), 51 deletions(-) diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index 8d8556ceb5..b480d2f8ad 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -940,7 +940,7 @@ (empty? messages))) (re-frame/reg-sub - :chats/chat-messages-stream + :chats/raw-chat-messages-stream (fn [[_ chat-id] _] [(re-frame/subscribe [:chats/message-list chat-id]) (re-frame/subscribe [:chats/chat-messages chat-id]) @@ -955,13 +955,49 @@ (hydrate-messages messages) (chat.db/add-gaps messages-gaps range all-loaded? public?)))) +;;we want to keep data unchanged so react doesn't change component when we leave screen +(def memo-chat-messages-stream (atom nil)) + +(re-frame/reg-sub + :chats/chat-messages-stream + (fn [[_ chat-id] _] + [(re-frame/subscribe [:chats/raw-chat-messages-stream chat-id]) + (re-frame/subscribe [:view-id])]) + (fn [[messages view-id]] + (if (= view-id :chat) + (do + (reset! memo-chat-messages-stream messages) + messages) + @memo-chat-messages-stream))) + +(def memo-profile-messages-stream (atom nil)) + +(re-frame/reg-sub + :chats/profile-messages-stream + (fn [[_ chat-id] _] + [(re-frame/subscribe [:chats/raw-chat-messages-stream chat-id]) + (re-frame/subscribe [:view-id])]) + (fn [[messages view-id]] + (if (= view-id :profile) + (do + (reset! memo-profile-messages-stream messages) + messages) + @memo-profile-messages-stream))) + +(def memo-timeline-messages-stream (atom nil)) + (re-frame/reg-sub :chats/timeline-messages-stream :<- [:chats/message-list constants/timeline-chat-id] :<- [:chats/chat-messages constants/timeline-chat-id] - (fn [[message-list messages]] - (-> (models.message-list/->seq message-list) - (hydrate-messages messages)))) + :<- [:view-id] + (fn [[message-list messages view-id]] + (if (= view-id :status) + (let [res (-> (models.message-list/->seq message-list) + (hydrate-messages messages))] + (reset! memo-timeline-messages-stream res) + res) + @memo-timeline-messages-stream))) (re-frame/reg-sub :chats/current-profile-chat @@ -1281,29 +1317,44 @@ (fn [[{:keys [:stickers/recent-stickers]} packs]] (map (fn [hash] {:hash hash :pack (find-pack-id-for-hash hash packs)}) recent-stickers))) +;;HOME ============================================================================================================== + +(def memo-home-items (atom nil)) + (re-frame/reg-sub :home-items :<- [:search/home-filter] :<- [:search/filtered-chats] :<- [:communities/communities] - (fn [[search-filter filtered-chats communities]] - (let [communities-count (count communities) - chats-count (count filtered-chats) - ;; If we have both communities & chats we want to display - ;; a separator between them + :<- [:view-id] + (fn [[search-filter filtered-chats communities view-id]] + (if (= view-id :home) + (let [communities-count (count communities) + chats-count (count filtered-chats) + ;; If we have both communities & chats we want to display + ;; a separator between them - communities-with-separator (if (and (pos? communities-count) - (pos? chats-count)) - (update communities - (dec communities-count) - assoc :last? true) - communities)] - {:search-filter search-filter - :items (concat communities-with-separator filtered-chats)}))) + communities-with-separator (if (and (pos? communities-count) + (pos? chats-count)) + (update communities + (dec communities-count) + assoc :last? true) + communities) + res {:search-filter search-filter + :items (concat communities-with-separator filtered-chats)}] + (reset! memo-home-items res) + res) + ;;we want to keep data unchanged so react doesn't change component when we leave screen + @memo-home-items))) + +(re-frame/reg-sub + :hide-home-tooltip? + :<- [:multiaccount] + (fn [multiaccount] + (:hide-home-tooltip? multiaccount))) ;;PAIRING ============================================================================================================== - (re-frame/reg-sub :pairing/installations :<- [:get-pairing-installations] @@ -1316,7 +1367,7 @@ (re-frame/reg-sub :pairing/installation-id :<- [:multiaccount] - :installation-id) + (fn [multiaccount] (:installation-id multiaccount))) (re-frame/reg-sub :pairing/installation-name diff --git a/src/status_im/ui/screens/home/views.cljs b/src/status_im/ui/screens/home/views.cljs index 1be75f41bb..cf147c2183 100644 --- a/src/status_im/ui/screens/home/views.cljs +++ b/src/status_im/ui/screens/home/views.cljs @@ -151,39 +151,41 @@ [inner-item/home-list-item home-item] [communities.views/community-home-list-item home-item])) -(defn communities-and-chats [items loading? search-filter hide-home-tooltip?] - [:<> - [connectivity/loading-indicator] - (if loading? - [react/view {:flex 1 :align-items :center :justify-content :center} - [react/activity-indicator {:animating true}]] - (if (and (empty? items) - (empty? search-filter) - hide-home-tooltip? - (not @search-active?)) - [welcome-blank-page] - [list/flat-list - {:key-fn #(or (:chat-id %) (:id %)) - :keyboard-should-persist-taps :always - :data items - :render-fn render-fn - :header [:<> - (when (or (seq items) @search-active? (seq search-filter)) - [search-input-wrapper search-filter items]) - [referral-item/list-item] - (when (and (empty? items) - (or @search-active? (seq search-filter))) - [start-suggestion search-filter])] - :footer (if (and (not hide-home-tooltip?) (not @search-active?)) - [home-tooltip-view] - [react/view {:height 68}])}]))]) +(defn chat-list-key-fn [item] + (or (:chat-id item) (:id item))) + +(views/defview communities-and-chats [] + (views/letsubs [{:keys [items search-filter]} [:home-items] + hide-home-tooltip? [:hide-home-tooltip?]] + (if (and (empty? items) + (empty? search-filter) + hide-home-tooltip? + (not @search-active?)) + [welcome-blank-page] + [list/flat-list + {:key-fn chat-list-key-fn + :keyboard-should-persist-taps :always + :data items + :render-fn render-fn + :header [:<> + (when (or (seq items) @search-active? (seq search-filter)) + [search-input-wrapper search-filter items]) + [referral-item/list-item] + (when (and (empty? items) + (or @search-active? (seq search-filter))) + [start-suggestion search-filter])] + :footer (if (and (not hide-home-tooltip?) (not @search-active?)) + [home-tooltip-view] + [react/view {:height 68}])}]))) (views/defview chats-list [] - (views/letsubs [loading? [:chats/loading?] - {:keys [items - search-filter]} [:home-items] - {:keys [hide-home-tooltip?]} [:multiaccount]] - [communities-and-chats items loading? search-filter hide-home-tooltip?])) + (views/letsubs [loading? [:chats/loading?]] + [:<> + [connectivity/loading-indicator] + (if loading? + [react/view {:flex 1 :align-items :center :justify-content :center} + [react/activity-indicator {:animating true}]] + [communities-and-chats])])) (views/defview plus-button [] (views/letsubs [logging-in? [:multiaccounts/login]] diff --git a/src/status_im/ui/screens/profile/contact/views.cljs b/src/status_im/ui/screens/profile/contact/views.cljs index 7bd806bf60..ad978340db 100644 --- a/src/status_im/ui/screens/profile/contact/views.cljs +++ b/src/status_im/ui/screens/profile/contact/views.cljs @@ -158,7 +158,7 @@ (defn profile [] (let [{:keys [public-key name ens-verified] :as contact} @(re-frame/subscribe [:contacts/current-contact]) current-chat-id @(re-frame/subscribe [:chats/current-profile-chat]) - messages @(re-frame/subscribe [:chats/chat-messages-stream current-chat-id]) + messages @(re-frame/subscribe [:chats/profile-messages-stream current-chat-id]) no-messages? @(re-frame/subscribe [:chats/chat-no-messages? current-chat-id]) muted? (:muted @(re-frame/subscribe [:chats/chat public-key])) [first-name second-name] (multiaccounts/contact-two-names contact true)