-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpendingAdvancedScreen.kt
More file actions
303 lines (278 loc) · 10.7 KB
/
SpendingAdvancedScreen.kt
File metadata and controls
303 lines (278 loc) · 10.7 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
package to.bitkit.ui.screens.transfer
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.material3.HorizontalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Devices.NEXUS_5
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import to.bitkit.R
import to.bitkit.ext.mockOrder
import to.bitkit.models.Toast
import to.bitkit.repositories.CurrencyState
import to.bitkit.ui.LocalCurrencies
import to.bitkit.ui.appViewModel
import to.bitkit.ui.components.Caption13Up
import to.bitkit.ui.components.Display
import to.bitkit.ui.components.FillHeight
import to.bitkit.ui.components.HorizontalSpacer
import to.bitkit.ui.components.MoneySSB
import to.bitkit.ui.components.NumberPad
import to.bitkit.ui.components.NumberPadActionButton
import to.bitkit.ui.components.NumberPadTextField
import to.bitkit.ui.components.PrimaryButton
import to.bitkit.ui.components.VerticalSpacer
import to.bitkit.ui.scaffold.AppTopBar
import to.bitkit.ui.scaffold.DrawerNavIcon
import to.bitkit.ui.scaffold.ScreenColumn
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.utils.withAccent
import to.bitkit.viewmodels.AmountInputEffect
import to.bitkit.viewmodels.AmountInputViewModel
import to.bitkit.viewmodels.TransferEffect
import to.bitkit.viewmodels.TransferToSpendingUiState
import to.bitkit.viewmodels.TransferValues
import to.bitkit.viewmodels.TransferViewModel
import to.bitkit.viewmodels.previewAmountInputViewModel
@Suppress("ViewModelForwarding")
@Composable
fun SpendingAdvancedScreen(
viewModel: TransferViewModel,
onBackClick: () -> Unit = {},
onOrderCreated: () -> Unit = {},
currencies: CurrencyState = LocalCurrencies.current,
amountInputViewModel: AmountInputViewModel = hiltViewModel(),
) {
val currentOnOrderCreated by rememberUpdatedState(onOrderCreated)
val app = appViewModel ?: return
val context = LocalContext.current
val state by viewModel.spendingUiState.collectAsStateWithLifecycle()
val order = state.order ?: return
val amountUiState by amountInputViewModel.uiState.collectAsStateWithLifecycle()
var isLoading by remember { mutableStateOf(false) }
val transferValues by viewModel.transferValues.collectAsStateWithLifecycle()
LaunchedEffect(order.clientBalanceSat) {
viewModel.updateTransferValues(order.clientBalanceSat)
}
LaunchedEffect(amountUiState.sats) {
viewModel.onReceivingAmountChange(amountUiState.sats)
}
LaunchedEffect(transferValues.maxLspBalance) {
amountInputViewModel.setMaxAmount(transferValues.maxLspBalance.toLong())
}
LaunchedEffect(Unit) {
viewModel.transferEffects.collect { effect ->
when (effect) {
TransferEffect.OnOrderCreated -> currentOnOrderCreated()
is TransferEffect.ToastException -> {
isLoading = false
app.toast(effect.e)
}
is TransferEffect.ToastError -> {
isLoading = false
app.toast(
type = Toast.ToastType.ERROR,
title = effect.title,
description = effect.description,
)
}
}
}
}
LaunchedEffect(amountInputViewModel) {
amountInputViewModel.effect.collect {
when (it) {
AmountInputEffect.MaxExceeded -> app.toast(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.lightning__spending_advanced__error_max__title),
description = context.getString(R.string.lightning__spending_advanced__error_max__description)
.replace("{amount}", "${transferValues.maxLspBalance}"),
visibilityTime = Toast.VISIBILITY_TIME_SHORT,
)
}
}
}
val isValid = transferValues.let {
val amount = amountUiState.sats.toULong()
amount > 0u && it.maxLspBalance > 0u && amount in it.minLspBalance..it.maxLspBalance
}
Content(
uiState = state,
transferValues = transferValues,
isValid = isValid,
isLoading = isLoading,
amountInputViewModel = amountInputViewModel,
currencies = currencies,
onBack = onBackClick,
onContinue = {
isLoading = true
viewModel.onSpendingAdvancedContinue(amountUiState.sats)
},
)
}
@Suppress("ViewModelForwarding")
@Composable
private fun Content(
uiState: TransferToSpendingUiState,
transferValues: TransferValues,
isValid: Boolean,
isLoading: Boolean,
amountInputViewModel: AmountInputViewModel,
onBack: () -> Unit,
onContinue: () -> Unit,
currencies: CurrencyState = LocalCurrencies.current,
) {
ScreenColumn {
AppTopBar(
titleText = stringResource(R.string.lightning__transfer__nav_title),
onBackClick = onBack,
actions = { DrawerNavIcon() },
)
Column(
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxSize()
.testTag("SpendingAdvanced")
) {
VerticalSpacer(minHeight = 16.dp, maxHeight = 32.dp)
Display(
text = stringResource(R.string.lightning__spending_advanced__title)
.withAccent(accentColor = Colors.Purple)
)
FillHeight()
NumberPadTextField(
viewModel = amountInputViewModel,
currencies = currencies,
showSecondaryField = false,
modifier = Modifier
.fillMaxWidth()
.testTag("SpendingAdvancedNumberField")
)
VerticalSpacer(16.dp)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.requiredHeight(20.dp),
) {
Caption13Up(
text = stringResource(R.string.lightning__spending_advanced__fee),
color = Colors.White64,
)
HorizontalSpacer(8.dp)
uiState.feeEstimate?.let {
MoneySSB(it, showSymbol = true)
} ?: run {
Caption13Up(text = "—", color = Colors.White64)
}
}
FillHeight()
Row(
verticalAlignment = Alignment.Bottom,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
) {
NumberPadActionButton(
text = stringResource(R.string.common__min),
color = Colors.Purple,
onClick = { amountInputViewModel.setSats(transferValues.minLspBalance.toLong(), currencies) },
modifier = Modifier.testTag("SpendingAdvancedMin")
)
NumberPadActionButton(
text = stringResource(R.string.common__default),
color = Colors.Purple,
onClick = { amountInputViewModel.setSats(transferValues.defaultLspBalance.toLong(), currencies) },
modifier = Modifier.testTag("SpendingAdvancedDefault")
)
NumberPadActionButton(
text = stringResource(R.string.common__max),
color = Colors.Purple,
onClick = { amountInputViewModel.setSats(transferValues.maxLspBalance.toLong(), currencies) },
modifier = Modifier.testTag("SpendingAdvancedMax")
)
}
HorizontalDivider()
VerticalSpacer(16.dp)
NumberPad(
viewModel = amountInputViewModel,
currencies = currencies,
)
PrimaryButton(
text = stringResource(R.string.common__continue),
onClick = onContinue,
enabled = !isLoading && isValid,
isLoading = isLoading,
modifier = Modifier.testTag("SpendingAdvancedContinue")
)
VerticalSpacer(16.dp)
}
}
}
@Preview(showSystemUi = true)
@Preview(showSystemUi = true, device = "id:pixel_9_pro_xl", name = "Large")
@Preview(showSystemUi = true, device = NEXUS_5, name = "Small")
@Composable
private fun Preview() {
AppThemeSurface {
Content(
uiState = TransferToSpendingUiState(
order = mockOrder().copy(clientBalanceSat = 100_000u),
receivingAmount = 55_000L,
feeEstimate = 2_500L,
),
transferValues = TransferValues(
defaultLspBalance = 50_000u,
minLspBalance = 10_000u,
maxLspBalance = 90_000u,
),
isValid = true,
amountInputViewModel = previewAmountInputViewModel(),
isLoading = false,
onBack = {},
onContinue = {},
)
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewLoading() {
AppThemeSurface {
Content(
uiState = TransferToSpendingUiState(
order = mockOrder().copy(clientBalanceSat = 50_000u),
receivingAmount = 20_000L,
feeEstimate = null,
isLoading = true,
),
transferValues = TransferValues(
defaultLspBalance = 25_000u,
minLspBalance = 10_000u,
maxLspBalance = 40_000u,
),
isValid = true,
amountInputViewModel = previewAmountInputViewModel(),
isLoading = true,
onBack = {},
onContinue = {},
)
}
}