-
Notifications
You must be signed in to change notification settings - Fork 985
/
view.cljs
244 lines (228 loc) · 9.12 KB
/
view.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(ns status-im2.common.scan-qr-code.view
(:require [clojure.string :as string]
[oops.core :as oops]
[quo.core :as quo]
[quo.foundations.colors :as colors]
[quo.theme :as quo.theme]
[react-native.blur :as blur]
[react-native.camera-kit :as camera-kit]
[react-native.core :as rn]
[react-native.hole-view :as hole-view]
[react-native.permissions :as permissions]
[react-native.safe-area :as safe-area]
[reagent.core :as reagent]
[status-im2.common.device-permissions :as device-permissions]
[status-im2.common.scan-qr-code.style :as style]
[utils.debounce :as debounce]
[utils.i18n :as i18n]
[utils.re-frame :as rf]
[utils.transforms :as transforms]))
(defonce camera-permission-granted? (reagent/atom false))
(defn- header
[{:keys [title subtitle]}]
[:<>
[rn/view {:style style/header-container}
[quo/button
{:icon-only? true
:type :grey
:background :blur
:size 32
:accessibility-label :close-scan-qr-code
:on-press #(rf/dispatch [:navigate-back])}
:i/arrow-left]]
[quo/text
{:size :heading-1
:weight :semi-bold
:style style/header-text}
title]
[quo/text
{:size :paragraph-1
:weight :regular
:style style/header-sub-text}
subtitle]])
(defn get-labels-and-on-press-method
[]
{:title-label-key :t/enable-access-to-camera
:description-label-key :t/to-scan-a-qr-enable-your-camera
:button-icon :i/camera
:button-label :t/enable-camera
:accessibility-label :request-camera-permission
:on-press (fn []
(device-permissions/camera #(reset! camera-permission-granted? true)))})
(defn- camera-permission-view
[]
(let [{:keys [title-label-key
description-label-key
button-icon
button-label
accessibility-label
on-press]} (get-labels-and-on-press-method)]
[rn/view {:style style/camera-permission-container}
[quo/text
{:size :paragraph-1
:weight :medium
:style style/enable-camera-access-header}
(i18n/label title-label-key)]
[quo/text
{:size :paragraph-2
:weight :regular
:style style/enable-camera-access-sub-text}
(i18n/label description-label-key)]
[quo/button
{:icon-left button-icon
:type :primary
:size 32
:accessibility-label accessibility-label
:customization-color :blue
:on-press on-press}
(i18n/label button-label)]]))
(defn- qr-scan-hole-area
[qr-view-finder]
[rn/view
{:style style/qr-view-finder
:on-layout (fn [event]
(let [layout (transforms/js->clj (oops/oget event "nativeEvent.layout"))
view-finder (assoc layout :height (:width layout))]
(reset! qr-view-finder view-finder)))}])
(defn- white-border
[corner]
(let [border-styles (style/white-border corner)]
[rn/view
[rn/view {:style (border-styles :border)}]
[rn/view {:style (border-styles :tip-1)}]
[rn/view {:style (border-styles :tip-2)}]]))
(defn- white-square
[layout-size]
[rn/view {:style (style/qr-view-finder-container layout-size)}
[rn/view {:style style/view-finder-border-container}
[white-border :top-left]
[white-border :top-right]]
[rn/view {:style style/view-finder-border-container}
[white-border :bottom-left]
[white-border :bottom-right]]])
(defn- viewfinder
[qr-view-finder]
(let [layout-size (+ (:width qr-view-finder) 2)]
[rn/view {:style (style/viewfinder-container qr-view-finder)}
[white-square layout-size]
[quo/text
{:size :paragraph-2
:weight :regular
:style style/viewfinder-text}
(i18n/label :t/ensure-qr-code-is-in-focus-to-scan)]]))
(defn- scan-qr-code-tab
[qr-view-finder]
(if (and @camera-permission-granted?
(boolean (not-empty qr-view-finder)))
[viewfinder qr-view-finder]
[camera-permission-view]))
(defn- check-qr-code-and-navigate
[{:keys [event error-message validate-fn on-success-scan on-failed-scan]}]
(let [scanned-value (string/trim (oops/oget event "nativeEvent.codeStringValue"))
validated? (if validate-fn (validate-fn scanned-value) true)]
(if validated?
(on-success-scan scanned-value)
(do
(on-failed-scan)
(debounce/debounce-and-dispatch
[:toasts/upsert
{:icon :i/info
:icon-color colors/danger-50
:theme :dark
:text error-message}]
300)))))
(defn- render-camera
[{:keys [torch-mode qr-view-finder scan-code? validate-fn error-message set-qr-code-succeeded
set-rescan-timeout]}]
[:<>
[rn/view {:style style/camera-container}
[camera-kit/camera
{:style style/camera-style
:camera-type camera-kit/camera-type-back
:zoom-mode :off
:torch-mode torch-mode
:scan-barcode true
:on-read-code #(when scan-code?
(check-qr-code-and-navigate {:event %
:validate-fn validate-fn
:error-message error-message
:on-success-scan set-qr-code-succeeded
:on-failed-scan set-rescan-timeout}))}]]
[hole-view/hole-view
{:style style/hole
:holes [(assoc qr-view-finder :borderRadius 16)]}
[blur/view
{:style style/absolute-fill
:blur-amount 10
:blur-type :transparent
:overlay-color colors/neutral-80-opa-80
:background-color colors/neutral-80-opa-80}]]])
(defn- set-listener-torch-off-on-app-inactive
[torch-atm]
(let [set-torch-off-fn #(when (not= % "active") (reset! torch-atm false))
app-state-listener (.addEventListener rn/app-state "change" set-torch-off-fn)]
#(.remove app-state-listener)))
(defn f-view-internal
[{:keys [title subtitle validate-fn on-success-scan error-message]}]
(let [insets (safe-area/get-insets)
qr-code-succeed? (reagent/atom false)
qr-view-finder (reagent/atom {})
torch? (reagent/atom false)
scan-code? (reagent/atom true)
set-rescan-timeout (fn []
(reset! scan-code? false)
(js/setTimeout #(reset! scan-code? true) 3000))]
(fn []
(let [torch-mode (if @torch? :on :off)
flashlight-icon (if @torch? :i/flashlight-on :i/flashlight-off)
show-camera? (and @camera-permission-granted?
(boolean (not-empty @qr-view-finder)))
camera-ready-to-scan? (and show-camera?
(not @qr-code-succeed?))]
(rn/use-effect
#(set-listener-torch-off-on-app-inactive torch?))
(rn/use-effect
(fn []
(when-not @camera-permission-granted?
(permissions/permission-granted? :camera
#(reset! camera-permission-granted? %)
#(reset! camera-permission-granted? false)))))
[:<>
[rn/view {:style style/background}]
(when camera-ready-to-scan?
[render-camera
{:torch-mode torch-mode
:qr-view-finder @qr-view-finder
:scan-code? @scan-code?
:error-message error-message
:validate-fn validate-fn
:set-qr-code-succeeded (fn [value]
(when on-success-scan
(on-success-scan value))
(rf/dispatch [:navigate-back]))
:set-rescan-timeout set-rescan-timeout}])
[rn/view {:style (style/root-container (:top insets))}
[header
{:title title
:subtitle subtitle}]
(when (empty? @qr-view-finder)
[:<>
[rn/view {:style style/scan-qr-code-container}]
[qr-scan-hole-area qr-view-finder]])
[scan-qr-code-tab @qr-view-finder]
[rn/view {:style style/flex-spacer}]
(when show-camera?
[quo.theme/provider {:theme :light}
[quo/button
{:icon-only? true
:type :grey
:background :photo
:size style/flash-button-size
:accessibility-label :camera-flash
:container-style (style/camera-flash-button @qr-view-finder)
:on-press #(swap! torch? not)}
flashlight-icon]])]]))))
(defn view-internal
[props]
[:f> f-view-internal props])
(def view (quo.theme/with-theme view-internal))