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
(: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]

View File

@ -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]

View File

@ -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 %))

View File

@ -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]

View File

@ -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))))

View File

@ -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))))

View File

@ -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]

View File

@ -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]

View File

@ -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?

View File

@ -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)}}))
false)}}))

View File

@ -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?