TransactionPeerPullCredit.kt (4851B)
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.peer 18 19 import androidx.compose.foundation.layout.ColumnScope 20 import androidx.compose.material3.Surface 21 import androidx.compose.runtime.Composable 22 import androidx.compose.ui.Modifier 23 import androidx.compose.ui.res.stringResource 24 import androidx.compose.ui.tooling.preview.Preview 25 import net.taler.common.Amount 26 import net.taler.common.CurrencySpecification 27 import net.taler.common.Timestamp 28 import net.taler.wallet.R 29 import net.taler.wallet.backend.TalerErrorCode.EXCHANGE_GENERIC_KYC_REQUIRED 30 import net.taler.wallet.backend.TalerErrorInfo 31 import net.taler.wallet.balances.ScopeInfo 32 import net.taler.wallet.transactions.AmountType 33 import net.taler.wallet.transactions.PeerActions 34 import net.taler.wallet.transactions.PeerInfoShort 35 import net.taler.wallet.transactions.TransactionAction.Abort 36 import net.taler.wallet.transactions.TransactionAction.Retry 37 import net.taler.wallet.transactions.TransactionAction.Suspend 38 import net.taler.wallet.transactions.TransactionAmountComposable 39 import net.taler.wallet.transactions.TransactionInfoComposable 40 import net.taler.wallet.transactions.TransactionMajorState.Done 41 import net.taler.wallet.transactions.TransactionMajorState.Pending 42 import net.taler.wallet.transactions.TransactionMinorState.CreatePurse 43 import net.taler.wallet.transactions.TransactionMinorState.Ready 44 import net.taler.wallet.transactions.TransactionPeerComposable 45 import net.taler.wallet.transactions.TransactionPeerPullCredit 46 import net.taler.wallet.transactions.TransactionState 47 48 @Composable 49 fun ColumnScope.TransactionPeerPullCreditComposable( 50 t: TransactionPeerPullCredit, 51 spec: CurrencySpecification?, 52 onConfirmKyc: (url: String) -> Unit, 53 ) { 54 PeerActions(t, onConfirmKyc) 55 56 if (t.error == null) PeerQrCode( 57 state = t.txState, 58 amount = t.amountRaw.withSpec(spec), 59 talerUri = t.talerUri, 60 instructionResId = R.string.receive_peer_payment_instruction, 61 ) 62 63 TransactionAmountComposable( 64 label = stringResource(id = R.string.amount_invoiced), 65 amount = t.amountRaw.withSpec(spec), 66 amountType = AmountType.Neutral, 67 ) 68 69 if (t.amountRaw > t.amountEffective) { 70 val fee = t.amountRaw - t.amountEffective 71 TransactionAmountComposable( 72 label = stringResource(id = R.string.amount_fee), 73 amount = fee.withSpec(spec), 74 amountType = AmountType.Negative, 75 ) 76 } 77 78 TransactionAmountComposable( 79 label = if (t.txState.major == Done) { 80 stringResource(id = R.string.amount_received) 81 } else { 82 stringResource(id = R.string.amount_receive) 83 }, 84 amount = t.amountEffective.withSpec(spec), 85 amountType = AmountType.Positive, 86 ) 87 88 TransactionInfoComposable( 89 label = stringResource(id = R.string.send_peer_purpose), 90 info = t.info.summary ?: "", 91 ) 92 } 93 94 @Preview 95 @Composable 96 fun TransactionPeerPullCreditPreview(loading: Boolean = false) { 97 val t = TransactionPeerPullCredit( 98 transactionId = "transactionId", 99 timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000), 100 txState = TransactionState(Pending, if (loading) CreatePurse else Ready), 101 txActions = listOf(Retry, Suspend, Abort), 102 exchangeBaseUrl = "https://exchange.example.org/", 103 amountRaw = Amount.fromString("TESTKUDOS", "42.23"), 104 amountEffective = Amount.fromString("TESTKUDOS", "42.1337"), 105 info = PeerInfoShort( 106 expiration = Timestamp.fromMillis(System.currentTimeMillis() + 60 * 60 * 1000), 107 summary = "test invoice", 108 ), 109 talerUri = "https://exchange.example.org/peer/pull/credit", 110 error = TalerErrorInfo(code = EXCHANGE_GENERIC_KYC_REQUIRED), 111 scopes = listOf(ScopeInfo.Exchange( 112 currency = "TESTKUDOS", 113 url = "exchange.test.taler.net", 114 )) 115 ) 116 Surface { 117 TransactionPeerComposable(t, true, null, Modifier, {}) {} 118 } 119 } 120 121 @Preview 122 @Composable 123 fun TransactionPeerPullCreditLoadingPreview() { 124 TransactionPeerPullCreditPreview(loading = true) 125 }