From 7f00f96b0c0ffcf1b720281b0a2d3c2670816002 Mon Sep 17 00:00:00 2001 From: Parvesh Monu Date: Wed, 19 Apr 2023 19:09:37 +0530 Subject: [PATCH] Change view-id as per the selected bottom tab and refactor subscriptions (#15636) --- src/status_im/search/core.cljs | 5 -- .../chat/home/chat_list_item/view.cljs | 3 +- src/status_im2/contexts/chat/home/view.cljs | 7 +-- .../contexts/communities/overview/view.cljs | 3 +- src/status_im2/contexts/shell/animation.cljs | 1 + src/status_im2/navigation/core.cljs | 6 +- src/status_im2/subs/communities.cljs | 55 ++++++++----------- src/status_im2/subs/general.cljs | 6 -- src/status_im2/subs/home.cljs | 34 +++--------- src/status_im2/subs/search.cljs | 38 +------------ src/status_im2/subs/shell.cljs | 11 +++- 11 files changed, 52 insertions(+), 117 deletions(-) diff --git a/src/status_im/search/core.cljs b/src/status_im/search/core.cljs index 8e0e636e40..ede89d13a2 100644 --- a/src/status_im/search/core.cljs +++ b/src/status_im/search/core.cljs @@ -1,11 +1,6 @@ (ns status-im.search.core (:require [utils.re-frame :as rf])) -(rf/defn home-filter-changed - {:events [:search/home-filter-changed]} - [cofx search-filter] - {:db (assoc-in (:db cofx) [:ui/search :home-filter] search-filter)}) - (rf/defn currency-filter-changed {:events [:search/currency-filter-changed]} [cofx search-filter] diff --git a/src/status_im2/contexts/chat/home/chat_list_item/view.cljs b/src/status_im2/contexts/chat/home/chat_list_item/view.cljs index 4089f09401..4f80ad8c80 100644 --- a/src/status_im2/contexts/chat/home/chat_list_item/view.cljs +++ b/src/status_im2/contexts/chat/home/chat_list_item/view.cljs @@ -14,8 +14,7 @@ [chat-id] (fn [] (rf/dispatch [:dismiss-keyboard]) - (rf/dispatch [:chat/navigate-to-chat chat-id]) - (rf/dispatch [:search/home-filter-changed nil]))) + (rf/dispatch [:chat/navigate-to-chat chat-id]))) (defn truncate-literal [literal] diff --git a/src/status_im2/contexts/chat/home/view.cljs b/src/status_im2/contexts/chat/home/view.cljs index b7a12d9cbf..4c01f55a83 100644 --- a/src/status_im2/contexts/chat/home/view.cljs +++ b/src/status_im2/contexts/chat/home/view.cljs @@ -37,10 +37,9 @@ (defn chats [selected-tab top] - (let [{:keys [items search-filter]} (rf/sub [:home-items]) - items (filter-items-by-tab selected-tab items)] - (if (and (empty? items) - (empty? search-filter)) + (let [unfiltered-items (rf/sub [:chats-stack-items]) + items (filter-items-by-tab selected-tab unfiltered-items)] + (if (empty? items) [welcome-blank-chats] [rn/flat-list {:key-fn #(or (:chat-id %) (:public-key %) (:id %)) diff --git a/src/status_im2/contexts/communities/overview/view.cljs b/src/status_im2/contexts/communities/overview/view.cljs index b270d534e2..4c9e3ba1e7 100644 --- a/src/status_im2/contexts/communities/overview/view.cljs +++ b/src/status_im2/contexts/communities/overview/view.cljs @@ -186,8 +186,7 @@ (when (and (not locked?) id) {:on-press (fn [] (rf/dispatch [:dismiss-keyboard]) - (rf/dispatch [:chat/navigate-to-chat (str community-id id)]) - (rf/dispatch [:search/home-filter-changed nil]))})))) + (rf/dispatch [:chat/navigate-to-chat (str community-id id)]))})))) (defn add-on-press-handler-to-chats [community-id chats] diff --git a/src/status_im2/contexts/shell/animation.cljs b/src/status_im2/contexts/shell/animation.cljs index 7a5395e776..1d5980ccc4 100644 --- a/src/status_im2/contexts/shell/animation.cljs +++ b/src/status_im2/contexts/shell/animation.cljs @@ -51,6 +51,7 @@ (calculate-home-stack-state-value stack-id))] (reset! selected-stack-id stack-id) (reset! home-stack-state home-stack-state-value) + (rf/dispatch [:set-view-id (or stack-id :shell)]) (when store? (async-storage/set-item! :selected-stack-id stack-id)))) diff --git a/src/status_im2/navigation/core.cljs b/src/status_im2/navigation/core.cljs index ffeed69dbe..a13a28d386 100644 --- a/src/status_im2/navigation/core.cljs +++ b/src/status_im2/navigation/core.cljs @@ -4,6 +4,7 @@ [react-native.gesture :as gesture] [react-native.navigation :as navigation] [status-im.multiaccounts.login.core :as login-core] + [status-im2.contexts.shell.animation :as shell.animation] [status-im2.navigation.roots :as roots] [status-im2.navigation.state :as state] [status-im2.navigation.view :as views] @@ -35,8 +36,11 @@ (defn set-view-id [view-id] (when-let [{:keys [on-focus]} (get views/screens view-id)] - (re-frame/dispatch [:set-view-id view-id]) (re-frame/dispatch [:screens/on-will-focus view-id]) + (re-frame/dispatch [:set-view-id + (if (= view-id :shell-stack) + (or @shell.animation/selected-stack-id :shell) + view-id)]) (when on-focus (re-frame/dispatch on-focus)))) diff --git a/src/status_im2/subs/communities.cljs b/src/status_im2/subs/communities.cljs index d69ac02be5..b8e076f4e9 100644 --- a/src/status_im2/subs/communities.cljs +++ b/src/status_im2/subs/communities.cljs @@ -72,14 +72,9 @@ (re-frame/reg-sub :communities/featured-communities - :<- [:search/home-filter] :<- [:communities] - (fn [[search-filter communities]] - (filterv - (fn [{:keys [name]}] - (or (empty? search-filter) - (string/includes? (string/lower-case (str name)) search-filter))) - (vals communities)))) + (fn [communities] + (vals communities))) (re-frame/reg-sub :communities/sorted-communities @@ -87,42 +82,38 @@ (fn [communities] (sort-by :name (vals communities)))) -(re-frame/reg-sub - :communities/communities - :<- [:search/home-filter] - :<- [:communities] - (fn [[search-filter communities]] - (filterv - (fn [{:keys [name]}] - (or (empty? search-filter) - (string/includes? (string/lower-case (str name)) search-filter))) - (vals communities)))) - (re-frame/reg-sub :communities/community-ids - :<- [:communities/communities] + :<- [:communities] (fn [communities] - (map :id communities))) + (map :id (vals communities)))) + +(def memo-communities-stack-items (atom nil)) (re-frame/reg-sub :communities/grouped-by-status - :<- [:communities/communities] + :<- [:view-id] + :<- [:communities] :<- [:communities/my-pending-requests-to-join] ;; Return communities splitted by level of user participation. Some communities user ;; already joined, to some of them join request sent and others were opened one day ;; and their data remained in app-db. ;; Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}" - (fn [[communities requests]] - (reduce (fn [acc community] - (let [joined? (:joined community) - community-id (:id community) - pending? (boolean (get requests community-id))] - (cond - joined? (update acc :joined conj community) - pending? (update acc :pending conj community) - :else (update acc :opened conj community)))) - {:joined [] :pending [] :opened []} - communities))) + (fn [[view-id communities requests]] + (if (= view-id :communities-stack) + (let [grouped-communities (reduce (fn [acc community] + (let [joined? (:joined community) + community-id (:id community) + pending? (boolean (get requests community-id))] + (cond + joined? (update acc :joined conj community) + pending? (update acc :pending conj community) + :else (update acc :opened conj community)))) + {:joined [] :pending [] :opened []} + (vals communities))] + (reset! memo-communities-stack-items grouped-communities) + grouped-communities) + @memo-communities-stack-items))) (defn community->home-item [community counts] diff --git a/src/status_im2/subs/general.cljs b/src/status_im2/subs/general.cljs index 70ac94ca62..4e57c1f9f6 100644 --- a/src/status_im2/subs/general.cljs +++ b/src/status_im2/subs/general.cljs @@ -145,12 +145,6 @@ (fn [animations [_ type item-id]] (get-in animations [type item-id :delete-swiped]))) -(re-frame/reg-sub - :search/home-filter - :<- [:ui/search] - (fn [search] - (get search :home-filter))) - (re-frame/reg-sub :search/recipient-filter :<- [:ui/search] diff --git a/src/status_im2/subs/home.cljs b/src/status_im2/subs/home.cljs index 73e5963d85..2231497195 100644 --- a/src/status_im2/subs/home.cljs +++ b/src/status_im2/subs/home.cljs @@ -1,38 +1,20 @@ (ns status-im2.subs.home (:require [re-frame.core :as re-frame])) -(def memo-home-items (atom nil)) +(def memo-chats-stack-items (atom nil)) (re-frame/reg-sub - :home-items - :<- [:search/home-filter] - :<- [:search/filtered-chats] - :<- [:communities/communities] + :chats-stack-items + :<- [:chats/home-list-chats] :<- [:view-id] :<- [:home-items-show-number] - (fn [[search-filter filtered-chats communities view-id home-items-show-number]] - (if (= view-id :shell-stack) - (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) - res {:search-filter search-filter - :items (concat communities-with-separator - (take home-items-show-number - filtered-chats))}] - (reset! memo-home-items res) + (fn [[chats view-id home-items-show-number]] + (if (= view-id :chats-stack) + (let [res (take home-items-show-number chats)] + (reset! memo-chats-stack-items res) res) ;;we want to keep data unchanged so react doesn't change component when we leave screen - @memo-home-items))) + @memo-chats-stack-items))) (re-frame/reg-sub :hide-home-tooltip? diff --git a/src/status_im2/subs/search.cljs b/src/status_im2/subs/search.cljs index c7b1d1064d..a5decafea2 100644 --- a/src/status_im2/subs/search.cljs +++ b/src/status_im2/subs/search.cljs @@ -1,7 +1,6 @@ (ns status-im2.subs.search (:require [clojure.string :as string]) (:require [clojure.string :as string] - [status-im.utils.gfycat.core :as gfycat] [re-frame.core :as re-frame] [status-im.utils.currency :as currency])) @@ -36,41 +35,6 @@ (sort-by-timestamp results) results))) -(defn filter-chat - [contacts search-filter {:keys [group-chat alias name chat-id]}] - (let [alias (if-not group-chat - (string/lower-case (or alias - (get-in contacts [chat-id :alias]) - (gfycat/generate-gfy chat-id))) - "") - nickname (get-in contacts [chat-id :nickname])] - (or - (string/includes? (string/lower-case (str name)) search-filter) - (string/includes? (string/lower-case alias) search-filter) - (when nickname - (string/includes? (string/lower-case nickname) search-filter)) - (and - (get-in contacts [chat-id :ens-verified]) - (string/includes? (string/lower-case - (str (get-in contacts [chat-id :name]))) - search-filter))))) - -(re-frame/reg-sub - :search/filtered-chats - :<- [:chats/home-list-chats] - :<- [:contacts/contacts] - :<- [:search/home-filter] - (fn [[chats contacts search-filter]] - ;; Short-circuit if search-filter is empty - (let [filtered-chats (if (seq search-filter) - (filter - (partial filter-chat - contacts - (string/lower-case search-filter)) - chats) - chats)] - (sort-by :timestamp > filtered-chats)))) - (defn extract-currency-attributes [currency] (let [{:keys [code display-name]} (val currency)] @@ -101,4 +65,4 @@ nil (apply-filter search-token-filter default-tokens extract-token-attributes - false)}})) \ No newline at end of file + false)}})) diff --git a/src/status_im2/subs/shell.cljs b/src/status_im2/subs/shell.cljs index 56249f88a5..9e78c7011a 100644 --- a/src/status_im2/subs/shell.cljs +++ b/src/status_im2/subs/shell.cljs @@ -103,11 +103,18 @@ #(re-frame/dispatch [:chat/navigate-to-chat channel-id]) 100))})) +(def memo-shell-cards (atom nil)) + (re-frame/reg-sub :shell/sorted-switcher-cards :<- [:shell/switcher-cards] - (fn [stacks] - (sort-by :clock > (map val stacks)))) + :<- [:view-id] + (fn [[stacks view-id]] + (if (= view-id :shell) + (let [sorted-shell-cards (sort-by :clock > (map val stacks))] + (reset! memo-shell-cards sorted-shell-cards) + sorted-shell-cards) + @memo-shell-cards))) (re-frame/reg-sub :shell/shell-pass-through?