From f98e013cffbcc2aa231adab7fa4240bb79bfbed4 Mon Sep 17 00:00:00 2001 From: yenda Date: Fri, 28 Sep 2018 16:48:59 +0200 Subject: [PATCH] fix login after logout when opening app from a notification moves most of the fx that are suppose to happen only at app startup to an init/start-app fn so that init/initialize-app which is also called after logout doesn't repeat unecessary fxs --- src/status_im/events.cljs | 7 +----- src/status_im/init/core.cljs | 34 ++++++++++++++++++--------- src/status_im/notifications/core.cljs | 23 +++++------------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 051bd1c416..0e7fc75627 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -56,7 +56,7 @@ (handlers/register-handler-fx :init/app-started (fn [cofx _] - (init/initialize-keychain cofx))) + (init/start-app cofx))) (handlers/register-handler-fx :init.callback/get-encryption-key-success @@ -665,11 +665,6 @@ (fn [cofx [_ event]] (notifications/handle-push-notification cofx event))) -(handlers/register-handler-fx - :notifications.callback/notification-stored - (fn [cofx _] - (accounts.login/user-login cofx))) - (handlers/register-handler-fx :notifications.callback/get-fcm-token-success (fn [{:keys [db]} [_ fcm-token]] diff --git a/src/status_im/init/core.cljs b/src/status_im/init/core.cljs index 9ad93e04eb..efb0ad9b06 100644 --- a/src/status_im/init/core.cljs +++ b/src/status_im/init/core.cljs @@ -54,9 +54,20 @@ [cofx] {:keychain/get-encryption-key [:init.callback/get-encryption-key-success]}) +(fx/defn start-app [cofx] + (fx/merge cofx + {:init/get-device-UUID nil + :ui/listen-to-window-dimensions-change nil + :notifications/handle-initial-push-notification nil + :network/listen-to-network-status nil + :network/listen-to-connection-status nil + :hardwallet/check-nfc-support nil + :hardwallet/check-nfc-enabled nil} + (initialize-keychain))) + (fx/defn initialize-app-db "Initialize db to initial state" - [{{:keys [status-module-initialized? view-id + [{{:keys [status-module-initialized? view-id hardwallet network-status network peers-count peers-summary device-UUID] :node/keys [status] :or {network (get app-db :network)}} :db}] @@ -69,19 +80,13 @@ :node/status status :network network :device-UUID device-UUID - :view-id view-id)}) + :view-id view-id + :hardwallet (select-keys hardwallet [:nfc-enabled? :nfc-supported?]))}) (fx/defn initialize-app [cofx encryption-key] (fx/merge cofx - {:init/get-device-UUID nil - :init/init-store encryption-key - :ui/listen-to-window-dimensions-change nil - :notifications/handle-initial-push-notification nil - :network/listen-to-network-status nil - :network/listen-to-connection-status nil - :hardwallet/check-nfc-support nil - :hardwallet/check-nfc-enabled nil} + {:init/init-store encryption-key} (initialize-app-db) (node/initialize nil))) @@ -123,7 +128,14 @@ (let [{{:accounts/keys [accounts] :as db} :db} cofx] (if (empty? accounts) (navigation/navigate-to-clean cofx :intro nil) - (let [{:keys [address photo-path name]} (first (sort-by :last-sign-in > (vals accounts)))] + (let [account-with-notification (first (keys (:push-notifications/stored db))) + selection-fn (if (not-empty account-with-notification) + #(filter (fn [account] + (= account-with-notification + (:public-key account))) + %) + #(sort-by :last-sign-in > %)) + {:keys [address photo-path name]} (first (selection-fn (vals accounts)))] (accounts.login/open-login cofx address photo-path name))))) (fx/defn load-accounts-and-initialize-views diff --git a/src/status_im/notifications/core.cljs b/src/status_im/notifications/core.cljs index f794913707..e071983c8b 100644 --- a/src/status_im/notifications/core.cljs +++ b/src/status_im/notifications/core.cljs @@ -3,6 +3,7 @@ [re-frame.core :as re-frame] [status-im.react-native.js-dependencies :as rn] [taoensso.timbre :as log] + [status-im.accounts.login.core :as accounts.login] [status-im.chat.models :as chat-model] [status-im.utils.platform :as platform] [status-im.utils.fx :as fx])) @@ -73,26 +74,14 @@ (then #(log/debug "Notification channel created:" channel-id) #(log/error "Notification channel creation error:" channel-id %))))) - (fx/defn store-event [{:keys [db] :as cofx} {:keys [from to]}] - (let [{:keys [address photo-path name]} (->> (get-in cofx [:db :accounts/accounts]) - vals - (filter #(= (:public-key %) to)) - first)] - (when address - {:db (assoc-in db [:push-notifications/stored to] from) - :dispatch [:notifications.callback/notification-stored address photo-path name]}))) - (fx/defn handle-push-notification [{:keys [db] :as cofx} {:keys [from to] :as event}] (let [current-public-key (get-in cofx [:db :current-public-key])] - (if current-public-key - ;; TODO(yenda) why do we ignore the notification if - ;; it is not for the current account ? - (when (= to current-public-key) - (fx/merge cofx - {:db (update db :push-notifications/stored dissoc to)} - (chat-model/navigate-to-chat from nil))) - (store-event cofx event)))) + (if (= to current-public-key) + (fx/merge cofx + {:db (update db :push-notifications/stored dissoc to)} + (chat-model/navigate-to-chat from nil)) + {:db (assoc-in db [:push-notifications/stored to] from)}))) (defn parse-notification-payload [s] (try