-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHardwareWalletScreen.kt
More file actions
336 lines (316 loc) · 11.4 KB
/
Copy pathHardwareWalletScreen.kt
File metadata and controls
336 lines (316 loc) · 11.4 KB
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
package to.bitkit.ui.screens.wallets
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.synonym.bitkitcore.Activity
import dev.chrisbanes.haze.hazeEffect
import dev.chrisbanes.haze.hazeSource
import dev.chrisbanes.haze.rememberHazeState
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableSet
import to.bitkit.R
import to.bitkit.ext.rawId
import to.bitkit.models.HwWallet
import to.bitkit.models.TransportType
import to.bitkit.ui.components.BalanceHeaderView
import to.bitkit.ui.components.SecondaryButton
import to.bitkit.ui.components.StatusBarSpacer
import to.bitkit.ui.components.TabBar
import to.bitkit.ui.components.TertiaryButton
import to.bitkit.ui.components.TopBarSpacer
import to.bitkit.ui.components.VerticalSpacer
import to.bitkit.ui.scaffold.AppAlertDialog
import to.bitkit.ui.scaffold.AppTopBar
import to.bitkit.ui.scaffold.DrawerNavIcon
import to.bitkit.ui.screens.wallets.activity.components.activityListGroupedItems
import to.bitkit.ui.screens.wallets.activity.utils.previewOnchainActivityItems
import to.bitkit.ui.shared.util.blockPointerInputPassthrough
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.theme.TopBarGradient
@Composable
fun HardwareWalletScreen(
deviceId: String,
onActivityItemClick: (String) -> Unit,
onTransferToSpendingClick: () -> Unit,
onBackClick: () -> Unit,
viewModel: HwWalletViewModel = hiltViewModel(),
) {
val wallets by viewModel.wallets.collectAsStateWithLifecycle()
val walletsLoaded by viewModel.walletsLoaded.collectAsStateWithLifecycle()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val wallet = remember(wallets, deviceId) { wallets.find { deviceId in it.deviceIds } }
// Leave the screen once the device is gone, whether removed here or forgotten elsewhere.
LaunchedEffect(wallet, walletsLoaded) {
if (walletsLoaded && wallet == null) onBackClick()
}
wallet?.let { device ->
HardwareWalletContent(
wallet = device,
showRemoveDialog = uiState.isPendingRemoval != null,
onActivityItemClick = onActivityItemClick,
onTransferToSpendingClick = onTransferToSpendingClick,
onRemoveClick = { viewModel.onRemoveClick(device) },
onConfirmRemove = { viewModel.removeDevice(deviceId) },
onDismissRemoveDialog = viewModel::onDismissRemoveDialog,
onBackClick = onBackClick,
)
}
}
@Composable
private fun HardwareWalletContent(
wallet: HwWallet,
showRemoveDialog: Boolean,
onActivityItemClick: (String) -> Unit,
onTransferToSpendingClick: () -> Unit,
onRemoveClick: () -> Unit,
onConfirmRemove: () -> Unit,
onDismissRemoveDialog: () -> Unit,
onBackClick: () -> Unit,
) {
val hasFunds = wallet.balanceSats > 0uL
val hasActivity = wallet.activities.isNotEmpty()
val showEmptyState = !hasFunds && !hasActivity
// Every activity here belongs to the watch-only device, so render them all with the blue
// hardware icon, matching the home list.
val hardwareIds = remember(wallet.activities) { wallet.activities.map { it.rawId() }.toImmutableSet() }
val hazeState = rememberHazeState()
Box(
modifier = Modifier
.fillMaxSize()
.blockPointerInputPassthrough()
.testTag("HardwareWalletScreen")
) {
Box(
modifier = Modifier
.matchParentSize()
.background(Colors.Black)
.hazeSource(hazeState)
) {
Image(
painter = painterResource(id = R.drawable.trezor),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = Modifier
.align(Alignment.TopEnd)
.offset(x = 119.dp, y = 92.dp)
.size(256.dp)
)
}
AppTopBar(
titleText = wallet.name,
icon = R.drawable.ic_btc_circle_blue,
onBackClick = onBackClick,
actions = {
DrawerNavIcon()
},
modifier = Modifier
.hazeEffect(state = hazeState) {
mask = TopBarGradient
}
.background(TopBarGradient)
.zIndex(1f)
.windowInsetsPadding(WindowInsets.statusBars.only(WindowInsetsSides.Vertical))
)
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
) {
item { StatusBarSpacer() }
item { TopBarSpacer() }
item {
BalanceHeaderView(
sats = wallet.balanceSats.toLong(),
testTag = "TotalBalance",
modifier = Modifier.fillMaxWidth()
)
}
if (!showEmptyState) {
item { VerticalSpacer(32.dp) }
if (hasFunds) {
item {
SecondaryButton(
onClick = onTransferToSpendingClick,
text = stringResource(R.string.wallet__transfer_to_spending),
icon = {
Icon(
painter = painterResource(R.drawable.ic_transfer),
contentDescription = null,
modifier = Modifier.size(16.dp)
)
},
hazeState = hazeState,
modifier = Modifier.testTag("HwTransferToSpending")
)
}
}
activityListGroupedItems(
items = wallet.activities,
onActivityItemClick = onActivityItemClick,
onEmptyActivityRowClick = {},
showFooter = false,
hardwareIds = hardwareIds,
footerContent = {
RemoveHardwareWalletButton(
walletName = wallet.name,
onClick = onRemoveClick,
)
},
)
}
if (showEmptyState) {
item { VerticalSpacer(32.dp) }
item {
RemoveHardwareWalletButton(
walletName = wallet.name,
onClick = onRemoveClick,
)
}
item { VerticalSpacer(120.dp) }
}
}
if (showRemoveDialog) {
AppAlertDialog(
title = stringResource(R.string.hardware__remove_dialog_title, wallet.name),
text = stringResource(R.string.hardware__remove_dialog_text),
confirmText = stringResource(R.string.common__remove),
dismissText = stringResource(R.string.common__cancel),
onConfirm = onConfirmRemove,
onDismiss = onDismissRemoveDialog,
)
}
}
}
@Composable
private fun RemoveHardwareWalletButton(
walletName: String,
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
TertiaryButton(
text = stringResource(R.string.hardware__remove_button, walletName),
onClick = onClick,
modifier = modifier
.wrapContentWidth()
.padding(top = 8.dp)
.testTag("RemoveHardwareWallet")
)
}
private fun previewWallet(
balanceSats: ULong = 10_562_411uL,
activities: ImmutableList<Activity> = previewOnchainActivityItems(),
) = HwWallet(
id = "dev1",
name = "Trezor Safe 3",
model = "Safe 3",
transportType = TransportType.USB,
isConnected = true,
balanceSats = balanceSats,
activities = activities,
)
@Preview(showSystemUi = true)
@Composable
private fun Preview() {
AppThemeSurface {
Box {
HardwareWalletContent(
wallet = previewWallet(),
showRemoveDialog = false,
onActivityItemClick = {},
onTransferToSpendingClick = {},
onRemoveClick = {},
onConfirmRemove = {},
onDismissRemoveDialog = {},
onBackClick = {},
)
TabBar()
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewNoActivity() {
AppThemeSurface {
Box {
HardwareWalletContent(
wallet = previewWallet(activities = persistentListOf()),
showRemoveDialog = false,
onActivityItemClick = {},
onTransferToSpendingClick = {},
onRemoveClick = {},
onConfirmRemove = {},
onDismissRemoveDialog = {},
onBackClick = {},
)
TabBar()
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewEmpty() {
AppThemeSurface {
Box {
HardwareWalletContent(
wallet = previewWallet(balanceSats = 0uL, activities = persistentListOf()),
showRemoveDialog = false,
onActivityItemClick = {},
onTransferToSpendingClick = {},
onRemoveClick = {},
onConfirmRemove = {},
onDismissRemoveDialog = {},
onBackClick = {},
)
TabBar()
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewRemoveDialog() {
AppThemeSurface {
Box {
HardwareWalletContent(
wallet = previewWallet(),
showRemoveDialog = true,
onActivityItemClick = {},
onTransferToSpendingClick = {},
onRemoveClick = {},
onConfirmRemove = {},
onDismissRemoveDialog = {},
onBackClick = {},
)
TabBar()
}
}
}