taler-android

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

PayTemplateScreen.kt (4574B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2026 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.payment
     18 
     19 import androidx.compose.foundation.layout.Box
     20 import androidx.compose.foundation.layout.fillMaxSize
     21 import androidx.compose.foundation.layout.padding
     22 import androidx.compose.material3.Text
     23 import androidx.compose.runtime.Composable
     24 import androidx.compose.runtime.LaunchedEffect
     25 import androidx.compose.runtime.getValue
     26 import androidx.compose.runtime.livedata.observeAsState
     27 import androidx.compose.ui.Modifier
     28 import androidx.compose.ui.res.stringResource
     29 import androidx.lifecycle.asFlow
     30 import net.taler.wallet.NavigateCallback
     31 import net.taler.wallet.R
     32 import net.taler.wallet.WalletDestination
     33 import net.taler.wallet.backend.TalerErrorInfo
     34 import net.taler.wallet.balances.BalanceState
     35 import net.taler.wallet.compose.ErrorComposable
     36 import net.taler.wallet.compose.GlobalScaffold
     37 import net.taler.wallet.compose.LoadingScreen
     38 import net.taler.wallet.compose.TalerSurface
     39 import net.taler.wallet.compose.collectAsStateLifecycleAware
     40 import net.taler.wallet.main.MainViewModel
     41 
     42 @Composable
     43 fun PayTemplateScreen(
     44     model: MainViewModel,
     45     uri: String,
     46     onNavigate: NavigateCallback,
     47     onNavigateBack: () -> Unit,
     48     onShowError: (TalerErrorInfo) -> Unit,
     49 ) {
     50     val paymentManager = model.paymentManager
     51     val balanceManager = model.balanceManager
     52     val exchangeManager = model.exchangeManager
     53     val transactionManager = model.transactionManager
     54 
     55     val payStatus by paymentManager.payStatus.asFlow().collectAsStateLifecycleAware(PayStatus.None)
     56     val balanceState by balanceManager.state.observeAsState(BalanceState.None)
     57     val devMode by model.devMode.observeAsState(false)
     58 
     59     LaunchedEffect(Unit) {
     60         balanceManager.loadAssets()
     61         paymentManager.checkPayForTemplate(uri)
     62     }
     63 
     64     LaunchedEffect(payStatus) {
     65         when (val s = payStatus) {
     66             is PayStatus.Prepared -> {
     67                 if (transactionManager.selectTransaction(s.transactionId)) {
     68                     onNavigate(WalletDestination.TransactionPayment, true)
     69                 }
     70             }
     71 
     72             is PayStatus.Pending -> if (s.error != null) {
     73                 onShowError(s.error)
     74             }
     75 
     76             is PayStatus.Checked -> {
     77                 val usableCurrencies = balanceManager.getCurrencies()
     78                     .intersect(s.supportedCurrencies.toSet())
     79                     .toList()
     80                 if (!s.details.isTemplateEditable(usableCurrencies)) {
     81                     paymentManager.preparePayForTemplate(uri, s.details.toTemplateParams())
     82                 }
     83             }
     84 
     85             else -> {}
     86         }
     87     }
     88 
     89     TalerSurface {
     90         GlobalScaffold(
     91             model = model,
     92             modifier = Modifier.fillMaxSize(),
     93             title = { Text(stringResource(R.string.payment_pay_template_title)) },
     94             onNavigateBack = onNavigateBack,
     95         ) { paddingValues ->
     96             Box (Modifier.padding(paddingValues)) {
     97                 when (val state = balanceState) {
     98                     is BalanceState.None, is BalanceState.Loading -> LoadingScreen()
     99                     is BalanceState.Error -> ErrorComposable(state.error, devMode = devMode)
    100                     is BalanceState.Success -> PayTemplateComposable(
    101                         currencies = state.balances.map { it.currency },
    102                         payStatus = payStatus,
    103                         onCreateAmount = model::createAmount,
    104                         onSubmit = { params ->
    105                             paymentManager.preparePayForTemplate(uri, params)
    106                         },
    107                         onError = { errorMsg ->
    108                             onShowError(TalerErrorInfo.makeCustomError(errorMsg))
    109                         },
    110                         getCurrencySpec = exchangeManager::getSpecForCurrency,
    111                     )
    112                 }
    113             }
    114         }
    115     }
    116 }