diff --git a/src/status_im/contexts/wallet/collectible/utils.cljs b/src/status_im/contexts/wallet/collectible/utils.cljs index b48ccc6a89..c0a0e8c6a4 100644 --- a/src/status_im/contexts/wallet/collectible/utils.cljs +++ b/src/status_im/contexts/wallet/collectible/utils.cljs @@ -28,17 +28,6 @@ (log/debug "unsupported collectible file type:" (or collectible-type "Unknown type")) false))) -(defn total-owned-collectible - ([ownership] - (total-owned-collectible ownership false)) - ([ownership address] - (reduce (fn [acc item] - (if (or (not address) (= (:address item) address)) - (+ acc (js/parseInt (:balance item))) - acc)) - 0 - ownership))) - (defn collectible-owned-counter [total] (when (> total 1) (str "x" total))) diff --git a/src/status_im/contexts/wallet/collectible/view.cljs b/src/status_im/contexts/wallet/collectible/view.cljs index cb86c75c04..2f814fd6a1 100644 --- a/src/status_im/contexts/wallet/collectible/view.cljs +++ b/src/status_im/contexts/wallet/collectible/view.cljs @@ -160,6 +160,9 @@ collectible-owner (rf/sub [:wallet/collectible-details-owner collectible]) aspect-ratio (rf/sub [:wallet/collectible-aspect-ratio]) gradient-color (rf/sub [:wallet/collectible-gradient-color]) + total-owned (rf/sub [:wallet/total-owned-collectible + (:ownership collectible) + (:address collectible-owner)]) {:keys [id preview-url collection-data @@ -182,10 +185,7 @@ :image-height 300 :id token-id :header collectible-name - :description collection-name} - total-owned (utils/total-owned-collectible - (:ownership collectible) - (:address collectible-owner))] + :description collection-name}] [rn/view {:style style/container} [rn/view [gradient-layer preview-uri] diff --git a/src/status_im/contexts/wallet/common/collectibles_tab/view.cljs b/src/status_im/contexts/wallet/common/collectibles_tab/view.cljs index 81c793232e..0f5cecd68a 100644 --- a/src/status_im/contexts/wallet/common/collectibles_tab/view.cljs +++ b/src/status_im/contexts/wallet/common/collectibles_tab/view.cljs @@ -7,7 +7,8 @@ [status-im.contexts.wallet.collectible.utils :as utils] [status-im.contexts.wallet.common.collectibles-tab.style :as style] [status-im.contexts.wallet.common.empty-tab.view :as empty-tab] - [utils.i18n :as i18n])) + [utils.i18n :as i18n] + [utils.re-frame :as rf])) (defn- collectible-item [{:keys [preview-url collection-data collectible-data total-owned on-press on-long-press] @@ -61,15 +62,16 @@ ;; TODO: https://github.com/status-im/status-mobile/issues/20137 ;; 1. If possible, move `collectibles-data` calculation to a subscription ;; 2. Optimization: do not recalculate all the collectibles, process only the new ones - (let [collectibles-data (map-indexed (fn [index {:keys [ownership] :as collectible}] - (assoc collectible - :total-owned (utils/total-owned-collectible - ownership - current-account-address) - :on-long-press on-collectible-long-press - :on-press on-collectible-press - :collectible-index index)) - collectibles)] + (let [collectibles-data (map-indexed + (fn [index {:keys [ownership] :as collectible}] + (let [total-owned (rf/sub [:wallet/total-owned-collectible ownership + current-account-address])] + (assoc collectible + :total-owned total-owned + :on-long-press on-collectible-long-press + :on-press on-collectible-press + :collectible-index index))) + collectibles)] [rn/flat-list {:data collectibles-data :style {:flex 1} diff --git a/src/status_im/contexts/wallet/home/tabs/view.cljs b/src/status_im/contexts/wallet/home/tabs/view.cljs index 4011ab6225..6475ce10f1 100644 --- a/src/status_im/contexts/wallet/home/tabs/view.cljs +++ b/src/status_im/contexts/wallet/home/tabs/view.cljs @@ -21,7 +21,7 @@ (defn view [{:keys [selected-tab]}] - (let [collectible-list (rf/sub [:wallet/all-collectibles-list-in-selected-networks]) + (let [collectible-list (rf/sub [:wallet/owned-collectibles-list-in-selected-networks]) request-collectibles #(rf/dispatch [:wallet/request-collectibles-for-all-accounts {}])] [rn/view {:style style/container} diff --git a/src/status_im/subs/wallet/collectibles.cljs b/src/status_im/subs/wallet/collectibles.cljs index d1f6fb7b35..48c310610d 100644 --- a/src/status_im/subs/wallet/collectibles.cljs +++ b/src/status_im/subs/wallet/collectibles.cljs @@ -55,13 +55,13 @@ (filter-collectibles-in-chains collectibles chain-ids))) (re-frame/reg-sub - :wallet/all-collectibles-list - :<- [:wallet] - (fn [{:keys [accounts]}] + :wallet/owned-collectibles-list + :<- [:wallet/accounts-without-watched-accounts] + (fn [accounts] (let [max-collectibles (->> accounts - (map (comp count :collectibles val)) + (map (comp count :collectibles)) (apply max)) - all-collectibles (map (fn [[_address {:keys [collectibles]}]] + all-collectibles (map (fn [{:keys [collectibles]}] (let [amount-to-add (- max-collectibles (count collectibles)) empty-collectibles (repeat amount-to-add nil)] (reduce conj collectibles empty-collectibles))) @@ -72,8 +72,8 @@ (add-collectibles-preview-url))))) (re-frame/reg-sub - :wallet/all-collectibles-list-in-selected-networks - :<- [:wallet/all-collectibles-list] + :wallet/owned-collectibles-list-in-selected-networks + :<- [:wallet/owned-collectibles-list] :<- [:wallet/selected-networks->chain-ids] (fn [[all-collectibles chain-ids]] (filter-collectibles-in-chains all-collectibles chain-ids))) @@ -123,3 +123,17 @@ %) accounts)))) +(re-frame/reg-sub + :wallet/total-owned-collectible + :<- [:wallet/accounts-without-watched-accounts] + (fn [accounts [_ ownership address]] + (let [addresses (map :address accounts)] + (reduce (fn [acc item] + (if (or + (and (not address) + (contains? (set addresses) (:address item))) + (= (:address item) address)) + (+ acc (js/parseInt (:balance item))) + acc)) + 0 + ownership))))