fix account switching

Former-commit-id: 753bdbbd4f
This commit is contained in:
Adrian Tiberius 2016-07-18 22:11:54 +03:00
parent a479ae3e15
commit 3364132b53
5 changed files with 20 additions and 11 deletions

View File

@ -57,10 +57,14 @@
(when is-login-screen? (dispatch [:navigate-to-clean default-view])))) (when is-login-screen? (dispatch [:navigate-to-clean default-view]))))
(defn logged-in [db address] (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) (log/debug "Logged in: " address account)
(realm/change-account-realm address #(when (nil? %) (realm/change-account-realm address new-account?
(initialize-account db account))))) #(if (nil? %)
(initialize-account db account)
(log/debug "Error changing acount realm: " %)))))
(register-handler :login-account (register-handler :login-account
(-> (fn [db [_ address password]] (-> (fn [db [_ address password]]

View File

@ -40,8 +40,9 @@
:key row-id}])) :key row-id}]))
(defn create-account [event] (defn create-account [event]
;(dispatch [:console-create-account])
(dispatch-sync [:reset-app]) (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"])) (dispatch [:navigate-to :chat "console"]))
(defview accounts [] (defview accounts []

View File

@ -24,6 +24,7 @@
[status-im.profile.screen :refer [profile my-profile]] [status-im.profile.screen :refer [profile my-profile]]
[status-im.utils.utils :refer [toast]] [status-im.utils.utils :refer [toast]]
[status-im.utils.encryption] [status-im.utils.encryption]
status-im.persistence.realm.core
[status-im.utils.logging :as log])) [status-im.utils.logging :as log]))
(defn init-back-button-handler! [] (defn init-back-button-handler! []

View File

@ -61,8 +61,8 @@
:user-identity nil))) :user-identity nil)))
(register-handler :initialize-account-db (register-handler :initialize-account-db
(fn [_ _] (fn [db _]
(assoc app-db (assoc db
:signed-up (storage/get kv/kv-store :signed-up) :signed-up (storage/get kv/kv-store :signed-up)
:password (storage/get kv/kv-store :password)))) :password (storage/get kv/kv-store :password))))

View File

@ -22,7 +22,7 @@
(when (cljs.core/exists? js/window) (when (cljs.core/exists? js/window)
(realm-class. (clj->js base)))) (realm-class. (clj->js base))))
(def account-realm (atom nil)) (def account-realm (atom (create-account-realm new-account-filename)))
(defn is-new-account? [] (defn is-new-account? []
(let [path (.-path @account-realm) (let [path (.-path @account-realm)
@ -50,18 +50,21 @@
(.write @account-realm #(.deleteAll @account-realm))) (.write @account-realm #(.deleteAll @account-realm)))
(defn move-file-handler [address err handler] (defn move-file-handler [address err handler]
(log/debug "Moved file with error: " err address)
(if err (if err
(log/error "Error moving account realm: " (.-message err)) (log/error "Error moving account realm: " (.-message err))
(reset! account-realm (create-account-realm address))) (reset! account-realm (create-account-realm address)))
(handler err)) (handler err))
(defn change-account-realm [address handler] (defn change-account-realm [address new-account? handler]
(let [path (.-path @account-realm) (let [path (.-path @account-realm)
realm-file (str new-account-filename ".realm") realm-file (str new-account-filename ".realm")]
is-new-account? (str/ends-with? path realm-file)] (log/debug "closing account realm: " path)
(close-account-realm) (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"))] (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))) (fs/move-file path new-path #(move-file-handler address % handler)))
(do (do
(reset! account-realm (create-account-realm address)) (reset! account-realm (create-account-realm address))