TransactionDepositComposable.kt (5431B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2022 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.deposit 18 19 import androidx.compose.foundation.layout.Column 20 import androidx.compose.foundation.layout.fillMaxWidth 21 import androidx.compose.foundation.layout.padding 22 import androidx.compose.foundation.rememberScrollState 23 import androidx.compose.foundation.verticalScroll 24 import androidx.compose.material3.MaterialTheme 25 import androidx.compose.material3.Surface 26 import androidx.compose.material3.Text 27 import androidx.compose.runtime.Composable 28 import androidx.compose.ui.Alignment 29 import androidx.compose.ui.Modifier 30 import androidx.compose.ui.platform.LocalContext 31 import androidx.compose.ui.res.stringResource 32 import androidx.compose.ui.tooling.preview.Preview 33 import androidx.compose.ui.unit.dp 34 import net.taler.common.Amount 35 import net.taler.common.CurrencySpecification 36 import net.taler.common.Timestamp 37 import net.taler.lib.android.toAbsoluteTime 38 import net.taler.wallet.BottomInsetsSpacer 39 import net.taler.wallet.R 40 import net.taler.wallet.backend.TalerErrorCode.EXCHANGE_GENERIC_KYC_REQUIRED 41 import net.taler.wallet.backend.TalerErrorInfo 42 import net.taler.wallet.balances.ScopeInfo 43 import net.taler.wallet.transactions.AmountType 44 import net.taler.wallet.transactions.DepositActions 45 import net.taler.wallet.transactions.ErrorTransactionButton 46 import net.taler.wallet.transactions.TransactionAction 47 import net.taler.wallet.transactions.TransactionAction.Abort 48 import net.taler.wallet.transactions.TransactionAction.Retry 49 import net.taler.wallet.transactions.TransactionAction.Suspend 50 import net.taler.wallet.transactions.TransactionAmountComposable 51 import net.taler.wallet.transactions.TransactionDeposit 52 import net.taler.wallet.transactions.TransactionMajorState.Pending 53 import net.taler.wallet.transactions.TransactionState 54 import net.taler.wallet.transactions.TransactionStateComposable 55 import net.taler.wallet.transactions.TransitionsComposable 56 57 @Composable 58 fun TransactionDepositComposable( 59 t: TransactionDeposit, 60 devMode: Boolean, 61 spec: CurrencySpecification?, 62 onWireTransfer: () -> Unit, 63 onShowQrCodes: () -> Unit, 64 onTransition: (t: TransactionAction) -> Unit, 65 modifier: Modifier = Modifier, 66 ) { 67 val scrollState = rememberScrollState() 68 Column( 69 modifier = modifier 70 .fillMaxWidth() 71 .verticalScroll(scrollState), 72 horizontalAlignment = Alignment.CenterHorizontally, 73 ) { 74 TransactionStateComposable(state = t.txState) 75 76 Text( 77 modifier = Modifier.padding(16.dp), 78 text = t.timestamp.ms.toAbsoluteTime(LocalContext.current).toString(), 79 style = MaterialTheme.typography.bodyLarge, 80 ) 81 82 DepositActions(t, 83 onWireTransfer = onWireTransfer, 84 onShowQrCodes = onShowQrCodes) 85 86 TransactionAmountComposable( 87 label = stringResource(id = R.string.amount_chosen), 88 amount = t.amountRaw.withSpec(spec), 89 amountType = AmountType.Neutral, 90 ) 91 92 if (t.amountEffective > t.amountRaw) { 93 val fee = t.amountEffective - t.amountRaw 94 TransactionAmountComposable( 95 label = stringResource(id = R.string.amount_fee), 96 amount = fee.withSpec(spec), 97 amountType = AmountType.Negative, 98 ) 99 } 100 101 TransactionAmountComposable( 102 label = stringResource(id = R.string.amount_sent), 103 amount = t.amountEffective.withSpec(spec), 104 amountType = AmountType.Negative, 105 ) 106 107 TransitionsComposable(t, devMode, onTransition) 108 if (devMode && t.error != null) { 109 ErrorTransactionButton(error = t.error) 110 } 111 112 BottomInsetsSpacer() 113 } 114 } 115 116 @Preview 117 @Composable 118 fun TransactionDepositComposablePreview() { 119 val t = TransactionDeposit( 120 transactionId = "transactionId", 121 timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000), 122 txState = TransactionState(Pending), 123 txActions = listOf(Retry, Suspend, Abort), 124 depositGroupId = "fooBar", 125 amountRaw = Amount.fromString("TESTKUDOS", "42.1337"), 126 amountEffective = Amount.fromString("TESTKUDOS", "42.23"), 127 targetPaytoUri = "https://exchange.example.org/peer/pull/credit", 128 error = TalerErrorInfo(code = EXCHANGE_GENERIC_KYC_REQUIRED), 129 scopes = listOf(ScopeInfo.Exchange( 130 currency = "TESTKUDOS", 131 url = "exchange.test.taler.net", 132 )) 133 ) 134 Surface { 135 TransactionDepositComposable( 136 t = t, 137 devMode = true, 138 spec = null, 139 onWireTransfer = {}, 140 onShowQrCodes = {}, 141 onTransition = {}, 142 ) 143 } 144 }