@@ -23,6 +23,7 @@ import androidx.compose.ui.Modifier
2323import androidx.compose.ui.platform.LocalContext
2424import androidx.compose.ui.res.stringResource
2525import androidx.compose.ui.unit.dp
26+ import androidx.lifecycle.compose.collectAsStateWithLifecycle
2627import kotlinx.coroutines.launch
2728import to.bitkit.R
2829import to.bitkit.models.Toast
@@ -60,6 +61,7 @@ fun SpendingAmountScreen(
6061 val blocktank = blocktankViewModel ? : return
6162 val currencies = LocalCurrencies .current
6263 val resources = LocalContext .current.resources
64+ val transferValues by viewModel.transferValues.collectAsStateWithLifecycle()
6365
6466 ScreenColumn {
6567 AppTopBar (
@@ -78,41 +80,39 @@ fun SpendingAmountScreen(
7880 var isLoading by remember { mutableStateOf(false ) }
7981
8082 val availableAmount = LocalBalances .current.totalOnchainSats - 512u // default tx fee
81-
82- var maxClientBalance by remember { mutableStateOf(0uL ) }
83- var maxLspBalance by remember { mutableStateOf(0uL ) }
8483 var maxLspFee by remember { mutableStateOf(0uL ) }
8584
8685 val feeMaximum = max(0 , availableAmount.toLong() - maxLspFee.toLong())
87- val maximum = min(maxClientBalance.toLong(), feeMaximum)
86+ val maximum = min(
87+ transferValues.maxClientBalance.toLong(),
88+ feeMaximum
89+ ) // TODO USE MAX AVAILABLE TO TRANSFER INSTEAD OF MAX ONCHAIN BALANCE
8890
8991 // Update maxClientBalance Effect
9092 LaunchedEffect (satsAmount) {
91- val transferValues = viewModel.calculateTransferValues(satsAmount.toULong())
92- maxClientBalance = transferValues.maxClientBalance
93- Logger .debug(" maxClientBalance: $maxClientBalance " , context = " SpendingAmountScreen" )
94- }
95-
96- // Update maxLspBalance Effect
97- LaunchedEffect (availableAmount) {
98- val transferValues = viewModel.calculateTransferValues(availableAmount)
99- maxLspBalance = transferValues.defaultLspBalance
100- Logger .debug(" maxLspBalance: $maxLspBalance " , context = " SpendingAmountScreen" )
93+ viewModel.updateTransferValues(satsAmount.toULong())
94+ Logger .debug(
95+ " satsAmount changed - maxClientBalance: ${transferValues.maxClientBalance} " ,
96+ context = " SpendingAmountScreen"
97+ )
10198 }
10299
103100 // Update maxLspFee Effect
104- LaunchedEffect (availableAmount, maxLspBalance) {
101+ LaunchedEffect (availableAmount, transferValues. maxLspBalance) { // TODO MOVE TO VIEWMODEL
105102 runCatching {
106103 val estimate = blocktank.estimateOrderFee(
107104 spendingBalanceSats = availableAmount,
108- receivingBalanceSats = maxLspBalance,
105+ receivingBalanceSats = transferValues. maxLspBalance,
109106 )
110107 maxLspFee = estimate.feeSat
111108 }
112109 }
113110
114111 Spacer (modifier = Modifier .height(32 .dp))
115- Display (text = stringResource(R .string.lightning__spending_amount__title).withAccent(accentColor = Colors .Purple ))
112+ Display (
113+ text = stringResource(R .string.lightning__spending_amount__title)
114+ .withAccent(accentColor = Colors .Purple )
115+ )
116116 Spacer (modifier = Modifier .height(32 .dp))
117117
118118 AmountInput (
@@ -167,11 +167,13 @@ fun SpendingAmountScreen(
167167 PrimaryButton (
168168 text = stringResource(R .string.common__continue),
169169 onClick = {
170- if (maxLspBalance == 0uL ) {
170+ if (transferValues. maxLspBalance == 0uL ) {
171171 app.toast(
172172 type = Toast .ToastType .ERROR ,
173173 title = resources.getString(R .string.lightning__spending_amount__error_max__title),
174- description = resources.getString(R .string.lightning__spending_amount__error_max__description_zero),
174+ description = resources.getString(
175+ R .string.lightning__spending_amount__error_max__description_zero
176+ ),
175177 )
176178 return @PrimaryButton
177179 }
0 commit comments