|
1 | 1 | (ns status-im.contexts.shell.share.wallet.view
|
2 | 2 | (:require
|
| 3 | + [oops.core :as oops] |
3 | 4 | [quo.core :as quo]
|
4 | 5 | [react-native.core :as rn]
|
5 | 6 | [react-native.platform :as platform]
|
|
11 | 12 | [status-im.contexts.wallet.sheets.network-preferences.view :as network-preferences]
|
12 | 13 | [utils.i18n :as i18n]
|
13 | 14 | [utils.image-server :as image-server]
|
| 15 | + [utils.number] |
14 | 16 | [utils.re-frame :as rf]))
|
15 | 17 |
|
16 | 18 | (def qr-size 500)
|
|
44 | 46 | (rf/dispatch [:hide-bottom-sheet])
|
45 | 47 | (reset! selected-networks (map #(get utils/id->network %)
|
46 | 48 | chain-ids)))}])}]))
|
47 |
| -(defn- wallet-qr-code-item-internal |
48 |
| - [props] |
49 |
| - (let [{:keys [account width index]} props |
50 |
| - selected-networks (reagent/atom [:ethereum :optimism :arbitrum]) |
51 |
| - wallet-type (reagent/atom :legacy) |
52 |
| - on-settings-press #(open-preferences selected-networks) |
53 |
| - on-legacy-press #(reset! wallet-type :legacy) |
54 |
| - on-multichain-press #(reset! wallet-type :multichain)] |
| 49 | +(defn- wallet-qr-code-item |
| 50 | + [{:keys [account index]}] |
| 51 | + (let [{window-width :width} (rn/get-window) |
| 52 | + selected-networks (reagent/atom [:ethereum :optimism :arbitrum]) |
| 53 | + wallet-type (reagent/atom :legacy) |
| 54 | + on-settings-press #(open-preferences selected-networks) |
| 55 | + on-legacy-press #(reset! wallet-type :legacy) |
| 56 | + on-multichain-press #(reset! wallet-type :multichain)] |
55 | 57 | (fn []
|
56 | 58 | (let [share-title (str (:name account) " " (i18n/label :t/address))
|
57 | 59 | qr-url (utils/get-wallet-qr {:wallet-type @wallet-type
|
|
62 | 64 | :port (rf/sub [:mediaserver/port])
|
63 | 65 | :qr-size qr-size
|
64 | 66 | :error-level :highest})]
|
65 |
| - [rn/view {:style {:height qr-size :width width :margin-left (if (zero? index) 0 -30)}} |
| 67 | + [rn/view {:style {:width window-width :margin-left (if (zero? index) 0 -30)}} |
66 | 68 | [rn/view {:style style/qr-code-container}
|
67 | 69 | [quo/share-qr-code
|
68 | 70 | {:type :wallet
|
| 71 | + :width (- window-width (* style/screen-padding 2)) |
69 | 72 | :address @wallet-type
|
70 | 73 | :qr-image-uri qr-media-server-uri
|
71 | 74 | :qr-data qr-url
|
|
79 | 82 | :on-legacy-press on-legacy-press
|
80 | 83 | :on-settings-press on-settings-press}]]]))))
|
81 | 84 |
|
82 |
| -(def wallet-qr-code-item (memoize wallet-qr-code-item-internal)) |
83 |
| - |
84 | 85 | (defn- indicator
|
85 | 86 | [active?]
|
86 |
| - [rn/view |
87 |
| - {:style (wallet-style/indicator-wrapper-style active?)}]) |
| 87 | + [rn/view {:style (wallet-style/indicator-wrapper-style active?)}]) |
88 | 88 |
|
89 | 89 | (defn- indicator-list
|
90 |
| - [indicator-count current-index] |
91 |
| - [rn/view |
92 |
| - {:style wallet-style/indicator-list-style} |
93 |
| - (for [i (range indicator-count)] |
94 |
| - (let [current-index (cond (<= current-index 0) 0 |
95 |
| - (>= current-index (dec indicator-count)) (dec indicator-count) |
96 |
| - :else current-index)] |
97 |
| - ^{:key i} [indicator (= current-index i)]))]) |
| 90 | + [num-indicators current-index] |
| 91 | + [rn/view {:style wallet-style/indicator-list-style} |
| 92 | + (for [i (range num-indicators)] |
| 93 | + ^{:key i} [indicator (= current-index i)])]) |
98 | 94 |
|
99 | 95 | (defn render-item
|
100 | 96 | [item]
|
101 |
| - (let [width (rf/sub [:dimensions/window-width])] |
102 |
| - [wallet-qr-code-item |
103 |
| - {:account item |
104 |
| - :index (:position item) |
105 |
| - :width width}])) |
| 97 | + [wallet-qr-code-item |
| 98 | + {:account item |
| 99 | + :index (:position item)}]) |
| 100 | + |
| 101 | +(defn- qr-code-visualized-index |
| 102 | + [offset qr-code-size num-qr-codes] |
| 103 | + (-> (+ (/ offset qr-code-size) 0.5) |
| 104 | + (int) |
| 105 | + (utils.number/value-in-range 0 (dec num-qr-codes)))) |
106 | 106 |
|
107 | 107 | (defn wallet-tab
|
108 | 108 | []
|
109 |
| - (let [accounts (rf/sub [:wallet/accounts]) |
110 |
| - width (rf/sub [:dimensions/window-width]) |
111 |
| - current-index (reagent/atom 0)] |
| 109 | + (let [current-index (reagent/atom 0) |
| 110 | + {window-width :width} (rn/get-window) |
| 111 | + qr-code-size (- window-width 30)] |
112 | 112 | (fn []
|
113 |
| - [rn/view |
114 |
| - [rn/flat-list |
115 |
| - {:horizontal true |
116 |
| - :deceleration-rate 0.9 |
117 |
| - :snap-to-alignment :start |
118 |
| - :snap-to-interval (- width 30) |
119 |
| - :disable-interval-momentum true |
120 |
| - :scroll-event-throttle 64 |
121 |
| - :data accounts |
122 |
| - :directional-lock-enabled true |
123 |
| - :shows-horizontal-scroll-indicator false |
124 |
| - :on-scroll (fn [e] |
125 |
| - (reset! current-index (js/Math.ceil |
126 |
| - (/ e.nativeEvent.contentOffset.x |
127 |
| - width)))) |
128 |
| - :render-fn render-item}] |
129 |
| - (when (> (count accounts) 1) |
130 |
| - [rn/view |
131 |
| - {:style {:margin-top 20}} |
132 |
| - (indicator-list (count accounts) @current-index)])]))) |
| 113 | + (let [accounts (rf/sub [:wallet/accounts]) |
| 114 | + num-accounts (count accounts) |
| 115 | + on-scroll (rn/use-callback |
| 116 | + (fn [e] |
| 117 | + (let [offset-x (oops/oget e "nativeEvent.contentOffset.x") |
| 118 | + index (qr-code-visualized-index offset-x qr-code-size num-accounts)] |
| 119 | + (reset! current-index index))))] |
| 120 | + [rn/view |
| 121 | + [rn/flat-list |
| 122 | + {:horizontal true |
| 123 | + :deceleration-rate 0.9 |
| 124 | + :snap-to-alignment :start |
| 125 | + :snap-to-interval qr-code-size |
| 126 | + :disable-interval-momentum true |
| 127 | + :scroll-event-throttle 64 |
| 128 | + :data accounts |
| 129 | + :directional-lock-enabled true |
| 130 | + :shows-horizontal-scroll-indicator false |
| 131 | + :on-scroll on-scroll |
| 132 | + :render-fn render-item}] |
| 133 | + (when (> num-accounts 1) |
| 134 | + [rn/view {:style {:margin-top 20}} |
| 135 | + [indicator-list num-accounts @current-index]])])))) |
0 commit comments