Skip to content

Commit

Permalink
[#17357] move status-im.async-storage.core (#17380)
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer authored and ibrkhalil committed Oct 1, 2023
1 parent 3ad32f2 commit b862437
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
(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))))

(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))
Expand All @@ -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
Expand All @@ -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)))
13 changes: 0 additions & 13 deletions src/status_im/async_storage/transit.cljs

This file was deleted.

46 changes: 0 additions & 46 deletions src/status_im/communities/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)})
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/notifications/local.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]}
Expand All @@ -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]}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/status_im2/common/async_storage.cljs
Original file line number Diff line number Diff line change
@@ -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)))
2 changes: 1 addition & 1 deletion src/status_im2/contexts/chat/composer/effects.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/status_im2/contexts/chat/composer/keyboard.cljs
Original file line number Diff line number Diff line change
@@ -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]))
Expand Down
8 changes: 4 additions & 4 deletions src/status_im2/contexts/chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]]))

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/status_im2/contexts/communities/discover/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/status_im2/contexts/shell/jump_to/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
2 changes: 1 addition & 1 deletion src/status_im2/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/status_im2/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down

0 comments on commit b862437

Please sign in to comment.