From 1a230b0ca1c691e195d302ec5cebbb8e2acd667b Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Fri, 10 Mar 2023 00:18:33 -0600 Subject: [wallet] Expand JSON error in Compose transaction detail fragments bug 0007606 --- .../transactions/ErrorTransactionComposable.kt | 104 +++++++++++++++++++++ .../transactions/TransactionDepositFragment.kt | 2 +- .../wallet/transactions/TransactionPeerFragment.kt | 7 +- .../wallet/transactions/TransactionTipFragment.kt | 12 ++- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 wallet/src/main/java/net/taler/wallet/transactions/ErrorTransactionComposable.kt (limited to 'wallet/src/main/java/net/taler/wallet/transactions') diff --git a/wallet/src/main/java/net/taler/wallet/transactions/ErrorTransactionComposable.kt b/wallet/src/main/java/net/taler/wallet/transactions/ErrorTransactionComposable.kt new file mode 100644 index 0000000..7af219a --- /dev/null +++ b/wallet/src/main/java/net/taler/wallet/transactions/ErrorTransactionComposable.kt @@ -0,0 +1,104 @@ +/* + * This file is part of GNU Taler + * (C) 2023 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see + */ + +package net.taler.wallet.transactions + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.size +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Error +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontFamily +import kotlinx.serialization.json.Json +import net.taler.wallet.R +import net.taler.wallet.backend.TalerErrorInfo +import net.taler.wallet.compose.copyToClipBoard + +val json = Json { prettyPrint = true } + +@Composable +fun ErrorTransactionButton( + modifier: Modifier = Modifier, + transaction: Transaction +) { + val showDialog = remember { mutableStateOf(false) } + + if (transaction.error != null && showDialog.value) { + val message = json.encodeToString(TalerErrorInfo.serializer(), transaction.error!!) + AlertDialog(onDismissRequest = { + showDialog.value = false + }, + title = { + Text(stringResource(R.string.nav_error)) + }, + text = { + Column { + Text( + fontFamily = FontFamily.Monospace, + text = message, + ) + } + }, + dismissButton = { + TextButton(onClick = { + showDialog.value = false + }) { + Text(stringResource(R.string.close)) + } + }, + confirmButton = { + val context = LocalContext.current + TextButton(onClick = { + copyToClipBoard(context, context.getString(R.string.nav_error), message) + }) { + Text(stringResource(R.string.copy)) + } + }) + } + + Button( + modifier = modifier, + colors = ButtonDefaults.buttonColors( + contentColor = MaterialTheme.colorScheme.onError, + containerColor = MaterialTheme.colorScheme.error, + ), + onClick = { + showDialog.value = true + } + ) { + val label = stringResource(R.string.nav_error) + Icon( + Icons.Default.Error, + label, + modifier = Modifier.size(ButtonDefaults.IconSize), + ) + Spacer(Modifier.size(ButtonDefaults.IconSpacing)) + Text(label) + } +} \ No newline at end of file diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionDepositFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDepositFragment.kt index dd09b4c..3fd37ce 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionDepositFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionDepositFragment.kt @@ -35,7 +35,7 @@ class TransactionDepositFragment : TransactionDetailFragment() { setContent { TalerSurface { val t = transactionManager.selectedTransaction.observeAsState().value - if (t is TransactionDeposit) TransactionDepositComposable(t) { + if (t is TransactionDeposit) TransactionDepositComposable(t, devMode.value) { onDeleteButtonClicked(t) } } diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionPeerFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionPeerFragment.kt index 3d99774..ef227ed 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionPeerFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionPeerFragment.kt @@ -56,7 +56,7 @@ class TransactionPeerFragment : TransactionDetailFragment() { setContent { TalerSurface { val t = transactionManager.selectedTransaction.observeAsState(null).value - if (t != null) TransactionPeerComposable(t) { + if (t != null) TransactionPeerComposable(t, devMode.value) { onDeleteButtonClicked(t) } } @@ -65,7 +65,7 @@ class TransactionPeerFragment : TransactionDetailFragment() { } @Composable -fun TransactionPeerComposable(t: Transaction, onDelete: () -> Unit) { +fun TransactionPeerComposable(t: Transaction, devMode: Boolean?, onDelete: () -> Unit) { val scrollState = rememberScrollState() Column( modifier = Modifier @@ -87,6 +87,9 @@ fun TransactionPeerComposable(t: Transaction, onDelete: () -> Unit) { else -> error("unexpected transaction: ${t::class.simpleName}") } DeleteTransactionComposable(onDelete) + if (devMode == true && t.error != null) { + ErrorTransactionButton(transaction = t) + } } } diff --git a/wallet/src/main/java/net/taler/wallet/transactions/TransactionTipFragment.kt b/wallet/src/main/java/net/taler/wallet/transactions/TransactionTipFragment.kt index 02b522f..df2077d 100644 --- a/wallet/src/main/java/net/taler/wallet/transactions/TransactionTipFragment.kt +++ b/wallet/src/main/java/net/taler/wallet/transactions/TransactionTipFragment.kt @@ -41,6 +41,8 @@ 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.EXCHANGE_GENERIC_KYC_REQUIRED +import net.taler.wallet.backend.TalerErrorInfo import net.taler.wallet.compose.TalerSurface import net.taler.wallet.transactions.ExtendedStatus.Pending @@ -54,7 +56,7 @@ class TransactionTipFragment : TransactionDetailFragment() { setContent { TalerSurface { val t = transactionManager.selectedTransaction.observeAsState(null).value - if (t is TransactionTip) TransactionTipComposable(t) { + if (t is TransactionTip) TransactionTipComposable(t, devMode.value) { onDeleteButtonClicked(t) } } @@ -63,7 +65,7 @@ class TransactionTipFragment : TransactionDetailFragment() { } @Composable -fun TransactionTipComposable(t: TransactionTip, onDelete: () -> Unit) { +fun TransactionTipComposable(t: TransactionTip, devMode: Boolean?, onDelete: () -> Unit) { val scrollState = rememberScrollState() Column( modifier = Modifier @@ -101,6 +103,9 @@ fun TransactionTipComposable(t: TransactionTip, onDelete: () -> Unit) { info = t.merchantBaseUrl, ) DeleteTransactionComposable(onDelete) + if (devMode == true && t.error != null) { + ErrorTransactionButton(transaction = t) + } } } @@ -114,8 +119,9 @@ fun TransactionTipPreview() { merchantBaseUrl = "https://merchant.example.org/", amountRaw = Amount.fromDouble("TESTKUDOS", 42.23), amountEffective = Amount.fromDouble("TESTKUDOS", 42.1337), + error = TalerErrorInfo(code = EXCHANGE_GENERIC_KYC_REQUIRED), ) Surface { - TransactionTipComposable(t) {} + TransactionTipComposable(t, true) {} } } -- cgit v1.2.3