taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

TransactionRefreshFragment.kt (5022B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 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.transactions
     18 
     19 import android.os.Bundle
     20 import android.view.LayoutInflater
     21 import android.view.View
     22 import android.view.ViewGroup
     23 import androidx.compose.foundation.layout.Column
     24 import androidx.compose.foundation.layout.fillMaxWidth
     25 import androidx.compose.foundation.layout.padding
     26 import androidx.compose.foundation.rememberScrollState
     27 import androidx.compose.foundation.verticalScroll
     28 import androidx.compose.material3.MaterialTheme
     29 import androidx.compose.material3.Surface
     30 import androidx.compose.material3.Text
     31 import androidx.compose.runtime.Composable
     32 import androidx.compose.runtime.getValue
     33 import androidx.compose.ui.Alignment.Companion.CenterHorizontally
     34 import androidx.compose.ui.Modifier
     35 import androidx.compose.ui.platform.ComposeView
     36 import androidx.compose.ui.platform.LocalContext
     37 import androidx.compose.ui.res.stringResource
     38 import androidx.compose.ui.tooling.preview.Preview
     39 import androidx.compose.ui.unit.dp
     40 import net.taler.common.Amount
     41 import net.taler.common.CurrencySpecification
     42 import net.taler.common.Timestamp
     43 import net.taler.common.toAbsoluteTime
     44 import net.taler.wallet.BottomInsetsSpacer
     45 import net.taler.wallet.R
     46 import net.taler.wallet.backend.TalerErrorCode
     47 import net.taler.wallet.backend.TalerErrorInfo
     48 import net.taler.wallet.balances.ScopeInfo
     49 import net.taler.wallet.compose.TalerSurface
     50 import net.taler.wallet.compose.collectAsStateLifecycleAware
     51 import net.taler.wallet.transactions.TransactionAction.Abort
     52 import net.taler.wallet.transactions.TransactionAction.Retry
     53 import net.taler.wallet.transactions.TransactionAction.Suspend
     54 import net.taler.wallet.transactions.TransactionMajorState.Pending
     55 
     56 class TransactionRefreshFragment : TransactionDetailFragment() {
     57 
     58     override fun onCreateView(
     59         inflater: LayoutInflater,
     60         container: ViewGroup?,
     61         savedInstanceState: Bundle?,
     62     ): View = ComposeView(requireContext()).apply {
     63         setContent {
     64             TalerSurface {
     65                 val t by transactionManager.selectedTransaction.collectAsStateLifecycleAware()
     66                 (t as? TransactionRefresh)?.let { tx ->
     67                     TransactionRefreshComposable(tx, devMode,
     68                         exchangeManager.getSpecForCurrency(tx.amountRaw.currency, tx.scopes),
     69                     ) {
     70                         onTransitionButtonClicked(tx, it)
     71                     }
     72                 }
     73             }
     74         }
     75     }
     76 }
     77 
     78 @Composable
     79 private fun TransactionRefreshComposable(
     80     t: TransactionRefresh,
     81     devMode: Boolean,
     82     spec: CurrencySpecification?,
     83     onTransition: (t: TransactionAction) -> Unit,
     84 ) {
     85     val scrollState = rememberScrollState()
     86     Column(
     87         modifier = Modifier
     88             .fillMaxWidth()
     89             .verticalScroll(scrollState),
     90         horizontalAlignment = CenterHorizontally,
     91     ) {
     92         val context = LocalContext.current
     93 
     94         TransactionStateComposable(state = t.txState)
     95 
     96         Text(
     97             modifier = Modifier.padding(16.dp),
     98             text = t.timestamp.ms.toAbsoluteTime(context).toString(),
     99             style = MaterialTheme.typography.bodyLarge,
    100         )
    101 
    102         TransactionAmountComposable(
    103             label = stringResource(id = R.string.amount_fee),
    104             amount = t.amountEffective.withSpec(spec),
    105             amountType = AmountType.Negative,
    106         )
    107 
    108         TransitionsComposable(t, devMode, onTransition)
    109         if (devMode && t.error != null) {
    110             ErrorTransactionButton(error = t.error)
    111         }
    112 
    113         BottomInsetsSpacer()
    114     }
    115 }
    116 
    117 @Preview
    118 @Composable
    119 private fun TransactionRefreshComposablePreview() {
    120     val t = TransactionRefresh(
    121         transactionId = "transactionId",
    122         timestamp = Timestamp.fromMillis(System.currentTimeMillis() - 360 * 60 * 1000),
    123         txState = TransactionState(Pending),
    124         txActions = listOf(Retry, Suspend, Abort),
    125         amountRaw = Amount.fromString("TESTKUDOS", "42.23"),
    126         amountEffective = Amount.fromString("TESTKUDOS", "42.1337"),
    127         error = TalerErrorInfo(code = TalerErrorCode.WALLET_WITHDRAWAL_KYC_REQUIRED),
    128         scopes = listOf(ScopeInfo.Exchange(
    129             currency = "TESTKUDOS",
    130             url = "exchange.test.taler.net",
    131         ))
    132     )
    133     Surface {
    134         TransactionRefreshComposable(t, true, null) {}
    135     }
    136 }