From 723573833ea0c16be17b3c9c1ee5367212ef5061 Mon Sep 17 00:00:00 2001 From: flexsurfer Date: Mon, 25 Sep 2023 12:45:39 +0200 Subject: [PATCH] [#17357] move status-im.async-storage.core (#17380) --- .../async_storage.cljs} | 44 +++++++++--------- src/status_im/async_storage/transit.cljs | 13 ------ src/status_im/communities/core.cljs | 46 ------------------- src/status_im/events.cljs | 7 ++- src/status_im/notifications/local.cljs | 2 +- src/status_im/wallet/core.cljs | 14 +++--- src/status_im2/common/async_storage.cljs | 7 +++ .../contexts/chat/composer/effects.cljs | 2 +- .../contexts/chat/composer/keyboard.cljs | 2 +- src/status_im2/contexts/chat/events.cljs | 8 ++-- .../contexts/communities/discover/view.cljs | 2 - .../onboarding/common/background/view.cljs | 2 +- .../contexts/shell/jump_to/utils.cljs | 2 +- src/status_im2/core.cljs | 2 +- src/status_im2/events.cljs | 1 + 15 files changed, 50 insertions(+), 104 deletions(-) rename src/{status_im/async_storage/core.cljs => react_native/async_storage.cljs} (54%) delete mode 100644 src/status_im/async_storage/transit.cljs create mode 100644 src/status_im2/common/async_storage.cljs diff --git a/src/status_im/async_storage/core.cljs b/src/react_native/async_storage.cljs similarity index 54% rename from src/status_im/async_storage/core.cljs rename to src/react_native/async_storage.cljs index 80312fae3b..d032549849 100644 --- a/src/status_im/async_storage/core.cljs +++ b/src/react_native/async_storage.cljs @@ -1,30 +1,37 @@ -(ns status-im.async-storage.core +(ns react-native.async-storage (:require ["@react-native-async-storage/async-storage" :default async-storage] - [goog.functions :as f] - [re-frame.core :as re-frame] - [status-im.async-storage.transit :refer [clj->transit transit->clj]] - [taoensso.timbre :as log])) + [cognitect.transit :as transit] + [taoensso.timbre :as log] + goog.functions)) (def ^:private debounce-ms 250) -(def key->string str) +(def ^:private reader (transit/reader :json)) +(def ^:private writer (transit/writer :json)) + +(defn clj->transit [o] (transit/write writer o)) +(defn transit->clj + [o] + (try (transit/read reader o) + (catch :default e + (log/error e)))) (defn set-item! [k value] (-> ^js async-storage - (.setItem (key->string k) + (.setItem (str k) (clj->transit value)) (.catch (fn [error] (log/error "[async-storage]" error))))) -(defn- set-item-factory +(defn set-item-factory [] (let [tmp-storage (atom {}) - debounced (f/debounce (fn [] - (doseq [[k v] @tmp-storage] - (swap! tmp-storage dissoc k) - (set-item! k v))) - debounce-ms)] + debounced (goog.functions/debounce (fn [] + (doseq [[k v] @tmp-storage] + (swap! tmp-storage dissoc k) + (set-item! k v))) + debounce-ms)] (fn [items] (swap! tmp-storage merge items) (debounced)))) @@ -32,7 +39,7 @@ (defn get-items [ks cb] (-> ^js async-storage - (.multiGet (to-array (map key->string ks))) + (.multiGet (to-array (map str ks))) (.then (fn [^js data] (cb (->> (js->clj data) (map (comp transit->clj second)) @@ -44,7 +51,7 @@ (defn get-item [k cb] (-> ^js async-storage - (.getItem (key->string k)) + (.getItem (str k)) (.then (fn [^js data] (-> data js->clj @@ -53,10 +60,3 @@ (.catch (fn [error] (cb nil) (log/error "[async-storage]" error))))) - -(re-frame/reg-fx ::set! (set-item-factory)) - -(re-frame/reg-fx - ::get - (fn [{ks :keys cb :cb}] - (get-items ks cb))) diff --git a/src/status_im/async_storage/transit.cljs b/src/status_im/async_storage/transit.cljs deleted file mode 100644 index 841dd94cb5..0000000000 --- a/src/status_im/async_storage/transit.cljs +++ /dev/null @@ -1,13 +0,0 @@ -(ns status-im.async-storage.transit - (:require [cognitect.transit :as transit] - [taoensso.timbre :as log])) - -(def reader (transit/reader :json)) -(def writer (transit/writer :json)) - -(defn clj->transit [o] (transit/write writer o)) -(defn transit->clj - [o] - (try (transit/read reader o) - (catch :default e - (log/error e)))) diff --git a/src/status_im/communities/core.cljs b/src/status_im/communities/core.cljs index 3d4710a1a9..23a0784307 100644 --- a/src/status_im/communities/core.cljs +++ b/src/status_im/communities/core.cljs @@ -6,7 +6,6 @@ [quo2.foundations.colors :as quo2.colors] [re-frame.core :as re-frame] [status-im.utils.deprecated-types :as types] - [status-im.async-storage.core :as async-storage] [status-im.ui.components.emoji-thumbnail.styles :as emoji-thumbnail-styles] [status-im.utils.universal-links.core :as universal-links] [status-im.bottom-sheet.events :as bottom-sheet] @@ -815,51 +814,6 @@ :on-success #(re-frame/dispatch [:sanitize-messages-and-process-response %]) :on-error #(log/error "failed to reorder community category" %)}]}) -(defn category-hash - [public-key community-id category-id] - (hash (str public-key community-id category-id))) - -(rf/defn store-category-state - {:events [::store-category-state]} - [{:keys [db]} community-id category-id state-open?] - (let [public-key (get-in db [:profile/profile :public-key]) - hash (category-hash public-key community-id category-id)] - {::async-storage/set! {hash state-open?}})) - -(rf/defn update-category-states-in-db - {:events [::category-states-loaded]} - [{:keys [db]} community-id hashes states] - (let [public-key (get-in db [:profile/profile :public-key]) - categories-old (get-in db [:communities community-id :categories]) - categories (reduce (fn [acc [category-id category]] - (let [hash (get hashes category-id) - state (get states hash) - category-updated (assoc category :state state)] - (assoc acc category-id category-updated))) - {} - categories-old)] - {:db (update-in db [:communities community-id :categories] merge categories)})) - -(rf/defn load-category-states - {:events [:communities/load-category-states]} - [{:keys [db]} community-id] - (let [public-key (get-in db [:profile/profile :public-key]) - categories (get-in db [:communities community-id :categories]) - {:keys [keys hashes]} (reduce (fn [acc category] - (let [category-id (get category 0) - hash (category-hash - public-key - community-id - category-id)] - (-> acc - (assoc-in [:hashes category-id] hash) - (update :keys #(conj % hash))))) - {} - categories)] - {::async-storage/get {:keys keys - :cb #(re-frame/dispatch - [::category-states-loaded community-id hashes %])}})) - ;; Note - dispatch is used to make sure we are opening community once `pop-to-root` is processed. ;; Don't directly merge effects using `navigation/navigate-to`, because it will work in debug and ;; release, but for e2e `pop-to-root` closes even currently opened community diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 1a8aa38601..bfaa44f354 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -2,7 +2,6 @@ (:require clojure.set [re-frame.core :as re-frame] - [status-im.async-storage.core :as async-storage] status-im.backup.core status-im.bootnodes.core status-im.browser.core @@ -193,9 +192,9 @@ (rf/merge cofx (cond (= :chat view-id) - {::async-storage/set! {:chat-id (get-in cofx [:db :current-chat-id]) - :key-uid (get-in cofx [:db :profile/profile :key-uid])} - :db (assoc db :screens/was-focused-once? true)} + {:async-storage-set {:chat-id (get-in cofx [:db :current-chat-id]) + :key-uid (get-in cofx [:db :profile/profile :key-uid])} + :db (assoc db :screens/was-focused-once? true)} (not (get db :screens/was-focused-once?)) {:db (assoc db :screens/was-focused-once? true)}) diff --git a/src/status_im/notifications/local.cljs b/src/status_im/notifications/local.cljs index 06ee95a3b6..8aa31d7d11 100644 --- a/src/status_im/notifications/local.cljs +++ b/src/status_im/notifications/local.cljs @@ -4,7 +4,7 @@ [clojure.string :as string] [quo.platform :as platform] [re-frame.core :as re-frame] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im.ethereum.decode :as decode] [status-im.ethereum.tokens :as tokens] [utils.i18n :as i18n] diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index 92f701fb6c..dbd745dcd3 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -3,7 +3,7 @@ [clojure.set :as set] [clojure.string :as string] [re-frame.core :as re-frame] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im.bottom-sheet.events :as bottom-sheet] [status-im.contact.db :as contact.db] [status-im.ethereum.core :as ethereum] @@ -826,8 +826,8 @@ (rf/defn get-buy-crypto-preference {:events [::get-buy-crypto]} [_] - {::async-storage/get {:keys [:buy-crypto-hidden] - :cb #(re-frame/dispatch [::store-buy-crypto-preference %])}}) + {:async-storage-get {:keys [:buy-crypto-hidden] + :cb #(re-frame/dispatch [::store-buy-crypto-preference %])}}) (rf/defn wallet-will-focus {:events [::wallet-stack]} @@ -848,8 +848,8 @@ (rf/defn hide-buy-crypto {:events [::hide-buy-crypto]} [{:keys [db]}] - {:db (assoc db :wallet/buy-crypto-hidden true) - ::async-storage/set! {:buy-crypto-hidden true}}) + {:db (assoc db :wallet/buy-crypto-hidden true) + :async-storage-set {:buy-crypto-hidden true}}) (rf/defn store-buy-crypto {:events [::store-buy-crypto-preference]} @@ -1025,8 +1025,8 @@ (rf/defn switch-transactions-management-enabled {:events [:multiaccounts.ui/switch-transactions-management-enabled]} [{:keys [db]} enabled?] - {::async-storage/set! {:transactions-management-enabled? enabled?} - :db (assoc db :wallet/transactions-management-enabled? enabled?)}) + {:async-storage-set {:transactions-management-enabled? enabled?} + :db (assoc db :wallet/transactions-management-enabled? enabled?)}) (re-frame/reg-fx :wallet/initialize-transactions-management-enabled diff --git a/src/status_im2/common/async_storage.cljs b/src/status_im2/common/async_storage.cljs new file mode 100644 index 0000000000..303073a9af --- /dev/null +++ b/src/status_im2/common/async_storage.cljs @@ -0,0 +1,7 @@ +(ns status-im2.common.async-storage + (:require [re-frame.core :as re-frame] + react-native.async-storage)) + +(re-frame/reg-fx :async-storage-set (react-native.async-storage/set-item-factory)) +(re-frame/reg-fx :async-storage-get + (fn [{ks :keys cb :cb}] (react-native.async-storage/get-items ks cb))) diff --git a/src/status_im2/contexts/chat/composer/effects.cljs b/src/status_im2/contexts/chat/composer/effects.cljs index 62e9c79164..ca37d98160 100644 --- a/src/status_im2/contexts/chat/composer/effects.cljs +++ b/src/status_im2/contexts/chat/composer/effects.cljs @@ -5,7 +5,7 @@ [react-native.core :as rn] [react-native.platform :as platform] [react-native.reanimated :as reanimated] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im2.contexts.chat.composer.constants :as constants] [status-im2.contexts.chat.composer.keyboard :as kb] [status-im2.contexts.chat.composer.utils :as utils] diff --git a/src/status_im2/contexts/chat/composer/keyboard.cljs b/src/status_im2/contexts/chat/composer/keyboard.cljs index fa99c1a80f..0d519b2761 100644 --- a/src/status_im2/contexts/chat/composer/keyboard.cljs +++ b/src/status_im2/contexts/chat/composer/keyboard.cljs @@ -1,6 +1,6 @@ (ns status-im2.contexts.chat.composer.keyboard (:require [oops.core :as oops] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [react-native.core :as rn] [react-native.platform :as platform] [react-native.reanimated :as reanimated])) diff --git a/src/status_im2/contexts/chat/events.cljs b/src/status_im2/contexts/chat/events.cljs index 3092931e88..383b258365 100644 --- a/src/status_im2/contexts/chat/events.cljs +++ b/src/status_im2/contexts/chat/events.cljs @@ -18,7 +18,7 @@ [reagent.core :as reagent] [quo2.foundations.colors :as colors] [re-frame.core :as re-frame] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im2.contexts.shell.jump-to.constants :as shell.constants] [status-im2.common.muting.helpers :refer [format-mute-till]])) @@ -173,9 +173,9 @@ (chat.state/reset-visible-item) (rf/merge cofx (merge - {:db (dissoc db :current-chat-id) - ::async-storage/set! {:chat-id nil - :key-uid nil}} + {:db (dissoc db :current-chat-id) + :async-storage-set {:chat-id nil + :key-uid nil}} (let [community-id (get-in db [:chats chat-id :community-id])] ;; When navigating back from community chat to community, update switcher card ;; A close chat event is also called while opening any chat. diff --git a/src/status_im2/contexts/communities/discover/view.cljs b/src/status_im2/contexts/communities/discover/view.cljs index 053e83b636..072defd6a6 100644 --- a/src/status_im2/contexts/communities/discover/view.cljs +++ b/src/status_im2/contexts/communities/discover/view.cljs @@ -23,7 +23,6 @@ :on-press #(rf/dispatch [:communities/navigate-to-community (:id item)])}] [quo/community-list-item {:on-press (fn [] - (rf/dispatch [:communities/load-category-states (:id item)]) (rf/dispatch [:dismiss-keyboard]) (rf/dispatch [:communities/navigate-to-community (:id item)])) :on-long-press #(rf/dispatch @@ -147,7 +146,6 @@ [quo/community-list-item {:on-press (fn [] - (rf/dispatch [:communities/load-category-states (:id community)]) (rf/dispatch [:dismiss-keyboard]) (rf/dispatch [:communities/navigate-to-community (:id community)])) :on-long-press #(js/alert "TODO: to be implemented")} diff --git a/src/status_im2/contexts/onboarding/common/background/view.cljs b/src/status_im2/contexts/onboarding/common/background/view.cljs index 052523e91f..54986220b2 100644 --- a/src/status_im2/contexts/onboarding/common/background/view.cljs +++ b/src/status_im2/contexts/onboarding/common/background/view.cljs @@ -6,7 +6,7 @@ [oops.core :refer [oget]] [react-native.platform :as platform] [status-im2.common.resources :as resources] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im2.contexts.shell.jump-to.state :as shell.state] [status-im2.contexts.onboarding.common.carousel.view :as carousel] [status-im2.contexts.onboarding.common.background.style :as style] diff --git a/src/status_im2/contexts/shell/jump_to/utils.cljs b/src/status_im2/contexts/shell/jump_to/utils.cljs index 72dd630e91..274d400406 100644 --- a/src/status_im2/contexts/shell/jump_to/utils.cljs +++ b/src/status_im2/contexts/shell/jump_to/utils.cljs @@ -5,7 +5,7 @@ [react-native.platform :as platform] [react-native.safe-area :as safe-area] [react-native.reanimated :as reanimated] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [status-im2.contexts.shell.jump-to.state :as state] [status-im2.contexts.shell.jump-to.constants :as shell.constants] [quo2.theme :as quo.theme])) diff --git a/src/status_im2/core.cljs b/src/status_im2/core.cljs index f4da25ca9c..7990fb0be4 100644 --- a/src/status_im2/core.cljs +++ b/src/status_im2/core.cljs @@ -15,7 +15,7 @@ [status-im2.setup.dev :as dev] [status-im2.setup.global-error :as global-error] [status-im2.common.log :as log] - [status-im.async-storage.core :as async-storage] + [react-native.async-storage :as async-storage] [native-module.core :as native-module] [status-im.notifications.local :as notifications] [status-im.utils.universal-links.core :as utils.universal-links] diff --git a/src/status_im2/events.cljs b/src/status_im2/events.cljs index d808c61fa2..d4c6cf9920 100644 --- a/src/status_im2/events.cljs +++ b/src/status_im2/events.cljs @@ -16,6 +16,7 @@ status-im2.contexts.shell.share.events status-im2.contexts.syncing.events status-im2.contexts.onboarding.common.overlay.events + status-im2.common.async-storage [status-im2.db :as db] [utils.re-frame :as rf]))