Skip to content

Commit cfcb2e6

Browse files
authored
Merge pull request #1013 from synonymdev/fix/set-max-amount-transfer
fix: set transfer amount to max on cap error
2 parents 99935dc + e8599e1 commit cfcb2e6

5 files changed

Lines changed: 29 additions & 31 deletions

File tree

app/src/main/java/to/bitkit/ui/screens/transfer/SpendingAdvancedScreen.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fun SpendingAdvancedScreen(
7575

7676
val transferValues by viewModel.transferValues.collectAsStateWithLifecycle()
7777
val currentMaxLspBalance by rememberUpdatedState(transferValues.maxLspBalance)
78+
val currentCurrencies by rememberUpdatedState(currencies)
7879

7980
LaunchedEffect(order.clientBalanceSat) {
8081
viewModel.updateTransferValues(order.clientBalanceSat)
@@ -112,13 +113,16 @@ fun SpendingAdvancedScreen(
112113
LaunchedEffect(Unit) {
113114
amountInputViewModel.effect.collect {
114115
when (it) {
115-
AmountInputEffect.MaxExceeded -> app.toast(
116-
type = Toast.ToastType.WARNING,
117-
title = context.getString(R.string.lightning__spending_advanced__error_max__title),
118-
description = context.getString(R.string.lightning__spending_advanced__error_max__description)
119-
.replace("{amount}", currentMaxLspBalance.formatToModernDisplay()),
120-
visibilityTime = Toast.VISIBILITY_TIME_SHORT,
121-
)
116+
AmountInputEffect.MaxExceeded -> {
117+
amountInputViewModel.setSats(currentMaxLspBalance.toLong(), currentCurrencies)
118+
app.toast(
119+
type = Toast.ToastType.WARNING,
120+
title = context.getString(R.string.lightning__spending_advanced__error_max__title),
121+
description = context.getString(R.string.lightning__spending_advanced__error_max__description)
122+
.replace("{amount}", currentMaxLspBalance.formatToModernDisplay()),
123+
visibilityTime = Toast.VISIBILITY_TIME_SHORT,
124+
)
125+
}
122126
}
123127
}
124128
}

app/src/main/java/to/bitkit/ui/screens/transfer/SpendingAmountScreen.kt

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import to.bitkit.viewmodels.TransferEffect
5555
import to.bitkit.viewmodels.TransferToSpendingUiState
5656
import to.bitkit.viewmodels.TransferViewModel
5757
import to.bitkit.viewmodels.previewAmountInputViewModel
58-
import kotlin.math.min
5958

6059
@Suppress("ViewModelForwarding")
6160
@Composable
@@ -74,6 +73,7 @@ fun SpendingAmountScreen(
7473
val amountUiState by amountInputViewModel.uiState.collectAsStateWithLifecycle()
7574
val context = LocalContext.current
7675
val currentMaxAllowedToSend by rememberUpdatedState(uiState.maxAllowedToSend)
76+
val currentCurrencies by rememberUpdatedState(currencies)
7777

7878
LaunchedEffect(isOffline) {
7979
viewModel.updateLimits()
@@ -92,11 +92,14 @@ fun SpendingAmountScreen(
9292
LaunchedEffect(Unit) {
9393
amountInputViewModel.effect.collect {
9494
when (it) {
95-
AmountInputEffect.MaxExceeded -> toast(
96-
context.getString(R.string.lightning__spending_amount__error_max__title),
97-
context.getString(R.string.lightning__spending_amount__error_max__description)
98-
.replace("{amount}", currentMaxAllowedToSend.formatToModernDisplay()),
99-
)
95+
AmountInputEffect.MaxExceeded -> {
96+
amountInputViewModel.setSats(currentMaxAllowedToSend, currentCurrencies)
97+
toast(
98+
context.getString(R.string.lightning__spending_amount__error_max__title),
99+
context.getString(R.string.lightning__spending_amount__error_max__description)
100+
.replace("{amount}", currentMaxAllowedToSend.formatToModernDisplay()),
101+
)
102+
}
100103
}
101104
}
102105
}
@@ -109,23 +112,10 @@ fun SpendingAmountScreen(
109112
currencies = currencies,
110113
onBackClick = onBackClick,
111114
onClickQuarter = {
112-
val quarter = uiState.balanceAfterFeeQuarter()
113-
val max = uiState.maxAllowedToSend
114-
if (quarter > max) {
115-
toast(
116-
context.getString(R.string.lightning__spending_amount__error_max__title),
117-
context.getString(R.string.lightning__spending_amount__error_max__description)
118-
.replace("{amount}", max.formatToModernDisplay()),
119-
)
120-
}
121-
val cappedQuarter = min(quarter, max)
122-
viewModel.updateLimits(cappedQuarter)
123-
amountInputViewModel.setSats(cappedQuarter, currencies)
115+
amountInputViewModel.setSats(uiState.quarterAmount, currencies)
124116
},
125117
onClickMaxAmount = {
126-
val newAmountSats = uiState.maxAllowedToSend
127-
viewModel.updateLimits(newAmountSats)
128-
amountInputViewModel.setSats(newAmountSats, currencies)
118+
amountInputViewModel.setSats(uiState.maxAllowedToSend, currencies)
129119
},
130120
onConfirmAmount = { viewModel.onConfirmAmount(amountUiState.sats) },
131121
)

app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,14 @@ class TransferViewModel @Inject constructor(
405405
liquidity.maxClientBalanceSat.toLong(),
406406
maxClientBalance.toLong()
407407
)
408+
val quarterAmount = min((maxSend.toDouble() * 0.25).roundToLong(), maxSend)
408409

409410
_spendingUiState.update {
410411
it.copy(
411412
maxAllowedToSend = maxSend,
412413
isLoading = false,
413414
balanceAfterFee = maxSend,
415+
quarterAmount = quarterAmount,
414416
)
415417
}
416418
}.onFailure {
@@ -650,12 +652,11 @@ data class TransferToSpendingUiState(
650652
val isAdvanced: Boolean = false,
651653
val maxAllowedToSend: Long = 0,
652654
val balanceAfterFee: Long = 0,
655+
val quarterAmount: Long = 0,
653656
val isLoading: Boolean = false,
654657
val receivingAmount: Long = 0,
655658
val feeEstimate: Long? = null,
656-
) {
657-
fun balanceAfterFeeQuarter() = (balanceAfterFee.toDouble() * 0.25).roundToLong()
658-
}
659+
)
659660

660661
data class TransferValues(
661662
val defaultLspBalance: ULong = 0u,

app/src/test/java/to/bitkit/viewmodels/TransferViewModelTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import to.bitkit.repositories.LightningState
2727
import to.bitkit.repositories.TransferRepo
2828
import to.bitkit.repositories.WalletRepo
2929
import to.bitkit.test.BaseUnitTest
30+
import kotlin.math.roundToLong
3031
import kotlin.test.assertEquals
3132
import kotlin.time.Clock
3233
import kotlin.time.ExperimentalTime
@@ -89,6 +90,7 @@ class TransferViewModelTest : BaseUnitTest() {
8990
val state = sut.spendingUiState.value
9091
assertEquals(OPTION_MAX_CLIENT_BALANCE.toLong(), state.maxAllowedToSend)
9192
assertEquals(OPTION_MAX_CLIENT_BALANCE.toLong(), state.balanceAfterFee)
93+
assertEquals((OPTION_MAX_CLIENT_BALANCE.toDouble() * 0.25).roundToLong(), state.quarterAmount)
9294

9395
// The order fee must be estimated against the clamped client balance, not the full balance.
9496
verify(blocktankRepo).estimateOrderFee(eq(LSP_MAX_CLIENT_BALANCE), any(), any())

changelog.d/next/1013.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Transfer to Spending now fills in the maximum transferable amount when you enter a value above the limit, instead of only showing an error.

0 commit comments

Comments
 (0)