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