diff options
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt')
-rw-r--r-- | wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt b/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt index 4fbb09b..2accaaf 100644 --- a/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/ReceiveFundsFragment.kt @@ -29,12 +29,9 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState -import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Button -import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -46,7 +43,6 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.input.KeyboardType.Companion.Decimal import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.core.os.bundleOf @@ -55,13 +51,19 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import net.taler.common.Amount -import net.taler.common.Amount.Companion.isValidAmountStr +import net.taler.common.CurrencySpecification +import net.taler.wallet.compose.AmountInputField +import net.taler.wallet.compose.DEFAULT_INPUT_DECIMALS import net.taler.wallet.compose.TalerSurface import net.taler.wallet.exchanges.ExchangeItem class ReceiveFundsFragment : Fragment() { private val model: MainViewModel by activityViewModels() private val exchangeManager get() = model.exchangeManager + private val withdrawManager get() = model.withdrawManager + private val balanceManager get() = model.balanceManager + private val peerManager get() = model.peerManager + private val scopeInfo get() = model.transactionManager.selectedScope ?: error("No scope selected") override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -70,7 +72,8 @@ class ReceiveFundsFragment : Fragment() { setContent { TalerSurface { ReceiveFundsIntro( - model.transactionManager.selectedCurrency ?: error("No currency selected"), + scopeInfo.currency, + balanceManager.getSpecForScopeInfo(scopeInfo), this@ReceiveFundsFragment::onManualWithdraw, this@ReceiveFundsFragment::onPeerPull, ) @@ -80,7 +83,7 @@ class ReceiveFundsFragment : Fragment() { override fun onStart() { super.onStart() - activity?.setTitle(R.string.transactions_receive_funds) + activity?.setTitle(getString(R.string.transactions_receive_funds_title, scopeInfo.currency)) } private fun onManualWithdraw(amount: Amount) { @@ -98,23 +101,24 @@ class ReceiveFundsFragment : Fragment() { Toast.makeText(requireContext(), "No exchange available", LENGTH_LONG).show() return } - exchangeManager.withdrawalExchange = exchange + // now that we have the exchange, we can navigate - val bundle = bundleOf("amount" to amount.toJSONString()) - findNavController().navigate( - R.id.action_receiveFunds_to_nav_exchange_manual_withdrawal, bundle) + exchangeManager.withdrawalExchange = exchange + withdrawManager.getWithdrawalDetails(exchange.exchangeBaseUrl, amount) + findNavController().navigate(R.id.action_receiveFunds_to_nav_prompt_withdraw) } private fun onPeerPull(amount: Amount) { val bundle = bundleOf("amount" to amount.toJSONString()) + peerManager.checkPeerPullCredit(amount) findNavController().navigate(R.id.action_receiveFunds_to_nav_peer_pull, bundle) } } -@OptIn(ExperimentalMaterial3Api::class) @Composable private fun ReceiveFundsIntro( currency: String, + spec: CurrencySpecification?, onManualWithdraw: (Amount) -> Unit, onPeerPull: (Amount) -> Unit, ) { @@ -124,39 +128,32 @@ private fun ReceiveFundsIntro( .fillMaxWidth() .verticalScroll(scrollState), ) { - var text by rememberSaveable { mutableStateOf("") } + var text by rememberSaveable { mutableStateOf("0") } var isError by rememberSaveable { mutableStateOf(false) } Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier .padding(16.dp), ) { - OutlinedTextField( + AmountInputField( modifier = Modifier .weight(1f) .padding(end = 16.dp), value = text, - keyboardOptions = KeyboardOptions.Default.copy(keyboardType = Decimal), onValueChange = { input -> isError = false - val filtered = input.filter { it.isDigit() || it == '.' } - if (filtered.endsWith('.') || isValidAmountStr(filtered)) text = filtered + text = input + }, + label = { Text(stringResource(R.string.receive_amount)) }, + supportingText = { + if (isError) Text(stringResource(R.string.receive_amount_invalid)) }, isError = isError, - label = { - if (isError) { - Text( - stringResource(R.string.receive_amount_invalid), - color = MaterialTheme.colorScheme.error, - ) - } else { - Text(stringResource(R.string.receive_amount)) - } - } + numberOfDecimals = spec?.numFractionalInputDigits ?: DEFAULT_INPUT_DECIMALS, ) Text( modifier = Modifier, - text = currency, + text = spec?.symbol ?: currency, softWrap = false, style = MaterialTheme.typography.titleLarge, ) @@ -174,7 +171,7 @@ private fun ReceiveFundsIntro( .weight(1f), onClick = { val amount = getAmount(currency, text) - if (amount == null) isError = true + if (amount == null || amount.isZero()) isError = true else onManualWithdraw(amount) }) { Text(text = stringResource(R.string.receive_withdraw)) @@ -185,7 +182,7 @@ private fun ReceiveFundsIntro( .height(IntrinsicSize.Max), onClick = { val amount = getAmount(currency, text) - if (amount == null) isError = true + if (amount == null || amount.isZero()) isError = true else onPeerPull(amount) }, ) { @@ -199,6 +196,6 @@ private fun ReceiveFundsIntro( @Composable fun PreviewReceiveFundsIntro() { Surface { - ReceiveFundsIntro("TESTKUDOS", {}) {} + ReceiveFundsIntro("TESTKUDOS", null, {}) {} } } |