Skip to content

Commit

Permalink
Change view-id as per the selected bottom tab and refactor subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Parveshdhull committed Apr 19, 2023
1 parent 3ca120c commit 7ba445f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 117 deletions.
5 changes: 0 additions & 5 deletions src/status_im/search/core.cljs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
3 changes: 1 addition & 2 deletions src/status_im2/contexts/chat/home/chat_list_item/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 3 additions & 4 deletions src/status_im2/contexts/chat/home/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %))
Expand Down
3 changes: 1 addition & 2 deletions src/status_im2/contexts/communities/overview/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/status_im2/contexts/shell/animation.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))))

Expand Down
6 changes: 5 additions & 1 deletion src/status_im2/navigation/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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))))

Expand Down
55 changes: 23 additions & 32 deletions src/status_im2/subs/communities.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -72,57 +72,48 @@

(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
:<- [:communities]
(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]
Expand Down
6 changes: 0 additions & 6 deletions src/status_im2/subs/general.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 8 additions & 26 deletions src/status_im2/subs/home.cljs
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
38 changes: 1 addition & 37 deletions src/status_im2/subs/search.cljs
Original file line number Diff line number Diff line change
@@ -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]))

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -101,4 +65,4 @@
nil (apply-filter search-token-filter
default-tokens
extract-token-attributes
false)}}))
false)}}))
11 changes: 9 additions & 2 deletions src/status_im2/subs/shell.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 7ba445f

Please sign in to comment.