From b4919261cbdc7870cc0c52a46b4f4fb3dcc8e6b3 Mon Sep 17 00:00:00 2001 From: yqrashawn Date: Fri, 10 Nov 2023 09:15:02 +0800 Subject: [PATCH] fix: nav conflicts when return from bg from link (#17853) --- src/react_native/async_storage.cljs | 14 ++++++++------ src/status_im/events.cljs | 4 +++- src/status_im2/common/universal_links.cljs | 16 +++++++++++++++- .../contexts/profile/login/events.cljs | 5 +++-- .../contexts/push_notifications/events.cljs | 3 ++- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/react_native/async_storage.cljs b/src/react_native/async_storage.cljs index 5bd14289dd..7cfd9cdbfa 100644 --- a/src/react_native/async_storage.cljs +++ b/src/react_native/async_storage.cljs @@ -18,12 +18,14 @@ (log/error e)))) (defn set-item! - [k value] - (-> ^js async-storage - (.setItem (str k) - (clj->transit value)) - (.catch (fn [error] - (log/error "[async-storage]" error))))) + ([k value] (set-item! k value identity)) + ([k value cb] + (-> ^js async-storage + (.setItem (str k) + (clj->transit value)) + (.then (fn [_] (cb))) + (.catch (fn [error] + (log/error "[async-storage]" error)))))) (defn set-item-factory [] diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index c7d6b86a86..5e9e459206 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -158,7 +158,9 @@ (rf/defn on-going-in-background [{:keys [db now]}] - {:db (assoc db :app-in-background-since now)}) + {:db (-> db + (assoc :app-in-background-since now) + (dissoc :universal-links/handling))}) ;; event not implemented ;; :dispatch-n [[:audio-recorder/on-background] [:audio-message/on-background]] diff --git a/src/status_im2/common/universal_links.cljs b/src/status_im2/common/universal_links.cljs index 44001aef2d..047a8bf49a 100644 --- a/src/status_im2/common/universal_links.cljs +++ b/src/status_im2/common/universal_links.cljs @@ -3,6 +3,7 @@ [clojure.string :as string] [native-module.core :as native-module] [re-frame.core :as re-frame] + [react-native.async-storage :as async-storage] [react-native.core :as rn] [status-im2.navigation.events :as navigation] [taoensso.timbre :as log] @@ -118,7 +119,10 @@ "Dispatch url so we can get access to re-frame/db" [url] (if-not (nil? url) - (re-frame/dispatch [:universal-links/handle-url url]) + (do + (async-storage/set-item! (str :chat-id) nil #(rf/dispatch [:universal-links/remove-handling])) + (re-frame/dispatch [:universal-links/handling]) + (re-frame/dispatch [:universal-links/handle-url url])) (log/debug "universal-links: no url"))) (rf/defn on-handle @@ -151,6 +155,16 @@ (log/info :store-url-for-later) {:db (assoc db :universal-links/url url)}) +(rf/defn handling + {:events [:universal-links/handling]} + [{:keys [db]}] + {:db (assoc db :universal-links/handling true)}) + +(rf/defn remove-handling + {:events [:universal-links/remove-handling]} + [{:keys [db]}] + {:db (dissoc db :universal-links/handling)}) + (rf/defn handle-url "Store url in the database if the user is not logged in, to be processed on login, otherwise just handle it." diff --git a/src/status_im2/contexts/profile/login/events.cljs b/src/status_im2/contexts/profile/login/events.cljs index 58bf31f97a..1e005d19ce 100644 --- a/src/status_im2/contexts/profile/login/events.cljs +++ b/src/status_im2/contexts/profile/login/events.cljs @@ -113,8 +113,9 @@ (fn [accounts tokens custom-tokens favourites] (re-frame/dispatch [:wallet-legacy/initialize-wallet accounts tokens custom-tokens favourites]))] - :check-eip1559-activation {:network-id network-id} - :chat/open-last-chat (get-in db [:profile/profile :key-uid])} + :check-eip1559-activation {:network-id network-id}} + (not (:universal-links/handling db)) + (assoc :chat/open-last-chat (get-in db [:profile/profile :key-uid])) notifications-enabled? (assoc :effects/push-notifications-enable nil)) (transport/start-messenger) diff --git a/src/status_im2/contexts/push_notifications/events.cljs b/src/status_im2/contexts/push_notifications/events.cljs index 5d5dc62f30..d2c00177ba 100644 --- a/src/status_im2/contexts/push_notifications/events.cljs +++ b/src/status_im2/contexts/push_notifications/events.cljs @@ -20,8 +20,9 @@ (defn handle-notification-press [{{deep-link :deepLink} :userInfo interaction :userInteraction}] - (async-storage/set-item! (str :chat-id) nil) + (async-storage/set-item! (str :chat-id) nil #(rf/dispatch [:universal-links/remove-handling])) (when (and deep-link (or platform/ios? (and platform/android? interaction))) + (rf/dispatch [:universal-links/handling]) (rf/dispatch [:universal-links/handle-url deep-link]))) (defn listen-notifications