taler-android

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

PayTemplateComposable.kt (3866B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2023 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.runtime.Composable
     20 import androidx.compose.ui.res.stringResource
     21 import androidx.compose.ui.tooling.preview.Preview
     22 import net.taler.common.CurrencySpecification
     23 import net.taler.wallet.R
     24 import net.taler.wallet.backend.TalerErrorInfo
     25 import net.taler.wallet.compose.ErrorComposable
     26 import net.taler.wallet.compose.LoadingScreen
     27 import net.taler.wallet.compose.TalerSurface
     28 
     29 @Composable
     30 fun PayTemplateComposable(
     31     currencies: List<String>,
     32     payStatus: PayStatus,
     33     devMode: Boolean = false,
     34     getCurrencySpec: (String) -> CurrencySpecification?,
     35     onSubmit: (params: TemplateParams) -> Unit,
     36     onRetry: (() -> Unit)? = null,
     37 ) {
     38     if (currencies.isEmpty()) {
     39         ErrorComposable(
     40             error = TalerErrorInfo.makeCustomError(stringResource(R.string.payment_balance_insufficient)),
     41             devMode = devMode,
     42         )
     43     } else when (val p = payStatus) {
     44         is PayStatus.Checked -> {
     45             val usableCurrencies = currencies
     46                 .intersect(p.supportedCurrencies.toSet())
     47                 .toList()
     48             if (usableCurrencies.isEmpty()) {
     49                 // If user doesn't have any supported currency, they can't pay either
     50                 ErrorComposable(
     51                     error = TalerErrorInfo.makeCustomError(stringResource(R.string.payment_balance_insufficient)),
     52                     devMode = devMode,
     53                 )
     54             } else if (!p.details.isTemplateEditable(usableCurrencies)) {
     55                 // Non-editable: auto-preparing, show loading instead of flashing the form
     56                 PayTemplateLoading()
     57             } else {
     58                 PayTemplateOrderComposable(
     59                     usableCurrencies = usableCurrencies,
     60                     templateDetails = p.details,
     61                     onSubmit = onSubmit,
     62                     getCurrencySpec = getCurrencySpec,
     63                 )
     64             }
     65         }
     66         is PayStatus.None, is PayStatus.Loading -> PayTemplateLoading()
     67         is PayStatus.Error -> ErrorComposable(
     68             error = p.error,
     69             message = stringResource(R.string.payment_template_error),
     70             devMode = devMode,
     71             onRetry = onRetry,
     72         )
     73         is PayStatus.Prepared -> PayTemplateLoading()
     74 
     75         // not emitted by template flow
     76         is PayStatus.Pending,
     77         is PayStatus.AlreadyPaid,
     78         is PayStatus.InsufficientBalance,
     79         is PayStatus.Success,
     80         is PayStatus.Choices -> {}
     81     }
     82 }
     83 
     84 @Composable
     85 fun PayTemplateLoading() {
     86     LoadingScreen()
     87 }
     88 
     89 @Preview
     90 @Composable
     91 fun PayTemplateLoadingPreview() {
     92     TalerSurface {
     93         PayTemplateComposable(
     94             payStatus = PayStatus.Loading,
     95             currencies = listOf("KUDOS", "ARS"),
     96             onSubmit = { _ -> },
     97             getCurrencySpec = { null },
     98         )
     99     }
    100 }
    101 
    102 @Preview
    103 @Composable
    104 fun PayTemplateNoCurrenciesPreview() {
    105     TalerSurface {
    106         PayTemplateComposable(
    107             payStatus = PayStatus.None,
    108             currencies = emptyList(),
    109             onSubmit = { _ -> },
    110             getCurrencySpec = { null },
    111         )
    112     }
    113 }