/
TxPinpadScreen.kt
483 lines (424 loc) · 18.6 KB
/
TxPinpadScreen.kt
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package gg.interstellar.wallet.android.ui
import android.app.Activity
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Clear
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import gg.interstellar.wallet.WGPUSurfaceView
import gg.interstellar.wallet.android.R
import gg.interstellar.wallet.android.ui.components.HeaderWithBrand
import gg.interstellar.wallet.android.ui.theme.InterstellarWalletTheme
import androidx.compose.material.Icon as MaterialIcon
// TODO TxPinpadScreen SHOULD be moved into shared/
/*
@Preview(name = "NEXUS_7", device = Devices.NEXUS_7)
@Preview(name = "NEXUS_7_2013", device = Devices.NEXUS_7_2013)
@Preview(name = "NEXUS_5", device = Devices.NEXUS_5)
@Preview(name = "NEXUS_6", device = Devices.NEXUS_6)
@Preview(name = "NEXUS_9", device = Devices.NEXUS_9)
@Preview(name = "NEXUS_10", device = Devices.NEXUS_10)
@Preview(name = "NEXUS_5X", device = Devices.NEXUS_5X)
@Preview(name = "NEXUS_6P", device = Devices.NEXUS_6P)
@Preview(name = "PIXEL_C", device = Devices.PIXEL_C)
@Preview(name = "PIXEL", device = Devices.PIXEL)
@Preview(name = "PIXEL_XL", device = Devices.PIXEL_XL)
@Preview(name = "PIXEL_2", device = Devices.PIXEL_2)
@Preview(name = "PIXEL_2_XL", device = Devices.PIXEL_2_XL)
@Preview(name = "PIXEL_3", device = Devices.PIXEL_3)
@Preview(name = "PIXEL_3_XL", device = Devices.PIXEL_3_XL)
@Preview(name = "PIXEL_3A", device = Devices.PIXEL_3A)
@Preview(name = "PIXEL_3A_XL", device = Devices.PIXEL_3A_XL)
@Preview(name = "PIXEL_4", device = Devices.PIXEL_4)
@Preview(name = "PIXEL_4_XL", device = Devices.PIXEL_4_XL)
@Preview(name = "AUTOMOTIVE_1024p", device = Devices.AUTOMOTIVE_1024p)
*/
@Preview(showBackground = true)
@Composable
fun TxPinpadScreen(callbackTxDone: () -> Unit) {
// Will store the computed WGPUSurfaceView's coordinates
// updated by AndroidView onGloballyPositioned
// It is needed to compute the BBox for pinpadRectsRelativeToWGPUSurfaceView/messageRectRelativeToWGPUSurfaceView relative
// to their parent WGPUSurfaceView
// MUST use a ref that is "passed around" the whole TxPinpadScreen
// using eg "LayoutCoordinates? = null" means that even if AndroidView.onGloballyPositioned is indeed called
// BEFORE SetPadCircle onGloballyPositioned, we get a NPE there...
// TODO we could probably remove it and look up using parent.parent... recursively until we find the WGPUSurfaceView
// (note that WGPUSurfaceView and Spacer/SetPadCircle are siblings!)
val wgpu_sv_pinpad_coordinates: Array<LayoutCoordinates?> = arrayOf(null)
// Array to store the computed coordinates of each "pinpad circle"
// updated by SetPadCircle onGloballyPositioned
// TODO? or a State? https://developer.android.com/jetpack/compose/state
//var sizeTopBar by remember { mutableStateOf(IntSize.Zero) }
//var positionInRootTopBar by remember { mutableStateOf(Offset.Zero) }
val pinpadRectsRelativeToWGPUSurfaceView: Array<Rect> = Array(12) { Rect.Zero }
// Same idea but for the message
// NOTE: contrary to pinpad we only have ONE Rect, not an array
// Array b/c same issue than "wgpu_sv_pinpad_coordinates"
val messageRectRelativeToWGPUSurfaceView: Array<Rect> = Array(1) { Rect.Zero }
// Array b/c same issue than "wgpu_sv_pinpad_coordinates"
val wgpuSurfaceView: Array<WGPUSurfaceView?> = arrayOf(null)
val activity = LocalContext.current as Activity
InterstellarWalletTheme {
Column {
HeaderWithBrand()
// we need to store a ref to colors b/c AndroidView below is not a Composable
val colors = MaterialTheme.colors
Box {
// We have a "native screen" in the background which consists of the "message screen" at the top
// and the "pinpad screen" at the bottom
//
// Only load the Rust wrapper if NOT in Preview
// else: java.lang.UnsatisfiedLinkError: no shared_substrate_client in java.library.path
if (!LocalInspectionMode.current) {
AndroidView(
factory = { ctx ->
wgpuSurfaceView[0] = WGPUSurfaceView(
context = ctx,
pinpadRectsRelativeToWGPUSurfaceView,
messageRectRelativeToWGPUSurfaceView,
colors
) {
Log.i("interstellar", "callbackTxDone[1]")
// TODO fix callbackTxDone!
// For some reasons we need to "bounce" using another callback else we get a weird NPE
// Directly adding "callbackTxDone = { navController.navigate(WalletScreen.Portfolio.name) }" param
// causes a NPE at "onClick = { wgpuSurfaceView[0]!!.onClickPinpadDigit(id) }"???
// TODO callbackTxDone()
activity.finish()
// recreate(activity) // works, but it restart on this screen which is NOT what we want!
}
wgpuSurfaceView[0]!!
},
modifier = Modifier.onGloballyPositioned { coordinates ->
wgpu_sv_pinpad_coordinates[0] = coordinates
}
)
}
// And on top of the Rust "native screen" we draw the UI(ie the slider for the confirmation)
Column {
// First space: above the "message"
// Then the "message" itself; it will be drawn over(or rather below) by the Rust renderer
// Finally another spacer
// IMPORTANT: all the heights MUST match the expected height(and aspect ratio) of the renderer
Spacer(
modifier = Modifier
.fillMaxHeight(0.25f * 0.1f)
.fillMaxWidth()
)
// TODO add onGloballyPositioned, and same as PinpadBottomScreen
Spacer(
modifier = Modifier
// Height: previously it was "full width * 16/9" but with 10% padding top and bottom
// Now the padding is removed hence 0.8 factor
.fillMaxHeight(0.25f * 0.8f)
// IMPORTANT: without "fillMaxWidth" in onGloballyPositioned: localBoundingBoxOf will return Rect.Zero
// cf /lib_circuits/circuit-gen-rs/circuit_crop.odg for why 0.1875
// long story short: 1.2/6.4 = 0.1875
.fillMaxWidth(1.0f - 0.1875f * 2.0f)
.align(Alignment.CenterHorizontally)
.onGloballyPositioned { coordinates ->
// TODO or keep fillMaxHeight / fillMaxWidth b/c that defines the positions of the rest
// and update `coordinates` here? (in this case: remove both spacers?)
// TODO? positionInWindow,boundsInParent,boundsInWindow,boundsInRoot,size?
// NOTE: use wgpu_sv_pinpad_coordinates?. to make sure the Preview does not get an NPE
messageRectRelativeToWGPUSurfaceView[0] =
wgpu_sv_pinpad_coordinates[0]?.localBoundingBoxOf(coordinates)!!
})
Spacer(
modifier = Modifier
.fillMaxHeight(0.25f * 0.1f)
.fillMaxWidth()
)
ConfirmMessageMiddleScreen()
PinpadBottomScreen(
pinpadRectsRelativeToWGPUSurfaceView,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
}
}
}
}
}
@Composable
private fun ConfirmMessageMiddleScreen() {
Row(
horizontalArrangement = Arrangement.Center, modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(0.2f)
)
{
Box(
modifier = Modifier
.shadow(elevation = 50.dp, shape = CircleShape, clip = false)
) {
Surface(
modifier = Modifier
.sizeIn(200.dp, 50.dp, 300.dp, 80.dp)
.padding(15.dp),
shape = CircleShape,
) {
Box(
modifier = Modifier
//TODO optimize gradient
.background(
Brush.linearGradient(
0.3f to MaterialTheme.colors.secondary,
1f to MaterialTheme.colors.primary,
start = Offset(0f, 0f),
end = Offset(280f, 280f)
)
)
)
{
Row(
modifier = Modifier
.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically
) {
Surface(
modifier = Modifier
.sizeIn(200.dp, 99.dp, 300.dp, 90.dp)
.padding(horizontal = 6.dp)
.padding(vertical = 6.dp),
color = MaterialTheme.colors.secondaryVariant,
shape = CircleShape,
) {
Text(
"Confirm Transaction",
textAlign = TextAlign.Center,
fontSize = 16.sp,
modifier = Modifier
.wrapContentHeight(Alignment.CenterVertically)
)
}
Text(
stringResource(R.string.three_point_redacted),
textAlign = TextAlign.Center,
fontSize = 8.sp,
color = if (MaterialTheme.colors.isLight) Color.White
else Color.Black,
modifier = Modifier
)
Spacer(Modifier.width(7.dp))
Surface(
modifier = Modifier
.sizeIn(30.dp, 30.dp, 40.dp, 40.dp)
.aspectRatio(1f),
color = MaterialTheme.colors.secondaryVariant,
shape = CircleShape,
) {
MaterialIcon(
Icons.Filled.Check,
modifier = Modifier
.padding(3.dp),
contentDescription = "check icon",
)
}
}
}
}
}
}
}
@Composable
private fun PinpadBottomScreen(
arrayRectFinalCoords: Array<Rect>,
wgpu_sv_pinpad_coordinates: Array<LayoutCoordinates?>,
wgpuSurfaceView: Array<WGPUSurfaceView?>
) {
// We MUST set "weight" on each children, that weight each row will have the same height
Box {
// Same level as the Column, so it will be drawn ON TOP of it
// TODO? NOTE: order matters! also TODO? setZOrderOnTop?
Column()
{
Spacer(Modifier.weight(0.5f))
StandardPinpadRow(
Modifier.weight(4f),
0,
1,
2,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(1f))
StandardPinpadRow(
Modifier.weight(4f),
3,
4,
5,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(1f))
StandardPinpadRow(
Modifier.weight(4f),
6,
7,
8,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(1f))
StandardPinpadRow(
Modifier.weight(4f),
9,
10,
11,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(2f))
}
}
}
/**
* Standard Pinpad row, with 3 circles
*/
@Composable
private fun ColumnScope.StandardPinpadRow(
modifier: Modifier,
id_left: Int,
id_middle: Int,
id_right: Int,
arrayRectFinalCoords: Array<Rect>,
wgpu_sv_pinpad_coordinates: Array<LayoutCoordinates?>,
wgpuSurfaceView: Array<WGPUSurfaceView?>
) {
Row(
horizontalArrangement = Arrangement.Center,
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 10.dp, vertical = 10.dp),
) {
Spacer(Modifier.weight(4f))
SetPadCircle(
Modifier.weight(4f),
id_left,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(1f))
SetPadCircle(
Modifier.weight(4f),
id_middle,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(1f))
SetPadCircle(
Modifier.weight(4f),
id_right,
arrayRectFinalCoords,
wgpu_sv_pinpad_coordinates,
wgpuSurfaceView
)
Spacer(Modifier.weight(4f))
}
}
/**
* @param forceDraw: usually in PROD we DO NOT draw the Circle in Java/Kotlin; all is done in Rust(OpenGL/Vulkan)
* But it can be useful to forceDraw the UI circles for layout purposes.
* NOTE: the POSITIONS of the circles is calculated by Compose, and then passed to Rust.
* If false(=default): it will draw them in PREVIEW mode, but not in the app.
*/
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SetPadCircle(
modifier: Modifier,
id: Int,
arrayRectFinalCoords: Array<Rect>,
wgpu_sv_pinpad_coordinates: Array<LayoutCoordinates?>,
wgpuSurfaceView: Array<WGPUSurfaceView?>
) {
fun isClearButton(): Boolean {
return id == 11
}
// IMPORTANT make the component invisible(still above the Rust OpenGL)
// (but still draw in Preview mode for easier dev/debug)
// TODO ideally it would be better to have the button GONE when not in preview, to avoid compositing
// but for now it does not seem to be possible
// Tried to replace the Surface with a Space when not in preview, but onGloballyPositioned returned different values
val alpha = if (LocalInspectionMode.current) {
1.0f
} else {
// when NOT in preview: we SHOULD draw CANCEL button
// [they are NOT drawn by the Rust side]
// TODO GO button? id != 9
if (!isClearButton()) {
0.0f
} else {
1.0f
}
}
// Avoid nesting, etc; keep it simple
Surface(
modifier = modifier
.aspectRatio(1f) // needed else "squished"(ie ellipses not circles)
.alpha(alpha)
.onGloballyPositioned { coordinates ->
// TODO? positionInWindow,boundsInParent,boundsInWindow,boundsInRoot,size?
arrayRectFinalCoords[id] =
wgpu_sv_pinpad_coordinates[0]!!.localBoundingBoxOf(coordinates)
}
.scale(
if (!isClearButton()) {
1.0f
} else {
0.33f
}
),
// TODO?: .shadow(elevation = 35.dp, /* shape = CircleShape */) are NOT supported on Rust side, so no point in having it enabled
shape = if (!isClearButton()) {
CircleShape
} else {
RoundedCornerShape(20)
},
elevation = 35.dp,
color = MaterialTheme.colors.surface,
onClick = { wgpuSurfaceView[0]!!.onClickPinpadDigit(id) }
) {
if (isClearButton()) {
MaterialIcon(
Icons.Filled.Clear,
contentDescription = "clear",
)
}
// TODO ok button if id == 11? But not really needed b/c we know how many inputs we want?
}
}