summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Transactions/ThreeAmountsV.swift
blob: 1d05508ef3e7228d9de94558f64befc358f759a0 (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
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift

struct ThreeAmountsSheet: View {
    let stack: CallStack
    var common: TransactionCommon
    var topAbbrev: String
    var topTitle: String
    var bottomTitle: String?
    var bottomAbbrev: String?
    let baseURL: String?
    let large: Bool               // set to false for QR or IBAN
    let summary: String?
    let merchant: String?

    var body: some View {
        let raw = common.amountRaw
        let effective = common.amountEffective
        let fee = common.fee()
        let incoming = common.incoming()
        let pending = common.isPending
        let isDone = common.isDone
        let incomplete = !(isDone || pending)

        let defaultBottomTitle  = incoming ? (pending ? String(localized: "Pending amount to obtain:")
                                                      : String(localized: "Obtained amount:") )
                                           : (pending ? String(localized: "Amount to pay:")
                                                      : String(localized: "Paid amount:") )
        let defaultBottomAbbrev = incoming ? (pending ? String(localized: "Pending:", comment: "mini")
                                                      : String(localized: "Obtained:", comment: "mini") )
                                           : (pending ? String(localized: "Pay:", comment: "mini")
                                                      : String(localized: "Paid:", comment: "mini") )
        ThreeAmountsV(stack: stack.push(),
                   topTitle: topTitle, topAbbrev: topAbbrev,
                  topAmount: raw, fee: fee,
                bottomTitle: bottomTitle ?? defaultBottomTitle,
               bottomAbbrev: bottomAbbrev ?? defaultBottomAbbrev,
               bottomAmount: incomplete ? nil : effective,
                      large: large, pending: pending, incoming: incoming,
                    baseURL: baseURL,
                     status: common.txState.major.localizedState,
                    summary: summary,
                   merchant: merchant)
    }
}
// MARK: -
struct ThreeAmountsV: View {
    let stack: CallStack
    var topTitle: String
    var topAbbrev: String
    var topAmount: Amount
    var fee: Amount?                        // nil = don't show fee line, zero = no fee for this tx
    var bottomTitle: String
    var bottomAbbrev: String
    var bottomAmount: Amount?               // nil = incomplete (aborted, timed out)
    let large: Bool
    let pending: Bool
    let incoming: Bool
    let baseURL: String?
    let status: String?
    let summary: String?
    let merchant: String?

    @Environment(\.colorScheme) private var colorScheme
    @Environment(\.colorSchemeContrast) private var colorSchemeContrast
    @AppStorage("minimalistic") var minimalistic: Bool = false

    var body: some View {
        let labelColor = WalletColors().labelColor
        let foreColor = pending ? WalletColors().pendingColor(incoming)
                                : WalletColors().transactionColor(incoming)
        Section {
            if let summary {
                Text(summary)
                    .talerFont(.title3)
                    .lineLimit(4)
                    .padding(.bottom)
            }
            if let merchant {
                Text(merchant)
                    .talerFont(.title3)
                    .lineLimit(4)
                    .padding(.bottom)
            }
            AmountRowV(title: minimalistic ? topAbbrev : topTitle,
                      amount: topAmount,
                       color: labelColor,
                       large: false)
                .padding(.bottom, 4)
            if let fee {
                AmountRowV(title: minimalistic ? String(localized: "Fee (short):", defaultValue:"Fee:", comment:"short version")
                                               : String(localized: "Fee (long):", defaultValue:"Fee:", comment:"long version"),
                          amount: fee,
                           color: labelColor,
                           large: false)
                .padding(.bottom, 4)
            }
            if let bottomAmount {
                AmountRowV(title: minimalistic ? bottomAbbrev : bottomTitle,
                          amount: bottomAmount,
                           color: foreColor,
                           large: large)
            }
            if let baseURL {
                VStack(alignment: .leading) {
                               // TODO: "Issued by" for withdrawals
                    Text(minimalistic ? "Payment provider:" : "Using payment service provider:")
                        .multilineTextAlignment(.leading)
                        .talerFont(.body)
                    Text(baseURL.trimURL())
                        .frame(maxWidth: .infinity, alignment: .trailing)
                        .multilineTextAlignment(.center)
                        .talerFont(large ? .title3 : .body)
//                        .fontWeight(large ? .medium : .regular)  // @available(iOS 16.0, *)
                        .foregroundColor(labelColor)
                }
                .padding(.top, 4)
                .frame(maxWidth: .infinity, alignment: .leading)
                .listRowSeparator(.hidden)
                .accessibilityElement(children: .combine)
            }
        } header: {
            if !minimalistic {
                Text("Summary")
                    .talerFont(.title3)
                    .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
            }
        }
    }
}
// MARK: -
struct ThreeAmounts_Previews: PreviewProvider {
    static var previews: some View {
        let common = TransactionCommon(type: .withdrawal,
                                    txState: TransactionState(major: .done),
                            amountEffective: Amount(currency: LONGCURRENCY, cent: 10),
                                  amountRaw: Amount(currency: LONGCURRENCY, cent: 20),
                              transactionId: "someTxID",
                                  timestamp: Timestamp(from: 1_666_666_000_000),
                                  txActions: [])
        Group {
            List {
                ThreeAmountsSheet(stack: CallStack("Preview"),
                                 common: common, topAbbrev: "Withdrawal",
                               topTitle: "Withdrawal", baseURL: DEMOEXCHANGE,
                                  large: 1==0, summary: nil, merchant: nil)
                    .safeAreaInset(edge: .bottom) {
                        Button(String(localized: "Accept"), action: {})
                            .buttonStyle(TalerButtonStyle(type: .prominent))
                            .padding(.horizontal)
                            .disabled(true)
                    }
            }
        }
    }
}