summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/payment/PayTemplateComposable.kt
blob: b6c2fb1ce50239948ec048d5fec9f8e00a6e5e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * This file is part of GNU Taler
 * (C) 2023 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.wallet.payment

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment.Companion.Center
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import net.taler.common.Amount
import net.taler.common.ContractTerms
import net.taler.wallet.AmountResult
import net.taler.wallet.R
import net.taler.wallet.compose.TalerSurface

sealed class AmountFieldStatus {
    object FixedAmount : AmountFieldStatus()
    class Default(
        val amountStr: String? = null,
        val currency: String? = null,
    ) : AmountFieldStatus()

    object Invalid : AmountFieldStatus()
}

@Composable
fun PayTemplateComposable(
    defaultSummary: String?,
    amountStatus: AmountFieldStatus,
    currencies: List<String>,
    payStatus: PayStatus,
    onCreateAmount: (String, String) -> AmountResult,
    onSubmit: (summary: String?, amount: Amount?) -> Unit,
    onError: (resId: Int) -> Unit,
) {
    // If wallet is empty, there's no way the user can pay something
    if (amountStatus is AmountFieldStatus.Invalid) {
        PayTemplateError(stringResource(R.string.receive_amount_invalid))
    } else if (currencies.isEmpty()) {
        PayTemplateError(stringResource(R.string.payment_balance_insufficient))
    } else when (val p = payStatus) {
        is PayStatus.None -> PayTemplateOrderComposable(
            currencies = currencies,
            defaultSummary = defaultSummary,
            amountStatus = amountStatus,
            onCreateAmount = onCreateAmount,
            onError = onError,
            onSubmit = onSubmit,
        )

        is PayStatus.Loading -> PayTemplateLoading()
        is PayStatus.AlreadyPaid -> PayTemplateError(stringResource(R.string.payment_already_paid))
        is PayStatus.InsufficientBalance -> PayTemplateError(stringResource(R.string.payment_balance_insufficient))
        is PayStatus.Pending -> {
            val error = p.error
            PayTemplateError(if (error != null) {
                stringResource(R.string.payment_error, error.userFacingMsg)
            } else {
                stringResource(R.string.payment_template_error)
            })
        }
        is PayStatus.Prepared -> {} // handled in fragment, will redirect
        is PayStatus.Success -> {} // handled by other UI flow, no need for content here
    }
}

@Composable
fun PayTemplateError(message: String) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Center,
    ) {
        Text(
            text = message,
            style = MaterialTheme.typography.titleLarge,
            color = MaterialTheme.colorScheme.error,
        )
    }
}

@Composable
fun PayTemplateLoading() {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Center,
    ) {
        CircularProgressIndicator()
    }
}

@Preview
@Composable
fun PayTemplateLoadingPreview() {
    TalerSurface {
        PayTemplateComposable(
            defaultSummary = "Donation",
            amountStatus = AmountFieldStatus.Default("20", "ARS"),
            payStatus = PayStatus.Loading,
            currencies = listOf("KUDOS", "ARS"),
            onCreateAmount = { text, currency ->
                AmountResult.Success(amount = Amount.fromString(currency, text))
            },
            onSubmit = { _, _ -> },
            onError = { _ -> },
        )
    }
}

@Preview
@Composable
fun PayTemplateInsufficientBalancePreview() {
    TalerSurface {
        PayTemplateComposable(
            defaultSummary = "Donation",
            amountStatus = AmountFieldStatus.Default("20", "ARS"),
            payStatus = PayStatus.InsufficientBalance(
                ContractTerms(
                    "test",
                    amount = Amount.zero("TESTKUDOS"),
                    products = emptyList()
                ), Amount.zero("TESTKUDOS")
            ),
            currencies = listOf("KUDOS", "ARS"),
            onCreateAmount = { text, currency ->
                AmountResult.Success(amount = Amount.fromString(currency, text))
            },
            onSubmit = { _, _ -> },
            onError = { _ -> },
        )
    }
}

@Preview
@Composable
fun PayTemplateAlreadyPaidPreview() {
    TalerSurface {
        PayTemplateComposable(
            defaultSummary = "Donation",
            amountStatus = AmountFieldStatus.Default("20", "ARS"),
            payStatus = PayStatus.AlreadyPaid(transactionId = "transactionId"),
            currencies = listOf("KUDOS", "ARS"),
            onCreateAmount = { text, currency ->
                AmountResult.Success(amount = Amount.fromString(currency, text))
            },
            onSubmit = { _, _ -> },
            onError = { _ -> },
        )
    }
}


@Preview
@Composable
fun PayTemplateNoCurrenciesPreview() {
    TalerSurface {
        PayTemplateComposable(
            defaultSummary = "Donation",
            amountStatus = AmountFieldStatus.Default("20", "ARS"),
            payStatus = PayStatus.None,
            currencies = emptyList(),
            onCreateAmount = { text, currency ->
                AmountResult.Success(amount = Amount.fromString(currency, text))
            },
            onSubmit = { _, _ -> },
            onError = { _ -> },
        )
    }
}