Skip to content

Commit

Permalink
Use callbacks for wallet value change
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrkhalil committed Sep 6, 2022
1 parent f571b60 commit 2919cbd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 66 deletions.
82 changes: 32 additions & 50 deletions src/status_im/ui/screens/wallet/manage_connections/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,25 @@
first
:accounts))

(defn account-selector-bottom-sheet [{:keys [session show-account-selector? dapp-name dapp-url peer-id idx]}]
(defn account-selector-bottom-sheet [{:keys [session show-account-selector? idx]}]
(reagent/create-class
{:reagent-render
(fn [{:keys [session show-account-selector? dapp-name dapp-url peer-id]}]
(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]]))
:component-did-update (fn [this old-argv]
(let [account-tab-changed? (not= (get-currently-selected-accounts old-argv)
(get-currently-selected-accounts (reagent/argv this)))]
(when account-tab-changed?
(re-frame/dispatch [:wallet-connect-legacy/save-session
{:peer-id peer-id
:connector (:connector session)
:dapp-name dapp-name
:dapp-url dapp-url}]))))}))
{:reagent-render (fn [{:keys [session show-account-selector? idx]}]
(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))
(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 @@ -58,53 +48,45 @@
[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/touchable-opacity {: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? dapp-name dapp-url peer-id]} idx]
(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
[account-selector-bottom-sheet {:session session
:show-account-selector? show-account-selector?
:dapp-name dapp-name
:dapp-url dapp-url
:peer-id peer-id
:idx idx}]])
:idx idx}]])

(defn views []
(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)
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])]
{:peer-id peer-id
:dapp-url dapp-url
:dapp-name dapp-name
:visible-accounts visible-accounts
(let [show-account-selector? (reagent/atom false)]
{:visible-accounts visible-accounts
:show-account-selector? show-account-selector?
:session session}))
:session session}))
legacy-sessions))]
[rn/flat-list {:flex 1
:keyboardShouldPersistTaps :always
Expand Down
38 changes: 22 additions & 16 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 @@ -222,23 +223,25 @@
(fx/defn change-session-account
{:events [:wallet-connect-legacy/change-session-account]}
[{:keys [db] :as cofx} session account]
(let [connector (:connector session)
address (:address account)
networks (get db :networks/networks)
(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)
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])]
current-network (get networks current-network-id)
_ (prn address)
_ (prn session)
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]
:db (assoc db :wallet-connect/showing-app-management-sheet? false)}
(save-session {:peer-id peer-id
:dapp-url dapp-url
:dapp-name dapp-name
:connector connector}))))
: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 @@ -356,6 +359,9 @@
(fx/defn sync-app-db-with-wc-sessions
{:events [:sync-wallet-connect-app-sessions]}
[{:keys [db]} session-data]
(prn "IBRAHEM" (->> session-data
(map :info)
(map js/JSON.parse)))
(let [chain-id (get-in db [:networks/networks (:networks/current-network db) :config :NetworkId])
sessions (->> session-data
(map :info)
Expand Down

0 comments on commit 2919cbd

Please sign in to comment.