From c3ed15f30d7b65f4127de7bd3466ac01c9508c9a Mon Sep 17 00:00:00 2001 From: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com> Date: Fri, 7 Apr 2023 00:59:14 +0800 Subject: [PATCH] [Improvements] Syncing completed events check (#15574) --- src/status_im/events.cljs | 2 +- src/status_im/keycard/recovery.cljs | 2 +- src/status_im/mobile_sync_settings/core.cljs | 2 +- .../multiaccounts/key_storage/core.cljs | 4 ++-- src/status_im/multiaccounts/login/core.cljs | 11 +++++----- src/status_im/multiaccounts/model.cljs | 5 ++--- src/status_im/multiaccounts/model_test.cljs | 4 ++-- src/status_im/signals/core.cljs | 22 +++++++++++-------- src/status_im/utils/universal_links/core.cljs | 2 +- src/status_im2/constants.cljs | 3 +++ src/status_im2/contexts/syncing/events.cljs | 19 +++++++++++----- src/status_im2/subs/general.cljs | 2 +- 12 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 5b6bbb46b1..a05986b3d7 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -115,7 +115,7 @@ {:events [:system-theme-mode-changed]} [{:keys [db] :as cofx} _] (let [current-theme-type (get-in cofx [:db :multiaccount :appearance])] - (when (and (multiaccounts.model/logged-in? cofx) + (when (and (multiaccounts.model/logged-in? db) (= current-theme-type status-im2.constants/theme-type-system)) {:multiaccounts.ui/switch-theme-fx [(get-in db [:multiaccount :appearance]) diff --git a/src/status_im/keycard/recovery.cljs b/src/status_im/keycard/recovery.cljs index 07b1d6c656..1cf2e9f4cd 100644 --- a/src/status_im/keycard/recovery.cljs +++ b/src/status_im/keycard/recovery.cljs @@ -238,7 +238,7 @@ :t/keycard-can-use-with-new-passcode :t/keycard-backup-success-body))}} (cond - (multiaccounts.model/logged-in? cofx) + (multiaccounts.model/logged-in? db) (navigation/set-stack-root :profile-stack [:my-profile :keycard-settings]) (:multiaccounts/login db) diff --git a/src/status_im/mobile_sync_settings/core.cljs b/src/status_im/mobile_sync_settings/core.cljs index 471924c4dd..b90aaaa2d1 100644 --- a/src/status_im/mobile_sync_settings/core.cljs +++ b/src/status_im/mobile_sync_settings/core.cljs @@ -21,7 +21,7 @@ (rf/defn on-network-status-change [{:keys [db] :as cofx}] (let [initialized? (get db :network-status/initialized?) - logged-in? (multiaccounts.model/logged-in? cofx) + logged-in? (multiaccounts.model/logged-in? db) {:keys [remember-syncing-choice?]} (:multiaccount db)] (apply rf/merge diff --git a/src/status_im/multiaccounts/key_storage/core.cljs b/src/status_im/multiaccounts/key_storage/core.cljs index c567c1fa78..881da6ee41 100644 --- a/src/status_im/multiaccounts/key_storage/core.cljs +++ b/src/status_im/multiaccounts/key_storage/core.cljs @@ -21,10 +21,10 @@ (rf/defn key-and-storage-management-pressed "This event can be dispatched before login and from profile and needs to redirect accordingly" {:events [::key-and-storage-management-pressed]} - [cofx] + [{:keys [db] :as cofx}] (navigation/navigate-to cofx - (if (multiaccounts.model/logged-in? cofx) + (if (multiaccounts.model/logged-in? db) :actions-logged-in :actions-not-logged-in) nil)) diff --git a/src/status_im/multiaccounts/login/core.cljs b/src/status_im/multiaccounts/login/core.cljs index e092810634..183e182439 100644 --- a/src/status_im/multiaccounts/login/core.cljs +++ b/src/status_im/multiaccounts/login/core.cljs @@ -476,9 +476,6 @@ "Decides which root should be initialised depending on user and app state" [db] (cond - (get db :local-pairing/completed-pairing?) - (re-frame/dispatch [:syncing/pairing-completed]) - (get db :onboarding-2/new-account?) (re-frame/dispatch [:onboarding-2/finalize-setup]) @@ -490,8 +487,9 @@ (rf/defn login-only-events [{:keys [db] :as cofx} key-uid password save-password?] - (let [auth-method (:auth-method db) - new-auth-method (get-new-auth-method auth-method save-password?)] + (let [auth-method (:auth-method db) + new-auth-method (get-new-auth-method auth-method save-password?) + pairing-in-progress? (get-in db [:syncing :pairing-in-progress?])] (log/debug "[login] login-only-events" "auth-method" auth-method "new-auth-method" new-auth-method) @@ -500,7 +498,8 @@ :json-rpc/call [{:method "settings_getSettings" :on-success #(do (re-frame/dispatch [::get-settings-callback %]) - (redirect-to-root db))}]} + (when-not pairing-in-progress? + (redirect-to-root db)))}]} (notifications/load-notification-preferences) (when save-password? (keychain/save-user-password key-uid password)) diff --git a/src/status_im/multiaccounts/model.cljs b/src/status_im/multiaccounts/model.cljs index 8ab31064f2..b2684ea2dc 100644 --- a/src/status_im/multiaccounts/model.cljs +++ b/src/status_im/multiaccounts/model.cljs @@ -1,9 +1,8 @@ (ns status-im.multiaccounts.model) (defn logged-in? - [cofx] - (boolean - (get-in cofx [:db :multiaccount]))) + [{:keys [multiaccount]}] + (boolean multiaccount)) (defn credentials [cofx] diff --git a/src/status_im/multiaccounts/model_test.cljs b/src/status_im/multiaccounts/model_test.cljs index 15697c00d0..968166661b 100644 --- a/src/status_im/multiaccounts/model_test.cljs +++ b/src/status_im/multiaccounts/model_test.cljs @@ -4,6 +4,6 @@ (deftest logged-in-test (testing "multiaccount is defined" - (is (multiaccounts.model/logged-in? {:db {:multiaccount {}}}))) + (is (multiaccounts.model/logged-in? {:multiaccount {}}))) (testing "multiaccount is not there" - (is (not (multiaccounts.model/logged-in? {:db {}}))))) + (is (not (multiaccounts.model/logged-in? {}))))) diff --git a/src/status_im/signals/core.cljs b/src/status_im/signals/core.cljs index 2f6b990f91..e9fecb64bd 100644 --- a/src/status_im/signals/core.cljs +++ b/src/status_im/signals/core.cljs @@ -60,27 +60,29 @@ [{:keys [db] :as cofx} event] (log/info "local pairing signal received" {:event event}) - (let [connection-success? (= (:type event) - constants/local-pairing-event-connection-success) + (let [connection-success? (and (= (:type event) + constants/local-pairing-event-connection-success) + (= (:action event) + constants/local-pairing-action-connect)) error-on-pairing? (contains? constants/local-pairing-event-errors (:type event)) completed-pairing? (and (= (:type event) - constants/local-pairing-event-process-success) + constants/local-pairing-event-transfer-success) (= (:action event) - constants/local-pairing-action-pairing-account)) - logged-in? (multiaccounts.model/logged-in? cofx) + constants/local-pairing-action-pairing-installation)) + logged-in? (multiaccounts.model/logged-in? db) ;; since `connection-success` event is received on both sender and receiver devices ;; we check the `logged-in?` status to identify the receiver and take the user to next screen navigate-to-syncing-devices? (and connection-success? (not logged-in?)) user-in-syncing-devices-screen? (= (:view-id db) :syncing-devices)] (merge {:db (cond-> db connection-success? - (assoc :local-pairing/completed-pairing? false) + (assoc-in [:syncing :pairing-in-progress?] true) error-on-pairing? - (dissoc :local-pairing/completed-pairing?) + (update-in [:syncing :pairing-in-progress?] dissoc) completed-pairing? - (assoc :local-pairing/completed-pairing? true))} + (assoc-in [:syncing :pairing-in-progress?] false))} (when navigate-to-syncing-devices? {:dispatch [:navigate-to :syncing-devices]}) (when (and error-on-pairing? user-in-syncing-devices-screen?) @@ -89,7 +91,9 @@ :icon-color colors/danger-50 :override-theme :light :text (i18n/label :t/error-syncing-connection-failed)}] - [:navigate-back]]})))) + [:navigate-back]]}) + (when completed-pairing? + {:dispatch [:syncing/pairing-completed]})))) (rf/defn process {:events [:signals/signal-received]} diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index c432edd9d5..671d6193c1 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -184,7 +184,7 @@ on login, otherwise just handle it" {:events [:universal-links/handle-url]} [{:keys [db] :as cofx} url] - (if (and (multiaccounts.model/logged-in? cofx) (= (:app-state db) "active")) + (if (and (multiaccounts.model/logged-in? db) (= (:app-state db) "active")) (route-url cofx url) (store-url-for-later cofx url))) diff --git a/src/status_im2/constants.cljs b/src/status_im2/constants.cljs index 32f61baa54..3b93fdaaf0 100644 --- a/src/status_im2/constants.cljs +++ b/src/status_im2/constants.cljs @@ -263,6 +263,9 @@ An example of a connection string is -> cs2:5vd6J6:Jfc:27xMmHKEYwzRGXcvTtuiLZFfXscMx4Mz8d9wEHUxDj4p7:EG7Z13QScfWBJNJ5cprszzDQ5fBVsYMirXo8MaQFJvpF:3 " "cs") +(def ^:const local-pairing-role-sender "sender") +(def ^:const local-pairing-role-receiver "receiver") + ;; sender and receiver events (def ^:const local-pairing-event-connection-success "connection-success") (def ^:const local-pairing-event-connection-error "connection-error") diff --git a/src/status_im2/contexts/syncing/events.cljs b/src/status_im2/contexts/syncing/events.cljs index 8efa97966b..520f4f0fd2 100644 --- a/src/status_im2/contexts/syncing/events.cljs +++ b/src/status_im2/contexts/syncing/events.cljs @@ -13,10 +13,17 @@ (rf/defn local-pairing-completed {:events [:syncing/pairing-completed]} - [{:keys [db] :as cofx}] - (rf/merge cofx - {:db (dissoc db :local-pairing/completed-pairing?) - :dispatch [:init-root :enable-notifications]})) + [{:keys [db]}] + (let [receiver? (= (get-in db [:syncing :role]) constants/local-pairing-role-receiver)] + (merge + {:db (dissoc db :syncing)} + (when receiver? + {:dispatch [:init-root :enable-notifications]})))) + +(rf/defn local-pairing-update-role + {:events [:syncing/update-role]} + [{:keys [db]} role] + {:db (assoc-in db [:syncing :role] role)}) (defn- get-default-node-config [installation-id] @@ -44,6 +51,7 @@ :nodeConfig final-node-config :settingCurrentNetwork config/default-network :deviceType utils.platform/os}}))] + (rf/dispatch [:syncing/update-role constants/local-pairing-role-receiver]) (status/input-connection-string-for-bootstrapping connection-string config-map @@ -61,7 +69,8 @@ [:show-bottom-sheet {:content (fn [] [sheet/qr-code-view-with-connection-string - connection-string])}]))] + connection-string])}]) + (rf/dispatch [:syncing/update-role constants/local-pairing-role-sender]))] (if valid-password? (let [sha3-pwd (status/sha3 (str (security/safe-unmask-data entered-password))) key-uid (get-in db [:multiaccount :key-uid]) diff --git a/src/status_im2/subs/general.cljs b/src/status_im2/subs/general.cljs index 4ac3d95a67..70ac94ca62 100644 --- a/src/status_im2/subs/general.cljs +++ b/src/status_im2/subs/general.cljs @@ -29,7 +29,7 @@ (re-frame/reg-sub :multiaccount/logged-in? (fn [db] - (multiaccounts.model/logged-in? {:db db}))) + (multiaccounts.model/logged-in? db))) (re-frame/reg-sub :hide-screen?