Change view-id as per the selected bottom tab and refactor subscriptions (#15636)

This commit is contained in:
Parvesh Monu 2023-04-19 19:09:37 +05:30 committed by GitHub
parent 3ca120ca94
commit 7f00f96b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 52 additions and 117 deletions

View File

@ -1,11 +1,6 @@
(ns status-im.search.core (ns status-im.search.core
(:require [utils.re-frame :as rf])) (: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 (rf/defn currency-filter-changed
{:events [:search/currency-filter-changed]} {:events [:search/currency-filter-changed]}
[cofx search-filter] [cofx search-filter]

View File

@ -14,8 +14,7 @@
[chat-id] [chat-id]
(fn [] (fn []
(rf/dispatch [:dismiss-keyboard]) (rf/dispatch [:dismiss-keyboard])
(rf/dispatch [:chat/navigate-to-chat chat-id]) (rf/dispatch [:chat/navigate-to-chat chat-id])))
(rf/dispatch [:search/home-filter-changed nil])))
(defn truncate-literal (defn truncate-literal
[literal] [literal]

View File

@ -37,10 +37,9 @@
(defn chats (defn chats
[selected-tab top] [selected-tab top]
(let [{:keys [items search-filter]} (rf/sub [:home-items]) (let [unfiltered-items (rf/sub [:chats-stack-items])
items (filter-items-by-tab selected-tab items)] items (filter-items-by-tab selected-tab unfiltered-items)]
(if (and (empty? items) (if (empty? items)
(empty? search-filter))
[welcome-blank-chats] [welcome-blank-chats]
[rn/flat-list [rn/flat-list
{:key-fn #(or (:chat-id %) (:public-key %) (:id %)) {:key-fn #(or (:chat-id %) (:public-key %) (:id %))

View File

@ -186,8 +186,7 @@
(when (and (not locked?) id) (when (and (not locked?) id)
{:on-press (fn [] {:on-press (fn []
(rf/dispatch [:dismiss-keyboard]) (rf/dispatch [:dismiss-keyboard])
(rf/dispatch [:chat/navigate-to-chat (str community-id id)]) (rf/dispatch [:chat/navigate-to-chat (str community-id id)]))}))))
(rf/dispatch [:search/home-filter-changed nil]))}))))
(defn add-on-press-handler-to-chats (defn add-on-press-handler-to-chats
[community-id chats] [community-id chats]

View File

@ -51,6 +51,7 @@
(calculate-home-stack-state-value stack-id))] (calculate-home-stack-state-value stack-id))]
(reset! selected-stack-id stack-id) (reset! selected-stack-id stack-id)
(reset! home-stack-state home-stack-state-value) (reset! home-stack-state home-stack-state-value)
(rf/dispatch [:set-view-id (or stack-id :shell)])
(when store? (when store?
(async-storage/set-item! :selected-stack-id stack-id)))) (async-storage/set-item! :selected-stack-id stack-id))))

View File

@ -4,6 +4,7 @@
[react-native.gesture :as gesture] [react-native.gesture :as gesture]
[react-native.navigation :as navigation] [react-native.navigation :as navigation]
[status-im.multiaccounts.login.core :as login-core] [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.roots :as roots]
[status-im2.navigation.state :as state] [status-im2.navigation.state :as state]
[status-im2.navigation.view :as views] [status-im2.navigation.view :as views]
@ -35,8 +36,11 @@
(defn set-view-id (defn set-view-id
[view-id] [view-id]
(when-let [{:keys [on-focus]} (get views/screens 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 [: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 (when on-focus
(re-frame/dispatch on-focus)))) (re-frame/dispatch on-focus))))

View File

@ -72,14 +72,9 @@
(re-frame/reg-sub (re-frame/reg-sub
:communities/featured-communities :communities/featured-communities
:<- [:search/home-filter]
:<- [:communities] :<- [:communities]
(fn [[search-filter communities]] (fn [communities]
(filterv (vals communities)))
(fn [{:keys [name]}]
(or (empty? search-filter)
(string/includes? (string/lower-case (str name)) search-filter)))
(vals communities))))
(re-frame/reg-sub (re-frame/reg-sub
:communities/sorted-communities :communities/sorted-communities
@ -87,42 +82,38 @@
(fn [communities] (fn [communities]
(sort-by :name (vals 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 (re-frame/reg-sub
:communities/community-ids :communities/community-ids
:<- [:communities/communities] :<- [:communities]
(fn [communities] (fn [communities]
(map :id communities))) (map :id (vals communities))))
(def memo-communities-stack-items (atom nil))
(re-frame/reg-sub (re-frame/reg-sub
:communities/grouped-by-status :communities/grouped-by-status
:<- [:communities/communities] :<- [:view-id]
:<- [:communities]
:<- [:communities/my-pending-requests-to-join] :<- [:communities/my-pending-requests-to-join]
;; Return communities splitted by level of user participation. Some communities user ;; 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 ;; already joined, to some of them join request sent and others were opened one day
;; and their data remained in app-db. ;; and their data remained in app-db.
;; Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}" ;; Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}"
(fn [[communities requests]] (fn [[view-id communities requests]]
(reduce (fn [acc community] (if (= view-id :communities-stack)
(let [joined? (:joined community) (let [grouped-communities (reduce (fn [acc community]
community-id (:id community) (let [joined? (:joined community)
pending? (boolean (get requests community-id))] community-id (:id community)
(cond pending? (boolean (get requests community-id))]
joined? (update acc :joined conj community) (cond
pending? (update acc :pending conj community) joined? (update acc :joined conj community)
:else (update acc :opened conj community)))) pending? (update acc :pending conj community)
{:joined [] :pending [] :opened []} :else (update acc :opened conj community))))
communities))) {:joined [] :pending [] :opened []}
(vals communities))]
(reset! memo-communities-stack-items grouped-communities)
grouped-communities)
@memo-communities-stack-items)))
(defn community->home-item (defn community->home-item
[community counts] [community counts]

View File

@ -145,12 +145,6 @@
(fn [animations [_ type item-id]] (fn [animations [_ type item-id]]
(get-in animations [type item-id :delete-swiped]))) (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 (re-frame/reg-sub
:search/recipient-filter :search/recipient-filter
:<- [:ui/search] :<- [:ui/search]

View File

@ -1,38 +1,20 @@
(ns status-im2.subs.home (ns status-im2.subs.home
(:require [re-frame.core :as re-frame])) (:require [re-frame.core :as re-frame]))
(def memo-home-items (atom nil)) (def memo-chats-stack-items (atom nil))
(re-frame/reg-sub (re-frame/reg-sub
:home-items :chats-stack-items
:<- [:search/home-filter] :<- [:chats/home-list-chats]
:<- [:search/filtered-chats]
:<- [:communities/communities]
:<- [:view-id] :<- [:view-id]
:<- [:home-items-show-number] :<- [:home-items-show-number]
(fn [[search-filter filtered-chats communities view-id home-items-show-number]] (fn [[chats view-id home-items-show-number]]
(if (= view-id :shell-stack) (if (= view-id :chats-stack)
(let [communities-count (count communities) (let [res (take home-items-show-number chats)]
chats-count (count filtered-chats) (reset! memo-chats-stack-items res)
;; 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)
res) res)
;;we want to keep data unchanged so react doesn't change component when we leave screen ;;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 (re-frame/reg-sub
:hide-home-tooltip? :hide-home-tooltip?

View File

@ -1,7 +1,6 @@
(ns status-im2.subs.search (ns status-im2.subs.search
(:require [clojure.string :as string]) (:require [clojure.string :as string])
(:require [clojure.string :as string] (:require [clojure.string :as string]
[status-im.utils.gfycat.core :as gfycat]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.utils.currency :as currency])) [status-im.utils.currency :as currency]))
@ -36,41 +35,6 @@
(sort-by-timestamp results) (sort-by-timestamp results)
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 (defn extract-currency-attributes
[currency] [currency]
(let [{:keys [code display-name]} (val currency)] (let [{:keys [code display-name]} (val currency)]
@ -101,4 +65,4 @@
nil (apply-filter search-token-filter nil (apply-filter search-token-filter
default-tokens default-tokens
extract-token-attributes extract-token-attributes
false)}})) false)}}))

View File

@ -103,11 +103,18 @@
#(re-frame/dispatch [:chat/navigate-to-chat channel-id]) #(re-frame/dispatch [:chat/navigate-to-chat channel-id])
100))})) 100))}))
(def memo-shell-cards (atom nil))
(re-frame/reg-sub (re-frame/reg-sub
:shell/sorted-switcher-cards :shell/sorted-switcher-cards
:<- [:shell/switcher-cards] :<- [:shell/switcher-cards]
(fn [stacks] :<- [:view-id]
(sort-by :clock > (map val stacks)))) (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 (re-frame/reg-sub
:shell/shell-pass-through? :shell/shell-pass-through?