77 lines
3.5 KiB
Plaintext
Raw Normal View History

(ns status-im.init.core
(:require [clojure.string :as string]
[re-frame.core :as re-frame]
2019-08-01 22:11:59 +02:00
[status-im.multiaccounts.login.core :as multiaccounts.login]
[status-im.native-module.core :as status]
[status-im.network.net-info :as network]
[status-im.db :refer [app-db]]
[status-im.utils.fx :as fx]
[status-im.theme.core :as theme]
[status-im.utils.theme :as utils.theme]))
(fx/defn initialize-app-db
"Initialize db to initial state"
[{{:keys [hardwallet initial-props supported-biometric-auth app-active-since]
:network/keys [type]} :db
now :now}]
{:db (assoc app-db
:initial-props initial-props
:network/type type
:hardwallet (dissoc hardwallet :secrets)
:supported-biometric-auth supported-biometric-auth
:app-active-since (or app-active-since now)
:multiaccounts/loading true)})
(fx/defn initialize-views
{:events [::initialize-view]}
[cofx {:keys [logout?]}]
(let [{{:multiaccounts/keys [multiaccounts]} :db} cofx]
(when (and (seq multiaccounts) (not logout?))
(let [{:keys [key-uid public-key photo-path name]}
(first (sort-by :timestamp > (vals multiaccounts)))]
(multiaccounts.login/open-login cofx key-uid photo-path name public-key)))))
2019-08-01 22:11:59 +02:00
(fx/defn initialize-multiaccounts
{:events [::initialize-multiaccounts]}
[{:keys [db] :as cofx} all-multiaccounts {:keys [logout?]}]
(let [multiaccounts (reduce (fn [acc {:keys [key-uid keycard-pairing]
:as multiaccount}]
(-> (assoc acc key-uid multiaccount)
(assoc-in [key-uid :keycard-pairing]
(when-not (string/blank? keycard-pairing)
keycard-pairing))))
2019-08-01 22:11:59 +02:00
{}
all-multiaccounts)]
(fx/merge cofx
{:db (-> db
(assoc :multiaccounts/multiaccounts multiaccounts)
(assoc :multiaccounts/logout? logout?)
(assoc :multiaccounts/loading false))
;; NOTE: Try to dispatch later navigation because of that https://github.com/react-navigation/react-navigation/issues/6879
:dispatch-later [{:dispatch [::initialize-view {:logout? logout?}]
:ms 100}]})))
2019-08-01 22:11:59 +02:00
(fx/defn start-app [cofx]
(fx/merge cofx
{:get-supported-biometric-auth nil
2020-03-16 14:02:35 +01:00
::init-theme nil
::open-multiaccounts #(re-frame/dispatch [::initialize-multiaccounts % {:logout? false}])
2019-08-01 22:11:59 +02:00
:ui/listen-to-window-dimensions-change nil
::network/listen-to-network-info nil
2020-02-25 20:36:29 +02:00
:hardwallet/register-card-events nil
:hardwallet/check-nfc-support nil
:hardwallet/check-nfc-enabled nil
:hardwallet/retrieve-pairings nil}
2019-08-01 22:11:59 +02:00
(initialize-app-db)))
(re-frame/reg-fx
2019-08-01 22:11:59 +02:00
::open-multiaccounts
(fn [callback]
(status/open-accounts callback)))
2020-03-16 14:02:35 +01:00
(re-frame/reg-fx
::init-theme
(fn []
(utils.theme/add-mode-change-listener #(re-frame/dispatch [:system-theme-mode-changed %]))
(theme/change-theme (if (utils.theme/is-dark-mode) :dark :light))))