taler-ios

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

SegmentControl.swift (3251B)


      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 
     10 struct SegmentControl: View {
     11     @Binding var value: Int
     12     let accountDetails: [ExchangeAccountDetails]
     13     let action: (Int) -> Void
     14 
     15     @Environment(\.colorScheme) private var colorScheme
     16     @Environment(\.colorSchemeContrast) private var colorSchemeContrast
     17 
     18     @State private var selectedAccount = 0
     19     @ScaledMetric var frameHeight = 80       // relative to fontSize
     20 
     21     public var body: some View {
     22         let count = accountDetails.count
     23         ZStack(alignment: .center) {
     24             GeometryReader { geo in
     25                 RoundedRectangle(cornerRadius: 6.0)
     26                     .foregroundColor(WalletColors().pickerSelected(colorScheme, colorSchemeContrast))
     27                     .cornerRadius(6.0)
     28                     .padding(4)
     29                     .frame(width: geo.size.width / CGFloat(count))
     30                     .offset(x: geo.size.width / CGFloat(count) * CGFloat(selectedAccount), y: 0)
     31             }
     32             .frame(height: frameHeight)
     33 
     34             HStack(spacing: 0) {
     35                 ForEach((0..<count), id: \.self) { index in
     36                     let detail = accountDetails[index]
     37                     let specs = detail.currencySpecification
     38                     let scope = detail.scope
     39                     let amount = detail.transferAmount
     40                     let formatted = amount?.formatted(specs: specs,
     41                                                  isNegative: false,
     42                                                       scope: scope,
     43                                                      useISO: false)
     44                                     ?? (EMPTYSTRING, EMPTYSTRING)
     45                     let bankName = detail.bankLabel
     46                     let a11yLabel = bankName != nil ? (bankName! + SPACE + formatted.1)
     47                                                     : formatted.1
     48 //            let _ = print(amountStr)
     49                     VStack(spacing: 6) {
     50                         Text(formatted.0)
     51                             .talerFont(.title3)
     52                         if let bankName {
     53                             Text(bankName)
     54                                 .talerFont(.subheadline)
     55                         }
     56                     }
     57                     .accessibilityElement(children: .combine)
     58                     .accessibilityLabel(a11yLabel)
     59                     .accessibilityAddTraits(.isButton)
     60                     .accessibilityAddTraits(index == selectedAccount ? .isSelected : [])
     61                     .frame(maxWidth: .infinity)
     62                     .onTapGesture {
     63                         withAnimation(.easeInOut(duration: 0.150)) {
     64                             selectedAccount = index
     65                         }
     66                     }
     67                 }
     68             }
     69         }
     70         .onAppear() {
     71             if selectedAccount != value {
     72                 withAnimation { selectedAccount = value }
     73             }
     74         }
     75         .onChange(of: selectedAccount) { selected in
     76             action(selected)
     77         }
     78         .background(
     79             RoundedRectangle(cornerRadius: 6.0)
     80                 .fill(WalletColors().sideBackground)
     81         )
     82     }
     83 }