From 3364132b53bfe3501c40870f56a3861d17d8d4a8 Mon Sep 17 00:00:00 2001 From: Adrian Tiberius Date: Mon, 18 Jul 2016 22:11:54 +0300 Subject: [PATCH] fix account switching Former-commit-id: 753bdbbd4f66433997a5397b728b5002eb9f91e1 --- src/status_im/accounts/handlers.cljs | 10 +++++++--- src/status_im/accounts/screen.cljs | 3 ++- src/status_im/android/core.cljs | 1 + src/status_im/handlers.cljs | 4 ++-- src/status_im/persistence/realm/core.cljs | 13 ++++++++----- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/status_im/accounts/handlers.cljs b/src/status_im/accounts/handlers.cljs index 408ff3a973..c9de35487b 100644 --- a/src/status_im/accounts/handlers.cljs +++ b/src/status_im/accounts/handlers.cljs @@ -57,10 +57,14 @@ (when is-login-screen? (dispatch [:navigate-to-clean default-view])))) (defn logged-in [db address] - (let [account (get-in db [:accounts address])] + (let [account (get-in db [:accounts address]) + is-login-screen? (= (:view-id db) :login) + new-account? (not is-login-screen?)] (log/debug "Logged in: " address account) - (realm/change-account-realm address #(when (nil? %) - (initialize-account db account))))) + (realm/change-account-realm address new-account? + #(if (nil? %) + (initialize-account db account) + (log/debug "Error changing acount realm: " %))))) (register-handler :login-account (-> (fn [db [_ address password]] diff --git a/src/status_im/accounts/screen.cljs b/src/status_im/accounts/screen.cljs index ea9f1a361a..ef847e1748 100644 --- a/src/status_im/accounts/screen.cljs +++ b/src/status_im/accounts/screen.cljs @@ -40,8 +40,9 @@ :key row-id}])) (defn create-account [event] - ;(dispatch [:console-create-account]) (dispatch-sync [:reset-app]) + ; add accounts screen to history ( maybe there is a better way ? ) + (dispatch [:navigate-to-clean :accounts]) (dispatch [:navigate-to :chat "console"])) (defview accounts [] diff --git a/src/status_im/android/core.cljs b/src/status_im/android/core.cljs index 51f4059906..e544d3b811 100644 --- a/src/status_im/android/core.cljs +++ b/src/status_im/android/core.cljs @@ -24,6 +24,7 @@ [status-im.profile.screen :refer [profile my-profile]] [status-im.utils.utils :refer [toast]] [status-im.utils.encryption] + status-im.persistence.realm.core [status-im.utils.logging :as log])) (defn init-back-button-handler! [] diff --git a/src/status_im/handlers.cljs b/src/status_im/handlers.cljs index 86167112cb..84887faa34 100644 --- a/src/status_im/handlers.cljs +++ b/src/status_im/handlers.cljs @@ -61,8 +61,8 @@ :user-identity nil))) (register-handler :initialize-account-db - (fn [_ _] - (assoc app-db + (fn [db _] + (assoc db :signed-up (storage/get kv/kv-store :signed-up) :password (storage/get kv/kv-store :password)))) diff --git a/src/status_im/persistence/realm/core.cljs b/src/status_im/persistence/realm/core.cljs index 801e245409..8a8dbc1ff6 100644 --- a/src/status_im/persistence/realm/core.cljs +++ b/src/status_im/persistence/realm/core.cljs @@ -22,7 +22,7 @@ (when (cljs.core/exists? js/window) (realm-class. (clj->js base)))) -(def account-realm (atom nil)) +(def account-realm (atom (create-account-realm new-account-filename))) (defn is-new-account? [] (let [path (.-path @account-realm) @@ -50,18 +50,21 @@ (.write @account-realm #(.deleteAll @account-realm))) (defn move-file-handler [address err handler] + (log/debug "Moved file with error: " err address) (if err (log/error "Error moving account realm: " (.-message err)) (reset! account-realm (create-account-realm address))) (handler err)) -(defn change-account-realm [address handler] +(defn change-account-realm [address new-account? handler] (let [path (.-path @account-realm) - realm-file (str new-account-filename ".realm") - is-new-account? (str/ends-with? path realm-file)] + realm-file (str new-account-filename ".realm")] + (log/debug "closing account realm: " path) (close-account-realm) - (if is-new-account? + (log/debug "is new account? " new-account?) + (if new-account? (let [new-path (str/replace path realm-file (str address ".realm"))] + (log/debug "Moving file to " new-path) (fs/move-file path new-path #(move-file-handler address % handler))) (do (reset! account-realm (create-account-realm address))