AmountV.swift (4905B)
1 /* 2 * This file is part of GNU Taler, ©2022-26 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 AmountV: View { 12 let stack: CallStack? 13 let scope: ScopeInfo? 14 let amount: Amount 15 let isNegative: Bool? // show a "-" or "+" before the amount, nothing for nil 16 let useISO: Bool 17 let strikethrough: Bool 18 let large: Bool // set to false for QR or IBAN 19 let xLarge: Bool 20 let a11yDecSep: String? 21 22 @EnvironmentObject private var controller: Controller 23 24 @State private var currencyInfo: CurrencyInfo? 25 @State private var showBanknotes = false 26 27 private var dismissAlertButton: some View { 28 Button("Cancel", role: .cancel) { 29 showBanknotes = false 30 } 31 } 32 33 private func currencyTickerChanged() async { 34 if let scope { 35 // print("currencyTickerChanged: \(amount.readableDescription)") 36 currencyInfo = controller.info(for: scope) // might be nil! 37 } 38 } 39 40 private func amountStr(_ currencyInfo : CurrencyInfo?) -> (String, String) { 41 let dontShow = (isNegative == nil) 42 let showSign: Bool = isNegative ?? false 43 let readable = amount.readableDescription 44 let amountFormatted: (String, String) 45 if let currencyInfo { 46 amountFormatted = amount.formatted(currencyInfo, isNegative: false, 47 useISO: useISO, a11yDecSep: a11yDecSep) 48 } else { 49 amountFormatted = (readable, readable) 50 } 51 let amountStr = dontShow ? amountFormatted.0 52 : showSign ? "- \(amountFormatted.0)" 53 : "+ \(amountFormatted.0)" 54 let amountA11y = dontShow ? amountFormatted.1 55 : showSign ? "- \(amountFormatted.1)" 56 : "+ \(amountFormatted.1)" 57 58 return (amountStr, amountA11y) 59 } 60 61 var body: some View { 62 let amountTuple = amountStr(currencyInfo) 63 Text(amountTuple.0) 64 .strikethrough(strikethrough, color: WalletColors().attention) 65 .multilineTextAlignment(.center) 66 .talerFont(xLarge ? .largeTitle 67 : large ? .title : .title2) 68 // .fontWeight(large ? .medium : .regular) // @available(iOS 16.0, *) 69 .monospacedDigit() 70 .accessibilityLabel(amountTuple.1) // TODO: locale.leadingCurrencySymbol 71 .privacySensitive() 72 .layoutPriority(10) 73 .task(id: controller.currencyTicker) { await currencyTickerChanged() } 74 // .onLongPressGesture(minimumDuration: 0.3) { 75 // showValue = true 76 // } 77 // .alert("Bla", 78 // isPresented: $showBanknotes, 79 // actions: { dismissAlertButton }, 80 // message: { Text("Blub") }) 81 } 82 } 83 extension AmountV { 84 init(_ scope: ScopeInfo?, _ amount: Amount) { 85 self.init(scope, amount, isNegative: false) 86 } 87 init(_ scope: ScopeInfo?, _ amount: Amount, isNegative: Bool?) { 88 self.init(scope, amount, isNegative: isNegative, strikethrough: false) 89 } 90 init(_ currency: String, cent: UInt64, isNegative: Bool?) { 91 self.stack = nil 92 self.scope = nil 93 self.amount = Amount(currency: currency , cent: cent) 94 self.isNegative = isNegative 95 self.useISO = false 96 self.strikethrough = false 97 self.large = true 98 self.xLarge = false 99 self.a11yDecSep = nil 100 } 101 init(_ currency: String, amount: Amount, isNegative: Bool?) { // for OIM 102 self.stack = nil 103 self.scope = nil 104 self.amount = Amount(currency: currency , integer: amount.integer, fraction: amount.fraction) 105 self.isNegative = isNegative 106 self.useISO = false 107 self.strikethrough = false 108 self.large = true 109 self.xLarge = true 110 self.a11yDecSep = nil 111 } 112 init(_ scope: ScopeInfo?, _ amount: Amount, isNegative: Bool?, strikethrough: Bool) { 113 self.stack = nil 114 self.scope = scope 115 self.amount = amount 116 self.isNegative = isNegative 117 self.useISO = false 118 self.strikethrough = strikethrough 119 self.large = false 120 self.xLarge = false 121 self.a11yDecSep = nil 122 } 123 init(stack: CallStack?, scope: ScopeInfo?, amount: Amount, isNegative: Bool? = nil, 124 strikethrough: Bool = false, large: Bool = false) { 125 self.stack = stack 126 self.scope = scope 127 self.amount = amount 128 self.isNegative = isNegative 129 self.useISO = false 130 self.strikethrough = strikethrough 131 self.large = false 132 self.xLarge = false 133 self.a11yDecSep = nil 134 } 135 } 136 // MARK: - 137 //#Preview { 138 // AmountV() 139 //}