ThreeAmountsSection.swift (10673B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import SwiftUI 9 import taler_swift 10 11 struct ThreeAmountsSheet: View { // should be in a separate file 12 let stack: CallStack 13 let scope: ScopeInfo? 14 var common: TransactionCommon 15 var topAbbrev: String 16 var topTitle: String 17 var bottomTitle: String? 18 var bottomAbbrev: String? 19 let baseURL: String? 20 let noFees: Bool? // true if exchange charges no fees at all 21 var feeIsNegative: Bool? // show fee with minus (or plus) sign, or no sign if nil 22 let large: Bool // set to false for QR or IBAN 23 let header: String? 24 25 #if DEBUG 26 @AppStorage("developerMode") var developerMode: Bool = true 27 #else 28 @AppStorage("developerMode") var developerMode: Bool = false 29 #endif 30 31 var body: some View { 32 let incoming = common.isIncoming 33 let pending = common.isPending || common.isFinalizing 34 let dialog = common.isDialog 35 let isDone = common.isDone 36 let incomplete = !(isDone || pending || dialog) 37 let raw = common.amountRaw 38 let effective: Amount? = incomplete ? nil : common.amountEffective 39 let fee: Amount? = incomplete ? nil : common.fee() 40 41 let defaultBottomTitle = incoming ? (pending ? String(localized: "Pending amount to obtain:") 42 : String(localized: "Obtained amount:") ) 43 : (pending ? String(localized: "Amount to pay:") 44 : String(localized: "Paid amount:") ) 45 let defaultBottomAbbrev = incoming ? (pending ? String(localized: "Pending:", comment: "mini") 46 : String(localized: "Obtained:", comment: "mini") ) 47 : (pending ? String(localized: "Pay:", comment: "mini") 48 : String(localized: "Paid:", comment: "mini") ) 49 let majorLcl = common.txState.major.localizedState 50 let txStateLcl = developerMode && pending ? (common.txState.minor?.localizedState ?? majorLcl) 51 : majorLcl 52 ThreeAmountsSection(stack: stack.push(), 53 scope: scope, 54 topTitle: topTitle, 55 topAbbrev: topAbbrev, 56 topAmount: raw, 57 noFees: noFees, 58 fee: fee, 59 feeIsNegative: feeIsNegative, 60 bottomTitle: bottomTitle ?? defaultBottomTitle, 61 bottomAbbrev: bottomAbbrev ?? defaultBottomAbbrev, 62 bottomAmount: effective, 63 large: large, 64 pendingDialog: pending || dialog, 65 isDone: isDone, 66 incoming: incoming, 67 baseURL: baseURL, 68 txStateLcl: txStateLcl, 69 header: header) 70 } 71 } 72 73 struct ProductImage: Codable, Hashable { 74 var imageBase64: String 75 76 var image: Image? { 77 Image(imageBase64) 78 } 79 } 80 81 // MARK: - 82 struct ThreeAmountsSection: View { 83 let stack: CallStack 84 let scope: ScopeInfo? 85 var topTitle: String 86 var topAbbrev: String 87 var topAmount: Amount 88 let noFees: Bool? // true if exchange charges no fees at all 89 var fee: Amount? // nil = don't show fee line, zero = no fee for this tx 90 var feeIsNegative: Bool? // show fee with minus (or plus) sign, or no sign if nil 91 var bottomTitle: String 92 var bottomAbbrev: String 93 var bottomAmount: Amount? // nil = incomplete (aborted, timed out) 94 let large: Bool 95 let pendingDialog: Bool 96 let isDone: Bool 97 let incoming: Bool 98 let baseURL: String? 99 let txStateLcl: String? // localizedState 100 let header: String? 101 102 @EnvironmentObject private var controller: Controller 103 @Environment(\.colorScheme) private var colorScheme 104 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 105 @AppStorage("minimalistic") var minimalistic: Bool = false 106 107 @State private var currencyInfo: CurrencyInfo = CurrencyInfo.zero(UNKNOWN) 108 109 @MainActor 110 private func viewDidLoad() async { 111 if let scope { 112 currencyInfo = controller.info(for: scope) ?? CurrencyInfo.zero(UNKNOWN) 113 } 114 } 115 116 117 var body: some View { 118 let currency = currencyInfo.currency 119 let labelColor = WalletColors().labelColor 120 let bottomColor = pendingDialog ? WalletColors().pendingColor(incoming) 121 : WalletColors().transactionColor(incoming) 122 let hasNoFees = noFees ?? false 123 Section { 124 if currency != "UNKNOWN" { 125 if pendingDialog || isDone { 126 Text(pendingDialog ? "Payment will be made in \(currency)" 127 : "Payment was made in \(currency)") 128 .talerFont(.callout) 129 .padding(.top, 4) 130 } 131 AmountRowV(stack: stack.push(), 132 title: minimalistic ? topAbbrev : topTitle, 133 amount: topAmount, 134 scope: scope, 135 isNegative: nil, 136 color: labelColor, 137 large: false) 138 .padding(.bottom, 4) 139 if hasNoFees == false { // otherwise raw==effective 140 if let fee { 141 let title = minimalistic ? String(localized: "Exchange fee (short):", defaultValue: "Fee:", comment: "short version") 142 : String(localized: "Exchange fee (long):", defaultValue: "Fee:", comment: "long version") 143 AmountRowV(stack: stack.push(), 144 title: title, 145 amount: fee, 146 scope: scope, 147 isNegative: fee.isZero ? nil : feeIsNegative, 148 color: labelColor, 149 large: false) 150 .padding(.bottom, 4) 151 } 152 if let bottomAmount { 153 AmountRowV(stack: stack.push(), 154 title: minimalistic ? bottomAbbrev : bottomTitle, 155 amount: bottomAmount, 156 scope: scope, 157 isNegative: nil, 158 color: bottomColor, 159 large: large) 160 } 161 } 162 let serviceURL = scope?.url ?? baseURL 163 if let serviceURL { 164 VStack(alignment: .leading) { 165 // TODO: "Issued by" for withdrawals 166 Text(minimalistic ? "Payment service:" : "Using payment service:") 167 .multilineTextAlignment(.leading) 168 .talerFont(.body) 169 Text(serviceURL.trimURL) 170 .frame(maxWidth: .infinity, alignment: .trailing) 171 .multilineTextAlignment(.center) 172 .talerFont(large ? .title3 : .body) 173 // .fontWeight(large ? .medium : .regular) // @available(iOS 16.0, *) 174 .foregroundColor(labelColor) 175 } 176 .padding(.top, 4) 177 .frame(maxWidth: .infinity, alignment: .leading) 178 .listRowSeparator(.hidden) 179 .accessibilityElement(children: .combine) 180 } 181 } 182 } header: { 183 if let header = header ?? scope?.url?.trimURL ?? scope?.currency { 184 Text(header) 185 .talerFont(.title3) 186 .foregroundColor(WalletColors().secondary2(colorScheme, colorSchemeContrast)) 187 } 188 } 189 .task { await viewDidLoad() } 190 .onChange(of: scope) { newVal in 191 if let newVal { 192 currencyInfo = controller.info(for: newVal) ?? CurrencyInfo.zero(UNKNOWN) 193 } 194 } 195 } 196 } 197 // MARK: - 198 #if DEBUG 199 struct ThreeAmounts_Previews: PreviewProvider { 200 @MainActor 201 struct StateContainer: View { 202 // @State private var previewD: CurrencyInfo = CurrencyInfo.zero(DEMOCURRENCY) 203 // @State private var previewT: CurrencyInfo = CurrencyInfo.zero(TESTCURRENCY) 204 205 var body: some View { 206 let scope = ScopeInfo.zero(LONGCURRENCY) 207 let common = TransactionCommon(type: .withdrawal, 208 transactionId: "someTxID", 209 timestamp: Timestamp(from: 1_666_666_000_000), 210 scopes: [scope], 211 txState: TransactionState(major: .done), 212 txActions: [], 213 amountRaw: Amount(currency: LONGCURRENCY, cent: 20), 214 amountEffective: Amount(currency: LONGCURRENCY, cent: 10)) 215 // let test = Amount(currency: TESTCURRENCY, cent: 123) 216 // let demo = Amount(currency: DEMOCURRENCY, cent: 123456) 217 List { 218 ThreeAmountsSheet(stack: CallStack("Preview"), 219 scope: scope, 220 common: common, 221 topAbbrev: "Withdrawal", 222 topTitle: "Withdrawal", 223 baseURL: DEMOEXCHANGE, 224 noFees: false, 225 large: false, 226 header: nil) 227 .safeAreaInset(edge: .bottom) { 228 Button(String("Preview")) {} 229 .buttonStyle(TalerButtonStyle(type: .prominent)) 230 .padding(.horizontal) 231 .disabled(true) 232 } 233 } 234 } 235 } 236 237 static var previews: some View { 238 StateContainer() 239 // .environment(\.sizeCategory, .extraExtraLarge) Canvas Device Settings 240 } 241 } 242 #endif