Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferossgp committed Aug 3, 2020
1 parent 28bd571 commit 106ef88
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 97 deletions.
21 changes: 12 additions & 9 deletions src/status_im/qr_scanner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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 {}))
Expand All @@ -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
Expand All @@ -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 %])}})
[cofx uri]
{::router/handle-uri {:chain (ethereum/chain-keyword cofx)
:uri uri
:cb #(re-frame/dispatch [::match-scanned-value %])}})
40 changes: 26 additions & 14 deletions src/status_im/router/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)))
5 changes: 3 additions & 2 deletions src/status_im/ui/screens/add_new/new_chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@
(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])}})
{::router/handle-uri {:chain (ethereum/chain-keyword cofx)
:uri data
:cb #(re-frame/dispatch [::qr-code-handled % opts])}})
5 changes: 2 additions & 3 deletions src/status_im/ui/screens/home/sheet/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 4 additions & 16 deletions src/status_im/ui/screens/wallet/accounts/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 5 additions & 19 deletions src/status_im/ui/screens/wallet/add_new/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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}
Expand Down
22 changes: 4 additions & 18 deletions src/status_im/ui/screens/wallet/send/sheets.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/status_im/utils/universal_links/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)))
Expand Down Expand Up @@ -88,8 +93,9 @@
(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 %])}})
{::router/handle-uri {:chain (ethereum/chain-keyword cofx)
: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"
Expand Down
13 changes: 13 additions & 0 deletions src/status_im/wallet/choose_recipient/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}]
Expand Down Expand Up @@ -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
Expand Down
14 changes: 3 additions & 11 deletions src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]}
Expand Down
Loading

0 comments on commit 106ef88

Please sign in to comment.