From f8ec3f696348b37823f8294a903cd9bf91815b7f Mon Sep 17 00:00:00 2001 From: Gheorghe Pinzaru Date: Mon, 3 Aug 2020 11:07:27 +0300 Subject: [PATCH] Cleanup --- src/status_im/qr_scanner/core.cljs | 21 +++++----- src/status_im/router/core.cljs | 40 ++++++++++++------- .../ui/screens/add_new/new_chat/events.cljs | 7 ++-- .../ui/screens/home/sheet/views.cljs | 5 +-- .../ui/screens/wallet/accounts/views.cljs | 20 ++-------- .../ui/screens/wallet/add_new/views.cljs | 24 +++-------- .../ui/screens/wallet/send/sheets.cljs | 22 ++-------- src/status_im/utils/universal_links/core.cljs | 18 ++++++--- .../wallet/choose_recipient/core.cljs | 13 ++++++ src/status_im/wallet/core.cljs | 14 ++----- translations/en.json | 1 + 11 files changed, 86 insertions(+), 99 deletions(-) diff --git a/src/status_im/qr_scanner/core.cljs b/src/status_im/qr_scanner/core.cljs index cfa1e3146a8a..4d9261938094 100644 --- a/src/status_im/qr_scanner/core.cljs +++ b/src/status_im/qr_scanner/core.cljs @@ -5,7 +5,7 @@ [status-im.router.core :as router] [status-im.navigation :as navigation] [status-im.utils.utils :as utils] - [status-im.wallet.choose-recipient.core :as choose-recipient] + [status-im.ethereum.core :as ethereum] [status-im.ui.screens.add-new.new-chat.db :as new-chat.db] [status-im.utils.fx :as fx])) @@ -40,8 +40,11 @@ {:browser/show-browser-selection domain} (navigation/navigate-back))) -(fx/defn handle-private-chat [cofx {:keys [chat-id]}] - (chat/start-chat cofx chat-id {})) +(fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}] + (if-not (new-chat.db/own-public-key? db chat-id) + (chat/start-chat cofx chat-id {}) + {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) + :content (i18n/label :t/can-not-add-yourself)}})) (fx/defn handle-public-chat [cofx {:keys [topic]}] (chat/start-public-chat cofx topic {})) @@ -50,14 +53,14 @@ [{:keys [db] :as cofx} {:keys [public-key]}] (cond (and public-key (new-chat.db/own-public-key? db public-key)) - (navigation/navigate-to-cofx cofx :tabs {:screen :my-profile}) + (navigation/navigate-to-cofx cofx :tabs {:screen :profile-stack}) public-key (navigation/navigate-to-cofx (assoc-in cofx [:db :contacts/identity] public-key) :tabs {:screen :profile}))) (fx/defn handle-eip681 [cofx data] (fx/merge cofx - (choose-recipient/parse-eip681-uri-and-resolve-ens data) + {:dispatch [:wallet/parse-eip681-uri-and-resolve-ens data]} (navigation/navigate-to-cofx :tabs {:screen :wallet} nil))) (fx/defn match-scan @@ -70,11 +73,11 @@ :browser (handle-browse cofx data) :eip681 (handle-eip681 cofx data) {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) - :content "Cannot handle this code" :on-dismiss #(re-frame/dispatch [:navigate-to :home])}})) (fx/defn on-scan {:events [::on-scan-success]} - [_ uri] - {::router/handle-uri {:uri uri - :cb #(re-frame/dispatch [::match-scanned-value %])}}) + [{:keys [db]} uri] + {::router/handle-uri {:chain (ethereum/chain-keyword db) + :uri uri + :cb #(re-frame/dispatch [::match-scanned-value %])}}) diff --git a/src/status_im/router/core.cljs b/src/status_im/router/core.cljs index 62e70716f663..07288deb14b0 100644 --- a/src/status_im/router/core.cljs +++ b/src/status_im/router/core.cljs @@ -6,7 +6,6 @@ [status-im.utils.security :as security] [status-im.ethereum.eip681 :as eip681] [status-im.ethereum.ens :as ens] - [status-im.ethereum.core :as ethereum] [status-im.ethereum.resolver :as resolver] [status-im.ethereum.stateofus :as stateofus] [cljs.spec.alpha :as spec])) @@ -49,7 +48,7 @@ (resolver/pubkey registry ens-name cb))) (defn match-contact - [{:keys [user-id]} callback] + [chain {:keys [user-id]} callback] (let [public-key? (and (string? user-id) (string/starts-with? user-id "0x")) valid-key (and (spec/valid? :global/public-key user-id) @@ -60,10 +59,9 @@ :public-key user-id}) (and (not public-key?) (string? user-id)) - (let [chain (ethereum/chain-keyword {:db nil}) ;FIXME: - registry (get ens/ens-registries chain) + (let [registry (get ens/ens-registries chain) ens-name (ens-name-parse user-id) - on-success #(match-contact {:user-id %} callback)] + on-success #(match-contact chain {:user-id %} callback)] (resolver/pubkey registry ens-name on-success)) :else @@ -120,18 +118,32 @@ :uri uri :error :cannot-parse}))) -(defn handle-uri [uri cb] +(defn handle-uri [chain uri cb] (let [{:keys [handler route-params]} (match-uri uri)] - (case handler - :public-chat (match-public-chat route-params cb) - :private-chat (match-private-chat route-params cb) - :browse (match-browser route-params cb) - :user (match-contact route-params cb) - :ethereum (match-eip681 uri cb) + (cond + (= handler :public-chat) + (match-public-chat route-params cb) + + (= handler :private-chat) + (match-private-chat route-params cb) + + (= handler :browse) + (match-browser route-params cb) + + (= handler :user) + (match-contact chain route-params cb) + + (= handler :ethereum) + (match-eip681 uri cb) + + (spec/valid? :global/public-key uri) + (match-contact chain {:user-id uri} cb) + + :else (cb {:type :undefined :data uri})))) (re-frame/reg-fx ::handle-uri - (fn [{:keys [uri cb]}] - (handle-uri uri cb))) + (fn [{:keys [chain uri cb]}] + (handle-uri chain uri cb))) diff --git a/src/status_im/ui/screens/add_new/new_chat/events.cljs b/src/status_im/ui/screens/add_new/new_chat/events.cljs index 0e62c2eeaeaa..bfb04a5e04a4 100644 --- a/src/status_im/ui/screens/add_new/new_chat/events.cljs +++ b/src/status_im/ui/screens/add_new/new_chat/events.cljs @@ -102,6 +102,7 @@ (fx/defn qr-code-scanned {:events [:contact/qr-code-scanned]} - [{:keys [db] :as cofx} data opts] - {::router/handle-uri {:uri data - :cb #(re-frame/dispatch [::qr-code-handled % opts])}}) + [{:keys [db]} data opts] + {::router/handle-uri {:chain (ethereum/chain-keyword db) + :uri data + :cb #(re-frame/dispatch [::qr-code-handled % opts])}}) diff --git a/src/status_im/ui/screens/home/sheet/views.cljs b/src/status_im/ui/screens/home/sheet/views.cljs index 81d997f32c3c..db338b5c9b54 100644 --- a/src/status_im/ui/screens/home/sheet/views.cljs +++ b/src/status_im/ui/screens/home/sheet/views.cljs @@ -20,13 +20,12 @@ :align-items :center}} [quo/text {:size :large :weight :bold} - (i18n/label :t/open)] + (i18n/label :t/open-home)] [quo/button {:type :icon :theme :icon :on-press #(hide-sheet-and-dispatch [::qr-scanner/scan-code - {:title "QR SCANNER" - :handler ::qr-scanner/on-scan-success}])} + {:handler ::qr-scanner/on-scan-success}])} :main-icons/qr]] [quo/list-item {:theme :accent diff --git a/src/status_im/ui/screens/wallet/accounts/views.cljs b/src/status_im/ui/screens/wallet/accounts/views.cljs index 4aedabc29b25..4fda567394bb 100644 --- a/src/status_im/ui/screens/wallet/accounts/views.cljs +++ b/src/status_im/ui/screens/wallet/accounts/views.cljs @@ -10,7 +10,7 @@ [status-im.ui.components.react :as react] [status-im.ui.screens.wallet.accounts.sheets :as sheets] [status-im.ui.screens.wallet.accounts.styles :as styles] - [status-im.utils.utils :as utils.utils] + [status-im.qr-scanner.core :as qr-scanner] [status-im.wallet.utils :as wallet.utils] [status-im.keycard.login :as keycard.login]) (:require-macros [status-im.utils.views :as views])) @@ -91,20 +91,6 @@ :key-fn :name :render-fn (render-asset (:code currency))}])) -(defn- request-camera-permissions [] - (let [options {:handler :wallet.send/qr-scanner-result}] - (re-frame/dispatch - [:request-permissions - {:permissions [:camera] - :on-allowed - #(re-frame/dispatch [:wallet.send/qr-scanner-allowed options]) - :on-denied - #(utils.utils/set-timeout - (fn [] - (utils.utils/show-popup (i18n/label :t/error) - (i18n/label :t/camera-access-error))) - 50)}]))) - (views/defview send-button [] (views/letsubs [account [:multiaccount/default-account]] [react/view styles/send-button-container @@ -187,7 +173,9 @@ [quo/animated-header {:extended-header total-value :use-insets true - :right-accessories [{:on-press #(request-camera-permissions) + :right-accessories [{:on-press #(re-frame/dispatch + [::qr-scanner/scan-code + {:handler :wallet.send/qr-scanner-result}]) :icon :main-icons/qr :accessibility-label :accounts-qr-code} {:on-press #(re-frame/dispatch [:bottom-sheet/show-sheet diff --git a/src/status_im/ui/screens/wallet/add_new/views.cljs b/src/status_im/ui/screens/wallet/add_new/views.cljs index b55e1c3757ac..566e8b32a64c 100644 --- a/src/status_im/ui/screens/wallet/add_new/views.cljs +++ b/src/status_im/ui/screens/wallet/add_new/views.cljs @@ -10,7 +10,6 @@ [status-im.multiaccounts.db :as multiaccounts.db] [status-im.ui.components.toolbar :as toolbar] [status-im.ui.components.topbar :as topbar] - [status-im.utils.utils :as utils.utils] [status-im.ui.components.icons.vector-icons :as icons] [status-im.ui.screens.wallet.account-settings.views :as account-settings] [status-im.ethereum.core :as ethereum] @@ -19,32 +18,19 @@ [quo.core :as quo] [status-im.ui.components.bottom-panel.views :as bottom-panel])) -(defn- request-camera-permissions [] - (let [options {:handler :wallet.add-new/qr-scanner-result}] - (re-frame/dispatch - [:request-permissions - {:permissions [:camera] - :on-allowed - #(re-frame/dispatch [:wallet.add-new/qr-scanner-allowed options]) - :on-denied - #(utils.utils/set-timeout - (fn [] - (utils.utils/show-popup (i18n/label :t/error) - (i18n/label :t/camera-access-error))) - 50)}]))) - (defn add-account-topbar [type] (let [title (case type :generate :t/generate-an-account - :watch :t/add-watch-account - :seed :t/add-seed-account - :key :t/add-private-key-account + :watch :t/add-watch-account + :seed :t/add-seed-account + :key :t/add-private-key-account "")] [topbar/topbar (merge {:title title} (when (= type :watch) {:accessories [{:icon :qr - :handler #(request-camera-permissions)}]}))])) + :handler #(re-frame/dispatch [:wallet.add-new/qr-scanner + {:handler :wallet.add-new/qr-scanner-result}])}]}))])) (defn common-settings [account] [react/view {:margin-horizontal 16} diff --git a/src/status_im/ui/screens/wallet/send/sheets.cljs b/src/status_im/ui/screens/wallet/send/sheets.cljs index b284b5e4874b..c97a006cf2c1 100644 --- a/src/status_im/ui/screens/wallet/send/sheets.cljs +++ b/src/status_im/ui/screens/wallet/send/sheets.cljs @@ -6,8 +6,7 @@ [quo.core :as quo] [status-im.ui.components.chat-icon.screen :as chat-icon] [status-im.ui.components.list.views :as list] - [status-im.ui.screens.wallet.accounts.views :as wallet.accounts] - [status-im.utils.utils :as utils.utils])) + [status-im.ui.screens.wallet.accounts.views :as wallet.accounts])) (views/defview assets [address] (views/letsubs [{:keys [tokens]} [:wallet/visible-assets-with-values address] @@ -34,21 +33,6 @@ :key-fn :address :render-fn (render-account field event)}])) -(defn- request-camera-permissions [] - (let [options {:handler :wallet.send/qr-scanner-result - :cancel-handler :wallet.send/qr-scanner-cancel - :modal-opened? true}] - (re-frame/dispatch - [:request-permissions - {:permissions [:camera] - :on-allowed #(re-frame/dispatch [:wallet.send/qr-scanner-allowed options]) - :on-denied - #(utils.utils/set-timeout - (fn [] - (utils.utils/show-popup (i18n/label :t/error) - (i18n/label :t/camera-access-error))) - 50)}]))) - (defn show-accounts-list [] (re-frame/dispatch [:bottom-sheet/hide]) (js/setTimeout #(re-frame/dispatch [:bottom-sheet/show-sheet @@ -66,7 +50,9 @@ :icon :main-icons/qr :theme :accent :accessibility-label :chose-recipient-scan-qr - :on-press request-camera-permissions} + :on-press #(re-frame/dispatch [:wallet.send/qr-scanner {:handler :wallet.send/qr-scanner-result + :cancel-handler :wallet.send/qr-scanner-cancel + :modal-opened? true}])} {:title (i18n/label :t/recipient-code) :icon :main-icons/address :theme :accent diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index 22a7e69ec60c..32e1d3c1de9c 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -5,6 +5,8 @@ [status-im.chat.models :as chat] [status-im.constants :as constants] [status-im.router.core :as router] + [status-im.i18n :as i18n] + [status-im.ethereum.core :as ethereum] [status-im.ui.components.react :as react] [status-im.ui.screens.add-new.new-chat.db :as new-chat.db] [status-im.navigation :as navigation] @@ -41,9 +43,12 @@ (log/info "universal-links: handling browse" domain) {:browser/show-browser-selection domain}) -(fx/defn handle-private-chat [cofx {:keys [chat-id]}] +(fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}] (log/info "universal-links: handling private chat" chat-id) - (chat/start-chat cofx chat-id {})) + (if-not (new-chat.db/own-public-key? db chat-id) + (chat/start-chat cofx chat-id {}) + {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) + :content (i18n/label :t/can-not-add-yourself)}})) (fx/defn handle-public-chat [cofx {:keys [topic]}] (log/info "universal-links: handling public chat" topic) @@ -54,7 +59,7 @@ (log/info "universal-links: handling view profile" public-key) (cond (and public-key (new-chat.db/own-public-key? db public-key)) - (navigation/navigate-to-cofx cofx :my-profile nil) + (navigation/navigate-to-cofx cofx :tabs {:screen :profile-stack}) public-key (navigation/navigate-to-cofx (assoc-in cofx [:db :contacts/identity] public-key) :profile nil))) @@ -87,9 +92,10 @@ (fx/defn route-url "Match a url against a list of routes and handle accordingly" - [cofx url] - {::router/handle-uri {:uri url - :cb #(re-frame/dispatch [::match-value url %])}}) + [{:keys [db]} url] + {::router/handle-uri {:chain (ethereum/chain-keyword db) + :uri url + :cb #(re-frame/dispatch [::match-value url %])}}) (fx/defn store-url-for-later "Store the url in the db to be processed on login" diff --git a/src/status_im/wallet/choose_recipient/core.cljs b/src/status_im/wallet/choose_recipient/core.cljs index 6ed2e4127ccf..6c7381a4b333 100644 --- a/src/status_im/wallet/choose_recipient/core.cljs +++ b/src/status_im/wallet/choose_recipient/core.cljs @@ -8,10 +8,13 @@ [status-im.i18n :as i18n] [status-im.utils.money :as money] [status-im.utils.fx :as fx] + [status-im.qr-scanner.core :as qr-scaner] + [status-im.ui.components.bottom-sheet.core :as bottom-sheet] [status-im.navigation :as navigation] [clojure.string :as string] [status-im.ethereum.stateofus :as stateofus])) +;; FIXME(Ferossgp): Should be part of QR scanner not wallet (fx/defn toggle-flashlight {:events [:wallet/toggle-flashlight]} [{:keys [db]}] @@ -107,12 +110,22 @@ {:data uri :chain current-chain-id})})))) {:ui/show-error (i18n/label :t/wallet-invalid-address {:data uri})}))) +(fx/defn qr-scanner-allowed + {:events [:wallet.send/qr-scanner]} + [{:keys [db] :as cofx} options] + (fx/merge cofx + (when (:modal-opened? options) + {:db (assoc-in db [:wallet/prepare-transaction :modal-opened?] true)}) + (bottom-sheet/hide-bottom-sheet) + (qr-scaner/scan-qr-code options))) + (fx/defn qr-scanner-cancel {:events [:wallet.send/qr-scanner-cancel]} [{db :db} _] {:db (assoc-in db [:wallet/prepare-transaction :modal-opened?] false)}) (fx/defn parse-eip681-uri-and-resolve-ens + {:events [:wallet/parse-eip681-uri-and-resolve-ens]} [{db :db :as cofx} {:keys [message uri paths ens-names error]}] (if-not error ;; first we get a vector of ens-names to resolve and a vector of paths of diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index 29b6d4798fcf..bf5179341049 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -2,6 +2,7 @@ (:require [re-frame.core :as re-frame] [status-im.multiaccounts.update.core :as multiaccounts.update] [status-im.constants :as constants] + [status-im.qr-scanner.core :as qr-scaner] [status-im.waku.core :as waku] [status-im.ethereum.core :as ethereum] [status-im.ethereum.eip55 :as eip55] @@ -548,21 +549,12 @@ :amount amount :from-command? true})}) -(fx/defn qr-scanner-allowed - {:events [:wallet.send/qr-scanner-allowed]} - [{:keys [db] :as cofx} options] - (fx/merge cofx - (when (:modal-opened? options) - {:db (assoc-in db [:wallet/prepare-transaction :modal-opened?] true)}) - (bottom-sheet/hide-bottom-sheet) - (navigation/navigate-to-cofx :qr-scanner options))) - (fx/defn view-only-qr-scanner-allowed - {:events [:wallet.add-new/qr-scanner-allowed]} + {:events [:wallet.add-new/qr-scanner]} [{:keys [db] :as cofx} options] (fx/merge cofx {:db (update-in db [:add-account] dissoc :address)} - (navigation/navigate-to-cofx :qr-scanner options))) + (qr-scaner/scan-qr-code options))) (fx/defn wallet-send-set-symbol {:events [:wallet.send/set-symbol]} diff --git a/translations/en.json b/translations/en.json index 1185aab5754a..a1d4f786ffc9 100644 --- a/translations/en.json +++ b/translations/en.json @@ -756,6 +756,7 @@ "okay": "Okay", "on": "On", "open": "Open", + "open-home": "Open...", "open-dapp": "Open ÐApp", "open-dapp-store": "Discover ÐApps", "open-nfc-settings": "Open NFC settings",