Skip to content

Commit

Permalink
Implement wallet account switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahem authored and ibrahem committed Sep 7, 2022
1 parent 33aa595 commit 3e6563d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/quo/design_system/colors.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
:interactive-02 "rgba(35,37,47,1)"
:interactive-03 "rgba(255,255,255,0.1)"
:interactive-04 "rgba(131,140,145,1)"
:ui-background "rgba(20,20,20,1)"
:ui-background "rgba(13,22,37,1)"
:ui-01 "rgba(37,37,40,1)"
:ui-02 "rgba(0,0,0,0.1)"
:ui-03 "rgba(0,0,0,0.86)"
Expand Down
4 changes: 2 additions & 2 deletions src/quo2/components/avatars/user_avatar.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
:border-radius inner-dimensions})

(defn container [inner-dimensions outer-dimensions & children]
(let [dark-kw (if dark? :dark :light)]
(let [bg-theme :light]
[rn/view {:style (merge {:background-color (colors/custom-color
:turquoise
dark-kw)
bg-theme)
:justify-content :center
:align-items :center}
(container-styling inner-dimensions outer-dimensions))}
Expand Down
95 changes: 69 additions & 26 deletions src/status_im/ui/screens/wallet/manage_connections/views.cljs
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
(ns status-im.ui.screens.wallet.manage-connections.views
(:require [re-frame.core :as re-frame]
(:require [quo.core :as quo]
[quo.design-system.colors :as colors]
[quo.react-native :as rn]
[re-frame.core :as re-frame]
[reagent.core :as reagent]
[status-im.ui.components.bottom-panel.views :as bottom-panel]
[status-im.ui.components.icons.icons :as icons]
[status-im.ui.components.react :as react]
[status-im.ui.screens.wallet-connect.session-proposal.views :refer [app-management-sheet-view]]
[status-im.ui.screens.wallet.manage-connections.styles :as styles]
[quo.core :as quo]
[quo.design-system.colors :as colors]
[status-im.ui.components.icons.icons :as icons]
[status-im.utils.handlers :refer [<sub]]
[status-im.utils.utils :as utils]))

(defn print-session-info [session]
(let [peer-meta (get-in session [:params 0 :peerMeta])
peer-id (get-in session [:params 0 :peerId])
name (get peer-meta :name)
url (get peer-meta :url)
account (get-in session [:params 0 :accounts 0])
icons (get peer-meta :icons)
icon-uri (first (status-im.utils.utils/exclude-svg-resources icons))
visible-accounts @(re-frame/subscribe [:visible-accounts-without-watch-only])
(defn get-currently-selected-accounts
[selected-tab]
(-> selected-tab
second
:session
:params
first
:accounts))

(defn account-selector-bottom-sheet [{:keys [session show-account-selector? idx]}]
(reagent/create-class
{:reagent-render (fn []
(when @show-account-selector?
[rn/view {:style (cond-> {:height 50}
(= idx 0) (assoc :margin-top 50))}
[bottom-panel/animated-bottom-panel
session
app-management-sheet-view
true]]))}))

(defn print-session-info [{:keys [session visible-accounts show-account-selector?]}]
(let [peer-meta (get-in session [:params 0 :peerMeta])
peer-id (get-in session [:params 0 :peerId])
name (get peer-meta :name)
url (get peer-meta :url)
account (get-in session [:params 0 :accounts 0])
icons (get peer-meta :icons)
icon-uri (first (status-im.utils.utils/exclude-svg-resources icons))
selected-account (first (filter
#(= account
(:address %))
Expand All @@ -25,28 +48,48 @@
[rn/view
[:<>
[rn/view {:style styles/app-row}
[react/image {:style styles/dapp-icon :source {:uri icon-uri}}]
[react/image {:style styles/dapp-icon
:source {:uri icon-uri}}]
[rn/view {:style styles/app-column}
[quo/text {:style styles/dapp-name} name]
[quo/text {:style styles/dapp-url} url]]

[rn/view {:flex-direction :row
:position :absolute
:right 10
:align-items :center}
[rn/touchable-opacity {:style styles/delete-icon-container
:position :absolute
:right 10
:align-items :center}
[rn/touchable-opacity {:style styles/delete-icon-container
:on-press #(re-frame/dispatch [:wallet-connect-legacy/disconnect session])}

[icons/icon :icons/delete {:width 20
:height 20
[icons/icon :icons/delete {:width 20
:height 20
:container-style {:elevation 3}
:color colors/red}]]

:color colors/red}]]
(when selected-account ;; The account might not be available in theory, if deleted
[rn/view {:style (styles/selected-account-container (:color selected-account))}
[rn/touchable-opacity {:style (styles/selected-account-container (:color selected-account))
:on-press #(swap! show-account-selector? not)}
[rn/text {:style styles/selected-account} (:name selected-account)]])]]]]))

(defn list-item [{:keys [session visible-accounts show-account-selector?]} idx]
[rn/view
[print-session-info {:session session
:visible-accounts visible-accounts
:show-account-selector? show-account-selector?}]
[account-selector-bottom-sheet {:session session
:show-account-selector? show-account-selector?
:idx idx}]])

(defn views []
(let [legacy-sessions @(re-frame/subscribe [:wallet-connect-legacy/sessions])]
[rn/view {:margin-top 10}
(doall (map print-session-info legacy-sessions))]))
(let [legacy-sessions (<sub [:wallet-connect-legacy/sessions])
visible-accounts (<sub [:visible-accounts-without-watch-only])
items (doall (map (fn [session]
(let [show-account-selector? (reagent/atom false)]
{:visible-accounts visible-accounts
:show-account-selector? show-account-selector?
:session session}))
legacy-sessions))]
[rn/flat-list {:flex 1
:keyboardShouldPersistTaps :always
:data items
:render-fn list-item
:key-fn str}]))
87 changes: 57 additions & 30 deletions src/status_im/wallet_connect_legacy/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@

(re-frame/reg-fx
:wc-1-update-session
(fn [[^js connector chain-id address]]
(.updateSession connector (clj->js {:chainId chain-id :accounts [address]}))))
(fn [[^js connector chain-id address on-sucess]]
(.updateSession connector (clj->js {:chainId chain-id :accounts [address]}))
(on-sucess)))

(re-frame/reg-fx
:wc-1-kill-session
Expand Down Expand Up @@ -142,30 +143,40 @@
(:wallet-connect-legacy/sessions db))]
{:wc-1-clean-up-sessions connectors}))

(fx/defn save-session
{:events [:wallet-connect-legacy/save-session]}
[{:keys [db]} {:keys [peer-id dapp-name dapp-url connector]}]
(let [info (.stringify js/JSON (.-session connector))]
{::json-rpc/call [{:method "wakuext_addWalletConnectSession"
:params [{:id peer-id
:info info
;; info will be the updated value
:dappName dapp-name
:dappUrl dapp-url}]
:on-success #(log/info "wakuext_addWalletConnectSession success call back , data =>" %)
:on-error #(log/error "wakuext_addWalletConnectSession error call back , data =>" %)}]}))

(fx/defn session-connected
{:events [:wallet-connect-legacy/created]}
[{:keys [db]} session]
[{:keys [db] :as cofx} session]
(let [connector (get db :wallet-connect-legacy/proposal-connector)
session (assoc (types/js->clj session)
:wc-version constants/wallet-connect-version-1
:connector connector)
info (.stringify js/JSON (.-session connector))
peer-id (get-in session [:params 0 :peerId])
dapp-name (get-in session [:params 0 :peerMeta :name])
dapp-url (get-in session [:params 0 :peerMeta :url])]
{:show-wallet-connect-success-sheet nil
:db (-> db
(assoc :wallet-connect/session-connected session)
(update :wallet-connect-legacy/sessions
conj
session))
::json-rpc/call [{:method "wakuext_addWalletConnectSession"
:params [{:id peer-id
:info info
:dappName dapp-name
:dappUrl dapp-url}]
:on-success #(log/info "wakuext_addWalletConnectSession success call back , data =>" %)
:on-error #(log/error "wakuext_addWalletConnectSession error call back , data =>" %)}]}))
(fx/merge cofx
{:db (-> db
(assoc :wallet-connect/session-connected session)
(update :wallet-connect-legacy/sessions
conj
session))
:show-wallet-connect-success-sheet nil}
(save-session {:peer-id peer-id
:dapp-name dapp-name
:dapp-url dapp-url
:connector connector}))))

(fx/defn manage-app
{:events [:wallet-connect-legacy/manage-app]}
Expand Down Expand Up @@ -211,16 +222,24 @@

(fx/defn change-session-account
{:events [:wallet-connect-legacy/change-session-account]}
[{:keys [db]} session account]
(let [connector (:connector session)
address (:address account)
networks (get db :networks/networks)
[{:keys [db] :as cofx} session account]
(let [connector (:connector session)
address (:address account)
networks (get db :networks/networks)
current-network-id (get db :networks/current-network)
current-network (get networks current-network-id)
chain-id (get-in current-network [:config :NetworkId])]
{:hide-wallet-connect-app-management-sheet nil
:wc-1-update-session [connector chain-id address]
:db (assoc db :wallet-connect/showing-app-management-sheet? false)}))
current-network (get networks current-network-id)
dapp-name (get-in session [:params 0 :peerMeta :name])
dapp-url (get-in session [:params 0 :peerMeta :url])
peer-id (get-in session [:params 0 :peerId])
chain-id (get-in current-network [:config :NetworkId])
on-sucess #(re-frame/dispatch [:wallet-connect-legacy/save-session {:peer-id peer-id
:dapp-url dapp-url
:dapp-name dapp-name
:connector connector}])]
(fx/merge cofx
{:hide-wallet-connect-app-management-sheet nil
:wc-1-update-session [connector chain-id address on-sucess]
:db (assoc db :wallet-connect/showing-app-management-sheet? false)})))

(fx/defn disconnect-by-peer-id
{:events [:wallet-connect-legacy/disconnect-by-peer-id]}
Expand Down Expand Up @@ -268,10 +287,18 @@
(let [sessions (get db :wallet-connect-legacy/sessions)
accounts-new (:accounts (first (:params payload)))
session (first (filter #(= (:connector %) connector) sessions))
updated-session (assoc-in session [:params 0 :accounts] accounts-new)]
{:db (-> db
(assoc :wallet-connect-legacy/sessions (conj (filter #(not= (:connector %) connector) sessions) updated-session))
(dissoc :wallet-connect/session-managed))}))
updated-session (assoc-in session [:params 0 :accounts] accounts-new)
peer-id (get-in updated-session [:params 0 :peerId])
dapp-name (get-in updated-session [:params 0 :peerMeta :name])
dapp-url (get-in updated-session [:params 0 :peerMeta :url])]
(fx/merge cofx
{:db (-> db
(assoc :wallet-connect-legacy/sessions (conj (filter #(not= (:connector %) connector) sessions) updated-session))
(dissoc :wallet-connect/session-managed))
:dispatch [:wallet-connect-legacy/save-session {:peer-id peer-id
:dapp-url dapp-url
:dapp-name dapp-name
:connector connector}]})))

(fx/defn wallet-connect-legacy-complete-transaction
{:events [:wallet-connect-legacy.dapp/transaction-on-result]}
Expand Down

0 comments on commit 3e6563d

Please sign in to comment.