-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAmountInputViewModel.kt
More file actions
444 lines (393 loc) · 16.1 KB
/
AmountInputViewModel.kt
File metadata and controls
444 lines (393 loc) · 16.1 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
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
package to.bitkit.viewmodels
import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import to.bitkit.ext.toLongOrDefault
import to.bitkit.models.CLASSIC_DECIMALS
import to.bitkit.models.FIAT_DECIMALS
import to.bitkit.models.PrimaryDisplay
import to.bitkit.models.SATS_GROUPING_SEPARATOR
import to.bitkit.models.SATS_IN_BTC
import to.bitkit.models.formatToClassicDisplay
import to.bitkit.models.formatToModernDisplay
import to.bitkit.repositories.AmountInputHandler
import to.bitkit.repositories.CurrencyState
import to.bitkit.ui.LocalCurrencies
import to.bitkit.ui.components.KEY_DECIMAL
import to.bitkit.ui.components.KEY_DELETE
import to.bitkit.ui.components.NumberPadType
import java.math.BigDecimal
import java.text.NumberFormat
import java.util.Locale
import javax.inject.Inject
@Suppress("TooManyFunctions")
@HiltViewModel
class AmountInputViewModel @Inject constructor(
private val amountInputHandler: AmountInputHandler,
) : ViewModel() {
companion object {
const val MAX_AMOUNT = 999_999_999L
const val MAX_MODERN_LENGTH = 10
const val MAX_DECIMAL_LENGTH = 20
const val ERROR_DELAY_MS = 500L
const val PLACEHOLDER_CLASSIC = "0.00000000"
const val PLACEHOLDER_MODERN = "0"
const val PLACEHOLDER_FIAT = "0.00"
const val PLACEHOLDER_CLASSIC_DECIMALS = ".00000000"
const val PLACEHOLDER_MODERN_DECIMALS = ""
const val PLACEHOLDER_FIAT_DECIMALS = ".00"
}
private val _uiState = MutableStateFlow(AmountInputUiState())
val uiState: StateFlow<AmountInputUiState> = _uiState.asStateFlow()
private val _effect = MutableSharedFlow<AmountInputEffect>(extraBufferCapacity = 1)
val effect: SharedFlow<AmountInputEffect> = _effect.asSharedFlow()
private var maxAmount: Long = MAX_AMOUNT
private var rawInputText: String = ""
fun setMaxAmount(amount: Long) {
maxAmount = amount.coerceIn(0, MAX_AMOUNT)
}
fun handleNumberPadInput(
key: String,
currencyState: CurrencyState,
) {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
val maxLength = getMaxLength(currencyState)
val maxDecimals = getMaxDecimals(currencyState)
val newText = handleInput(key = key, current = rawInputText, maxLength, maxDecimals)
if (newText == rawInputText && key != KEY_DELETE) {
triggerErrorState(key)
return
}
// For modern Bitcoin (integer input), format the final amount
if (primaryDisplay == PrimaryDisplay.BITCOIN && isModern) {
val newAmount = convertToSats(newText, primaryDisplay, isModern = true)
if (newAmount <= maxAmount) {
rawInputText = newText
_uiState.update {
it.copy(
text = formatDisplayTextFromAmount(newAmount, primaryDisplay, isModern = true),
sats = newAmount,
errorKey = null
)
}
} else {
emitMaxExceeded()
triggerErrorState(key)
}
} else {
// For decimal input, check limits before updating state
if (newText.isNotEmpty()) {
val newAmount = convertToSats(newText, primaryDisplay, isModern)
if (newAmount <= maxAmount) {
// Update both raw input and display text
rawInputText = newText
_uiState.update {
it.copy(
text = if (primaryDisplay == PrimaryDisplay.FIAT) {
formatFiatGroupingOnly(newText)
} else {
newText
},
sats = newAmount,
errorKey = null
)
}
} else {
emitMaxExceeded()
triggerErrorState(key)
}
} else {
// If input is empty, set sats to 0
rawInputText = newText
_uiState.update {
it.copy(
sats = 0,
text = "",
errorKey = null
)
}
}
}
}
fun setSats(sats: Long, currencyState: CurrencyState) {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
_uiState.update {
it.copy(
sats = sats,
text = formatDisplayTextFromAmount(sats, primaryDisplay, isModern)
)
}
// Update raw input text based on the formatted display
rawInputText = when (primaryDisplay) {
PrimaryDisplay.FIAT -> _uiState.value.text.replace(",", "")
else -> _uiState.value.text
}
}
/**
* Toggles between Bitcoin and Fiat display modes while preserving input
*/
fun switchUnit(currencies: CurrencyState) {
viewModelScope.launch {
val currentRawInput = rawInputText
val isModern = currencies.displayUnit.isModern()
val newPrimaryDisplay = amountInputHandler.switchUnit(currencies.primaryDisplay)
// Update display text when currency changes
val amountSats = _uiState.value.sats
if (amountSats > 0) {
_uiState.update {
it.copy(
text = formatDisplayTextFromAmount(amountSats, newPrimaryDisplay, isModern)
)
}
// Update raw input text based on the new display
rawInputText = when (newPrimaryDisplay) {
PrimaryDisplay.FIAT -> _uiState.value.text.replace(",", "")
else -> _uiState.value.text
}
} else if (currentRawInput.isNotEmpty()) {
// Convert the raw input from the old currency to the new currency
when (newPrimaryDisplay) {
PrimaryDisplay.FIAT -> {
// Converting from bitcoin to fiat
val sats = convertBitcoinToSats(currentRawInput, isModern)
val converted = amountInputHandler.convertSatsToFiatString(sats)
if (converted.isNotEmpty()) {
rawInputText = converted.replace(",", "")
_uiState.update { it.copy(text = formatFiatGroupingOnly(rawInputText)) }
}
}
PrimaryDisplay.BITCOIN -> {
// Converting from fiat to bitcoin
val sats = convertFiatToSats(currentRawInput)
if (sats != null) {
rawInputText = formatBitcoinFromSats(sats, isModern)
_uiState.update { it.copy(text = rawInputText) }
}
}
}
}
}
}
fun getNumberPadType(currencyState: CurrencyState): NumberPadType {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
val isBtc = primaryDisplay == PrimaryDisplay.BITCOIN
return if (isModern && isBtc) NumberPadType.INTEGER else NumberPadType.DECIMAL
}
fun getMaxLength(currencyState: CurrencyState): Int {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
val isBtc = primaryDisplay == PrimaryDisplay.BITCOIN
return if (isModern && isBtc) MAX_MODERN_LENGTH else MAX_DECIMAL_LENGTH
}
fun getMaxDecimals(currencyState: CurrencyState): Int {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
val isBtc = primaryDisplay == PrimaryDisplay.BITCOIN
return if (isModern && isBtc) 0 else (if (isBtc) CLASSIC_DECIMALS else FIAT_DECIMALS)
}
@Suppress("NestedBlockDepth")
fun getPlaceholder(currencyState: CurrencyState): String {
val primaryDisplay = currencyState.primaryDisplay
val isModern = currencyState.displayUnit.isModern()
if (_uiState.value.text.isEmpty()) {
return when (primaryDisplay) {
PrimaryDisplay.BITCOIN -> if (isModern) PLACEHOLDER_MODERN else PLACEHOLDER_CLASSIC
PrimaryDisplay.FIAT -> PLACEHOLDER_FIAT
}
} else {
return when (primaryDisplay) {
PrimaryDisplay.BITCOIN -> {
if (isModern) {
PLACEHOLDER_MODERN_DECIMALS
} else {
if (_uiState.value.text.contains(".")) {
val parts = _uiState.value.text.split(".", limit = 2)
val decimalPart = if (parts.size > 1) parts[1] else ""
val remainingDecimals = CLASSIC_DECIMALS - decimalPart.length
if (remainingDecimals > 0) "0".repeat(remainingDecimals) else ""
} else {
PLACEHOLDER_CLASSIC_DECIMALS
}
}
}
PrimaryDisplay.FIAT -> {
if (_uiState.value.text.contains(".")) {
val parts = _uiState.value.text.split(".", limit = 2)
val decimalPart = if (parts.size > 1) parts[1] else ""
val remainingDecimals = FIAT_DECIMALS - decimalPart.length
if (remainingDecimals > 0) "0".repeat(remainingDecimals) else ""
} else {
PLACEHOLDER_FIAT_DECIMALS
}
}
}
}
}
fun clearInput() {
rawInputText = ""
maxAmount = MAX_AMOUNT
_uiState.update { AmountInputUiState() }
}
private fun emitMaxExceeded() {
if (maxAmount < MAX_AMOUNT) {
_effect.tryEmit(AmountInputEffect.MaxExceeded)
}
}
private fun triggerErrorState(key: String) {
_uiState.update { it.copy(errorKey = key) }
viewModelScope.launch {
delay(ERROR_DELAY_MS)
_uiState.update { it.copy(errorKey = null) }
}
}
private fun formatDisplayTextFromAmount(
amountSats: Long,
primaryDisplay: PrimaryDisplay,
isModern: Boolean,
): String {
if (amountSats == 0L) return ""
return when (primaryDisplay) {
PrimaryDisplay.BITCOIN -> formatBitcoinFromSats(amountSats, isModern)
PrimaryDisplay.FIAT -> amountInputHandler.convertSatsToFiatString(amountSats)
}
}
@Suppress("ReturnCount")
private fun formatFiatGroupingOnly(text: String): String {
// Remove any existing grouping separators for parsing
val cleanText = text.replace(",", "")
// If the text ends with a decimal point, don't format it (preserve the decimal point)
if (text.endsWith(".")) {
// Only add grouping separators to the integer part
val integerPart = cleanText.dropLast(1) // Remove the decimal point
integerPart.toIntOrNull()?.let { intValue ->
val formatter = NumberFormat.getNumberInstance(Locale.US)
return formatter.format(intValue) + "."
}
return text
}
// If the text contains a decimal point, preserve the decimal structure
if (text.contains(".")) {
val parts = cleanText.split(".", limit = 2)
val integerPart = parts[0]
val decimalPart = if (parts.size > 1) parts[1] else ""
// Format only the integer part with grouping separators
integerPart.toIntOrNull()?.let { intValue ->
val formatter = NumberFormat.getNumberInstance(Locale.US)
return formatter.format(intValue) + "." + decimalPart
}
return text
}
// For integer-only input, add grouping separators
cleanText.toIntOrNull()?.let { intValue ->
val formatter = NumberFormat.getNumberInstance(Locale.US)
return formatter.format(intValue)
}
return text
}
private fun formatBitcoinFromSats(sats: Long, isModern: Boolean): String {
return if (isModern) sats.formatToModernDisplay() else sats.formatToClassicDisplay()
}
private fun convertToSats(
text: String,
primaryDisplay: PrimaryDisplay,
isModern: Boolean,
): Long {
if (text.isEmpty()) return 0L
return when (primaryDisplay) {
PrimaryDisplay.BITCOIN -> convertBitcoinToSats(text, isModern)
PrimaryDisplay.FIAT -> convertFiatToSats(text) ?: 0
}
}
private fun convertBitcoinToSats(text: String, isModern: Boolean): Long {
if (text.isEmpty()) return 0
return if (isModern) {
text.replace("$SATS_GROUPING_SEPARATOR", "").toLongOrDefault()
} else {
runCatching {
val btcBigDecimal = BigDecimal(text)
val satsBigDecimal = btcBigDecimal.multiply(BigDecimal(SATS_IN_BTC))
satsBigDecimal.toLong()
}.getOrDefault(0)
}
}
private fun convertFiatToSats(text: String): Long? {
return text.replace(",", "")
.toDoubleOrNull()
?.let { fiat -> amountInputHandler.convertFiatToSats(fiat) }
}
private fun handleInput(
key: String,
current: String,
maxLength: Int,
maxDecimals: Int,
): String {
return if (maxDecimals == 0) {
handleIntegerInput(key, current, maxLength)
} else {
handleDecimalInput(key, current, maxLength, maxDecimals)
}
}
private fun handleIntegerInput(key: String, current: String, maxLength: Int): String {
if (key == KEY_DELETE) return current.dropLast(1)
if (current == "0") return key
if (current.length >= maxLength) return current
return current + key
}
@Suppress("ReturnCount")
private fun handleDecimalInput(
key: String,
current: String,
maxLength: Int,
maxDecimals: Int,
): String {
val parts = current.split(".", limit = 2)
val decimalPart = if (parts.size > 1) parts[1] else ""
if (key == KEY_DELETE) {
if (current == "0.") return ""
return current.dropLast(1)
}
// Handle leading zeros - replace "0" with new digit but allow "0."
if (current == "0" && key != ".") return key
// Limit to maxLength
if (current.length >= maxLength) return current
// Limit decimal places
if (decimalPart.length >= maxDecimals) return current
if (key == KEY_DECIMAL) {
// No multiple decimal symbols
if (current.contains(".")) return current
// Add leading zero
if (current.isEmpty()) return "0."
}
return current + key
}
}
data class AmountInputUiState(
val sats: Long = 0L,
val text: String = "",
val errorKey: String? = null,
)
sealed interface AmountInputEffect {
data object MaxExceeded : AmountInputEffect
}
@SuppressLint("ViewModelConstructorInComposable")
@Composable
fun previewAmountInputViewModel(
sats: Long = 4_567,
currencies: CurrencyState = LocalCurrencies.current,
) = AmountInputViewModel(AmountInputHandler.stub()).also {
it.setSats(sats, currencies)
}