[#17357] move status-im.async-storage.core (#17380)

This commit is contained in:
flexsurfer 2023-09-25 12:45:39 +02:00 committed by GitHub
parent 53f40b55f2
commit 723573833e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 50 additions and 104 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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