taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

AmountRowV.swift (3489B)


      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 // Title and Amount
     12 struct AmountRowV: View {
     13     let stack: CallStack?
     14     let title: String
     15     let amount: Amount
     16     let scope: ScopeInfo?
     17     let isNegative: Bool?        // show fee with minus (or plus) sign, or no sign if nil
     18     let color: Color
     19     let large: Bool      // set to false for QR or IBAN
     20 
     21     var body: some View {
     22         let titleV = Text(title)
     23                         .multilineTextAlignment(.leading)
     24                         .talerFont(.body)
     25         let amountV = AmountV(stack: stack?.push(),
     26                               scope: scope,
     27                              amount: amount,
     28                          isNegative: isNegative,
     29                       strikethrough: false,
     30                               large: large)
     31                         .foregroundColor(color)
     32         let verticalV = VStack(alignment: .leading) {
     33             titleV
     34             HStack(alignment: .lastTextBaseline) {
     35                 Spacer(minLength: 2)
     36                 amountV
     37             }
     38         }
     39         Group {
     40             if #available(iOS 16.4, *) {
     41                 ViewThatFits(in: .horizontal) {
     42                     HStack(alignment: .lastTextBaseline) {
     43                         titleV//.border(.orange)
     44                         Spacer(minLength: 2)
     45                         amountV//.border(.gray)
     46                     }
     47                     HStack(alignment: .lastTextBaseline) {
     48                         titleV//.border(.blue)
     49                             .lineLimit(2, reservesSpace: true)
     50                             .fixedSize(horizontal: false, vertical: true)
     51                         Spacer(minLength: 2)
     52                         amountV//.border(.gray)
     53                     }
     54                     verticalV
     55                 }
     56             } else { // view for iOS 15
     57                 verticalV
     58             }
     59         }
     60             .frame(maxWidth: .infinity, alignment: .leading)
     61             .accessibilityElement(children: .combine)
     62             .listRowSeparator(.hidden)
     63     }
     64 }
     65 extension AmountRowV {
     66     init(_ title: String, amount: Amount, scope: ScopeInfo?, isNegative: Bool, color: Color) {
     67         self.stack = nil
     68         self.title = title
     69         self.amount = amount
     70         self.scope = scope
     71         self.isNegative = isNegative
     72         self.color = color
     73         self.large = true
     74     }
     75 }
     76 
     77 // MARK: -
     78 fileprivate func talerFromStr(_ from: String) -> Amount {
     79     do {
     80         let amount = try Amount(fromString: from)
     81         return amount
     82     } catch {
     83         return Amount(currency: "Taler", cent: 480)
     84     }
     85 }
     86 
     87 #if DEBUG
     88 @MainActor
     89 fileprivate struct BindingViewContainer: View {
     90     @State private var previewD: CurrencyInfo = CurrencyInfo.zero(DEMOCURRENCY)
     91     var body: some View {
     92         let scope = ScopeInfo.zero(DEMOCURRENCY)
     93         let fee = Amount(currency: DEMOCURRENCY, cent: 20)
     94         AmountRowV("Fee", amount: fee, scope: scope, isNegative: true, color: Color("Outgoing"))
     95         let cents = Amount(currency: DEMOCURRENCY, cent: 480)
     96         AmountRowV("Cents", amount: cents, scope: scope, isNegative: false, color: Color("Incoming"))
     97         let amount = talerFromStr("Taler:4.80")
     98         AmountRowV("Chosen amount to withdraw", amount: amount, scope: scope, isNegative: false, color: Color("Incoming"))
     99     }
    100 }
    101 
    102 #Preview {
    103     List {
    104         BindingViewContainer()
    105     }
    106 }
    107 #endif