diff options
author | Torsten Grote <t@grobox.de> | 2023-04-13 10:36:39 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2023-04-13 11:01:02 -0300 |
commit | 1c6fda4c3bfd7599f530b313e6533aa1a5c7c7e8 (patch) | |
tree | 01e096ac3b88b92343a5635a64aa8ef3a0def595 /wallet/src/main/java/net/taler | |
parent | c2b8d13fc0c0360ca1f3b66404f41ce1d08c1122 (diff) | |
download | taler-android-1c6fda4c3bfd7599f530b313e6533aa1a5c7c7e8.tar.gz taler-android-1c6fda4c3bfd7599f530b313e6533aa1a5c7c7e8.tar.bz2 taler-android-1c6fda4c3bfd7599f530b313e6533aa1a5c7c7e8.zip |
[wallet] Clean up compose migration of withdrawal and refresh transactions
Diffstat (limited to 'wallet/src/main/java/net/taler')
4 files changed, 146 insertions, 84 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/transactions/ActionButtonComposable.kt b/wallet/src/main/java/net/taler/wallet/transactions/ActionButtonComposable.kt index 2e2b4ac..d4c12aa 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/ActionButtonComposable.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/ActionButtonComposable.kt @@ -17,7 +17,6 @@ package net.taler.wallet.transactions import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.defaultMinSize import androidx.compose.foundation.layout.size import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.AccountBalance @@ -28,11 +27,10 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource -import androidx.compose.ui.unit.dp import net.taler.wallet.R import net.taler.wallet.backend.TalerErrorCode -import net.taler.wallet.transactions.WithdrawalDetails.TalerBankIntegrationApi import net.taler.wallet.transactions.WithdrawalDetails.ManualTransfer +import net.taler.wallet.transactions.WithdrawalDetails.TalerBankIntegrationApi interface ActionListener { enum class Type { @@ -47,28 +45,27 @@ interface ActionListener { @Composable fun ActionButton( modifier: Modifier = Modifier, - tx: Transaction, + tx: TransactionWithdrawal, listener: ActionListener, - isSmall: Boolean = false, ) { if (tx.error != null) { // There is an error! - when (tx.error!!.code) { + when (tx.error.code) { TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED -> { - KycButton(modifier, tx, listener, isSmall) + KycButton(modifier, tx, listener) } else -> {} } - } else if (tx is TransactionWithdrawal && !tx.confirmed) { + } else if (!tx.confirmed) { // There is a transaction! if (tx.withdrawalDetails is TalerBankIntegrationApi && tx.withdrawalDetails.bankConfirmationUrl != null ) { // The transaction can be completed with a link! - ConfirmBankButton(modifier, tx, listener, isSmall) + ConfirmBankButton(modifier, tx, listener) } else if (tx.withdrawalDetails is ManualTransfer) { // The transaction must be completed manually! - ConfirmManualButton(modifier, tx, listener, isSmall) + ConfirmManualButton(modifier, tx, listener) } } } @@ -76,14 +73,12 @@ fun ActionButton( @Composable private fun KycButton( modifier: Modifier = Modifier, - tx: Transaction, + tx: TransactionWithdrawal, listener: ActionListener, - isSmall: Boolean = false, ) { - val minSize = if (isSmall) 1.dp else 0.dp Button( onClick = { listener.onActionButtonClicked(tx, ActionListener.Type.COMPLETE_KYC) }, - modifier = modifier.defaultMinSize(minSize, minSize), + modifier = modifier, ) { Text(stringResource(R.string.transaction_action_kyc)) } @@ -94,12 +89,10 @@ private fun ConfirmBankButton( modifier: Modifier = Modifier, tx: TransactionWithdrawal, listener: ActionListener, - isSmall: Boolean = false, ) { - val minSize = if (isSmall) 1.dp else 0.dp Button( onClick = { listener.onActionButtonClicked(tx, ActionListener.Type.CONFIRM_WITH_BANK) }, - modifier = modifier.defaultMinSize(minSize, minSize), + modifier = modifier, ) { val label = stringResource(R.string.withdraw_button_confirm_bank) Icon( @@ -117,12 +110,10 @@ private fun ConfirmManualButton( modifier: Modifier = Modifier, tx: TransactionWithdrawal, listener: ActionListener, - isSmall: Boolean = false, ) { - val minSize = if (isSmall) 1.dp else 0.dp Button( onClick = { listener.onActionButtonClicked(tx, ActionListener.Type.CONFIRM_MANUAL) }, - modifier = modifier.defaultMinSize(minSize, minSize), + modifier = modifier, ) { val label = stringResource(R.string.withdraw_manual_ready_details_intro) Icon( diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt index 7bd4999..391eefa 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionRefreshFragment.kt @@ -20,10 +20,30 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Alignment.Companion.CenterHorizontally +import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import net.taler.common.Amount +import net.taler.common.Timestamp +import net.taler.common.toAbsoluteTime +import net.taler.wallet.R +import net.taler.wallet.backend.TalerErrorCode +import net.taler.wallet.backend.TalerErrorInfo import net.taler.wallet.compose.TalerSurface -import net.taler.wallet.withdraw.TransactionWithdrawalComposable class TransactionRefreshFragment : TransactionDetailFragment() { @@ -35,15 +55,58 @@ class TransactionRefreshFragment : TransactionDetailFragment() { setContent { TalerSurface { val t = transactionManager.selectedTransaction.observeAsState().value - if (t is TransactionRefresh) { - TransactionWithdrawalComposable(t, devMode.value, null) { - onDeleteButtonClicked(t) - } + val devMode = devMode.observeAsState().value ?: false + if (t is TransactionRefresh) TransactionRefreshComposable(t, devMode) { + onDeleteButtonClicked(t) } } } } +} +@Composable +private fun TransactionRefreshComposable( + t: TransactionRefresh, + devMode: Boolean, + onDelete: () -> Unit, +) { + val scrollState = rememberScrollState() + Column( + modifier = Modifier + .fillMaxWidth() + .verticalScroll(scrollState), + horizontalAlignment = CenterHorizontally, + ) { + val context = LocalContext.current + Text( + modifier = Modifier.padding(16.dp), + text = t.timestamp.ms.toAbsoluteTime(context).toString(), + style = MaterialTheme.typography.bodyLarge, + ) + TransactionAmountComposable( + label = stringResource(id = R.string.withdraw_fees), + amount = t.amountEffective, + amountType = AmountType.Negative, + ) + DeleteTransactionComposable(onDelete) + if (devMode && t.error != null) { + ErrorTransactionButton(error = t.error) + } + } +} - +@Preview +@Composable +private fun TransactionRefreshComposablePreview() { + val t = TransactionRefresh( + transactionId = "transactionId", + timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000), + extendedStatus = ExtendedStatus.Pending, + amountRaw = Amount.fromDouble("TESTKUDOS", 42.23), + amountEffective = Amount.fromDouble("TESTKUDOS", 42.1337), + error = TalerErrorInfo(code = TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED), + ) + Surface { + TransactionRefreshComposable(t, true) {} + } } diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt index 3a5b0d0..7a85522 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionWithdrawalFragment.kt @@ -38,6 +38,16 @@ class TransactionWithdrawalFragment : TransactionDetailFragment(), ActionListene private val model: MainViewModel by activityViewModels() private val withdrawManager by lazy { model.withdrawManager } + private val isPending get() = transactionManager.selectedTransaction.value?.extendedStatus == ExtendedStatus.Pending + + override val deleteDialogTitle: Int + get() = if (isPending) R.string.cancel else super.deleteDialogTitle + override val deleteDialogMessage: Int + get() = if (isPending) R.string.transactions_cancel_dialog_message + else super.deleteDialogMessage + override val deleteDialogButton: Int + get() = if (isPending) R.string.ok else super.deleteDialogButton + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -46,17 +56,20 @@ class TransactionWithdrawalFragment : TransactionDetailFragment(), ActionListene setContent { TalerSurface { val t = transactionManager.selectedTransaction.observeAsState().value - if (t is TransactionWithdrawal) { - TransactionWithdrawalComposable(t, devMode.value, this@TransactionWithdrawalFragment) { - onDeleteButtonClicked(t) - } + val devMode = devMode.observeAsState().value ?: false + if (t is TransactionWithdrawal) TransactionWithdrawalComposable( + t = t, + devMode = devMode, + actionListener = this@TransactionWithdrawalFragment, + ) { + onDeleteButtonClicked(t) } } } } override fun onActionButtonClicked(tx: Transaction, type: ActionListener.Type) { - when(type) { + when (type) { ActionListener.Type.COMPLETE_KYC -> { tx.error?.getStringExtra("kycUrl")?.let { kycUrl -> launchInAppBrowser(requireContext(), kycUrl) diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt b/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt index 578e106..f1a22d3 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/TransactionWithdrawalComposable.kt @@ -17,17 +17,23 @@ package net.taler.wallet.withdraw import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment +import androidx.compose.ui.Alignment.Companion.CenterVertically import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @@ -47,15 +53,14 @@ import net.taler.wallet.transactions.ExtendedStatus import net.taler.wallet.transactions.Transaction import net.taler.wallet.transactions.TransactionAmountComposable import net.taler.wallet.transactions.TransactionInfoComposable -import net.taler.wallet.transactions.TransactionRefresh import net.taler.wallet.transactions.TransactionWithdrawal import net.taler.wallet.transactions.WithdrawalDetails.ManualTransfer @Composable fun TransactionWithdrawalComposable( - t: Transaction, - devMode: Boolean?, - listener: ActionListener?, + t: TransactionWithdrawal, + devMode: Boolean, + actionListener: ActionListener, onDelete: () -> Unit, ) { val scrollState = rememberScrollState() @@ -71,43 +76,50 @@ fun TransactionWithdrawalComposable( text = t.timestamp.ms.toAbsoluteTime(context).toString(), style = MaterialTheme.typography.bodyLarge, ) - if (t !is TransactionRefresh) { - TransactionAmountComposable( - label = stringResource(id = R.string.withdraw_total), - amount = t.amountEffective, - amountType = AmountType.Positive, - ) - } - listener?.let { - ActionButton(tx = t, listener = it) - } - if (t !is TransactionRefresh) { - TransactionAmountComposable( - label = stringResource(id = R.string.amount_chosen), - amount = t.amountRaw, - amountType = AmountType.Neutral, - ) - } + TransactionAmountComposable( + label = stringResource(id = R.string.withdraw_total), + amount = t.amountEffective, + amountType = AmountType.Positive, + ) + ActionButton(tx = t, listener = actionListener) + TransactionAmountComposable( + label = stringResource(id = R.string.amount_chosen), + amount = t.amountRaw, + amountType = AmountType.Neutral, + ) TransactionAmountComposable( label = stringResource(id = R.string.withdraw_fees), - amount = when (t) { - is TransactionWithdrawal -> t.amountRaw - t.amountEffective - else -> t.amountEffective - }, + amount = t.amountRaw - t.amountEffective, amountType = AmountType.Negative, ) - when (t) { - is TransactionWithdrawal -> t.exchangeBaseUrl - else -> null - }?.let { url -> - TransactionInfoComposable( - label = stringResource(id = R.string.withdraw_exchange), - info = cleanExchange(url), - ) + TransactionInfoComposable( + label = stringResource(id = R.string.withdraw_exchange), + info = cleanExchange(t.exchangeBaseUrl), + ) + if (t.extendedStatus == ExtendedStatus.Pending) { + Button( + modifier = Modifier.padding(16.dp), + colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error), + onClick = onDelete, + ) { + Row(verticalAlignment = CenterVertically) { + Icon( + painter = painterResource(id = R.drawable.ic_cancel), + contentDescription = null, + tint = MaterialTheme.colorScheme.onError, + ) + Text( + modifier = Modifier.padding(start = 8.dp), + text = stringResource(R.string.cancel), + color = MaterialTheme.colorScheme.onError, + ) + } + } + } else { + DeleteTransactionComposable(onDelete) } - DeleteTransactionComposable(onDelete) - if (devMode == true && t.error != null) { - ErrorTransactionButton(error = t.error!!) + if (devMode && t.error != null) { + ErrorTransactionButton(error = t.error) } } } @@ -127,26 +139,9 @@ fun TransactionWithdrawalComposablePreview() { ) val listener = object : ActionListener { override fun onActionButtonClicked(tx: Transaction, type: ActionListener.Type) { - TODO("Not yet implemented") } } Surface { TransactionWithdrawalComposable(t, true, listener) {} } } - -@Preview -@Composable -fun TransactionRefreshComposablePreview() { - val t = TransactionRefresh( - transactionId = "transactionId", - timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000), - extendedStatus = ExtendedStatus.Pending, - amountRaw = Amount.fromDouble("TESTKUDOS", 42.23), - amountEffective = Amount.fromDouble("TESTKUDOS", 42.1337), - error = TalerErrorInfo(code = TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED), - ) - Surface { - TransactionWithdrawalComposable(t, true, null) {} - } -}
\ No newline at end of file |