[#6382]: Makes extensions dev-mode aware

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Aleksandr Pantiukhov 2018-10-18 14:14:12 +02:00 committed by Julien Eluard
parent c857eb5357
commit 2c65077270
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
9 changed files with 79 additions and 43 deletions

View File

@ -41,13 +41,26 @@
(wallet.settings.models/wallet-autoconfig-tokens)
(accounts.update/account-update {:wallet-set-up-passed? true} {})))
(fx/defn switch-dev-mode [cofx dev-mode?]
(merge (accounts.update/account-update cofx
{:dev-mode? dev-mode?}
{})
(if dev-mode?
{:dev-server/start nil}
{:dev-server/stop nil})))
(fx/defn update-dev-server-state
[_ dev-mode?]
(if dev-mode?
{:dev-server/start nil}
{:dev-server/stop nil}))
(fx/defn update-extensions-state
[{{:account/keys [account]} :db} dev-mode?]
(let [extensions (-> account :extensions vals)]
(if dev-mode?
{:extensions/load {:extensions extensions
:follow-up :extensions/add}}
{:dispatch [:extensions/deactivate-all (filter :active? extensions)]})))
(fx/defn switch-dev-mode [{{:account/keys [account]} :db :as cofx} dev-mode?]
(fx/merge cofx
(update-dev-server-state dev-mode?)
(update-extensions-state dev-mode?)
(accounts.update/account-update {:dev-mode? dev-mode?}
{})))
(fx/defn switch-web3-opt-in-mode [{:keys [db] :as cofx} opt-in]
(let [settings (get-in db [:account/account :settings])]

View File

@ -26,8 +26,8 @@
(defn- deserialize-extensions [extensions]
(reduce-kv
(fn [acc _ {:keys [id] :as extension}]
(assoc acc id extension))
(fn [acc _ {:keys [name] :as extension}]
(assoc acc name extension))
{}
extensions))

View File

@ -421,8 +421,8 @@
(handlers/register-handler-fx
:extensions.ui/add-extension-pressed
(fn [cofx [_ extension-id]]
(extensions/edit cofx extension-id)))
(fn [cofx [_ name]]
(extensions/edit cofx name)))
(handlers/register-handler-fx
:extensions.ui/input-changed
@ -431,13 +431,13 @@
(handlers/register-handler-fx
:extensions.ui/activation-checkbox-pressed
(fn [cofx [_ id state]]
(extensions/toggle-activation cofx id state)))
(fn [cofx [_ name state]]
(extensions/toggle-activation cofx name state)))
(handlers/register-handler-fx
:extensions.ui/show-button-pressed
(fn [_ [_ uri]]
{:extension/load [uri :extension/stage]}))
(fn [cofx [_ url]]
(extensions/load cofx url)))
(handlers/register-handler-fx
:extensions.ui/install-button-pressed

View File

@ -230,7 +230,7 @@
(defn valid-uri? [s]
(boolean
(when s
(re-matches (re-pattern (str "^" uri-prefix "\\w+@\\w+")) (string/trim s)))))
(re-matches (re-pattern (str "^" uri-prefix "\\w+@.+")) (string/trim s)))))
(defn url->uri [s]
(when s
@ -250,12 +250,12 @@
[{:keys [db]} input-key value]
{:db (update db :extensions/manage assoc input-key {:value value})})
(fx/defn fetch [cofx id]
(get-in cofx [:db :account/account :extensions id]))
(fx/defn fetch [cofx name]
(get-in cofx [:db :account/account :extensions name]))
(fx/defn edit
[cofx id]
(let [{:keys [url]} (fetch cofx id)]
[cofx name]
(let [{:keys [url]} (fetch cofx name)]
(fx/merge (set-input cofx :url (str url))
(navigation/navigate-to-cofx :edit-extension nil))))
@ -279,7 +279,7 @@
:name (str extension-key)
:url (:value url)
:active? true}
new-extensions (assoc (:extensions account) (:id extension) extension)]
new-extensions (assoc (:extensions account) (:name extension) extension)]
(fx/merge cofx
{:ui/show-confirmation {:title (i18n/label :t/success)
:content (i18n/label :t/extension-installed)
@ -289,19 +289,31 @@
(add extension-data true))))
(fx/defn toggle-activation
[cofx id state]
[cofx name state]
(let [toggle-fn (get {true registry/activate
false registry/deactivate}
state)
extensions (get-in cofx [:db :account/account :extensions])
new-extensions (assoc-in extensions [id :active?] state)
extension-key (get-in extensions [id :name])]
new-extensions (assoc-in extensions [name :active?] state)]
(fx/merge cofx
(accounts.update/account-update {:extensions new-extensions} {:success-event nil})
#(toggle-fn extension-key %))))
#(toggle-fn name %))))
(defn load-active-extensions
[{:keys [db]}]
(let [extensions (vals (get-in db [:account/account :extensions]))]
(doseq [{:keys [url active?]} extensions]
(load-from url #(re-frame/dispatch [:extension/add (-> % read-extension parse :data) active?])))))
(fx/defn load
[cofx url]
(if (->> (get-in cofx [:db :account/account :extensions])
vals
(filter #(= (:url %) (string/trim url)))
first)
{:utils/show-popup {:title (i18n/label :t/error)
:content (i18n/label :t/extension-is-already-added)}}
{:extensions/load {:extensions [{:url (string/trim url)
:active? true}]
:follow-up :extensions/stage}}))
(fx/defn activate-extensions
[{{:account/keys [account]} :db}]
(let [{:keys [extensions dev-mode?]} account]
(when dev-mode?
{:extensions/load {:extensions (vals extensions)
:follow-up :extensions/add}})))

View File

@ -212,7 +212,7 @@
(chat-loading/initialize-pending-messages)
(browser/initialize-browsers)
(browser/initialize-dapp-permissions)
#(extensions/load-active-extensions %)
(extensions/activate-extensions)
#(when-not platform/desktop?
(initialize-wallet %))
(accounts.update/update-sign-in-time)

View File

@ -1,23 +1,32 @@
(ns status-im.ui.screens.extensions.add.events
(:require [re-frame.core :as re-frame]
(:require [pluto.registry :as registry]
[re-frame.core :as re-frame]
[status-im.extensions.core :as extensions]
[status-im.ui.screens.navigation :as navigation]
[status-im.utils.handlers :as handlers]
[status-im.utils.fx :as fx]))
(re-frame/reg-fx
:extension/load
(fn [[url follow-up-event]]
(extensions/load-from url #(re-frame/dispatch [follow-up-event (-> % extensions/read-extension extensions/parse)]))))
:extensions/load
(fn [{:keys [extensions follow-up]}]
(doseq [{:keys [url active?]} extensions]
(extensions/load-from url #(re-frame/dispatch [follow-up (-> % extensions/read-extension extensions/parse) active?])))))
(handlers/register-handler-fx
:extension/stage
:extensions/stage
(fn [{:keys [db] :as cofx} [_ extension-data]]
(fx/merge cofx
{:db (assoc db :staged-extension extension-data)}
(navigation/navigate-to-cofx :show-extension nil))))
(handlers/register-handler-fx
:extension/add
(fn [cofx [_ data active?]]
(extensions/add cofx data active?)))
:extensions/add
(fn [cofx [_ {:keys [data]} active?]]
(extensions/add cofx data active?)))
(handlers/register-handler-fx
:extensions/deactivate-all
(fn [cofx [_ extensions]]
(apply fx/merge cofx (map (fn [{:keys [name]}]
(partial registry/deactivate name))
extensions))))

View File

@ -14,10 +14,10 @@
[react/view (styles/wnode-icon true)
[vector-icons/icon :icons/wnode {:color :white}]])
(defn- render-extension [{:keys [id name url active?]}]
(defn- render-extension [{:keys [name url active?]}]
[list/list-item-with-checkbox
{:checked? active?
:on-value-change #(re-frame/dispatch [:extensions.ui/activation-checkbox-pressed id %])}
:on-value-change #(re-frame/dispatch [:extensions.ui/activation-checkbox-pressed name %])}
[list/item
wnode-icon
[list/item-content

View File

@ -5,6 +5,7 @@
[re-frame.core :as re-frame]
[status-im.accounts.db :as accounts.db]
[status-im.chat.models :as chat]
[status-im.extensions.core :as extensions]
[status-im.ui.components.list-selection :as list-selection]
[status-im.ui.components.react :as react]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db]
@ -71,7 +72,7 @@
(fx/defn handle-extension [cofx url]
(log/info "universal-links: handling url profile" url)
{:extension/load [url :extensions/stage]})
(extensions/load cofx url))
(defn handle-not-found [full-url]
(log/info "universal-links: no handler for " full-url))

View File

@ -754,5 +754,6 @@
"share-public-chat-text": "Check out this public chat on the Status app: {{link}}",
"share-dapp-text": "Check out this DApp I'm using on Status: {{link}}",
"network-invalid-url": "Network URL is invalid",
"network-invalid-status-code": "Invalid status code: {{code}}"
"network-invalid-status-code": "Invalid status code: {{code}}",
"extension-is-already-added": "The extension is already added"
}