SegmentControl.swift (5476B)
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: AccountDetailsArray 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 = 60 // relative to fontSize 20 21 public var body: some View { 22 let cornerRadius: CGFloat = Bool.iOS26 ? 16.0 : 6.0 23 let count = accountDetails.count 24 if count > 0 { 25 ZStack(alignment: .center) { 26 GeometryReader { geo in 27 RoundedRectangle(cornerRadius: cornerRadius) 28 .foregroundColor(WalletColors().pickerSelected(colorScheme, colorSchemeContrast)) 29 .cornerRadius(cornerRadius) 30 .padding(4) 31 .frame(width: geo.size.width / CGFloat(count)) 32 .offset(x: geo.size.width / CGFloat(count) * CGFloat(selectedAccount), y: 0) 33 } 34 .frame(height: frameHeight) 35 36 HStack(spacing: 0) { 37 ForEach((0..<count), id: \.self) { index in 38 let detail = accountDetails[index] 39 let specs = detail.currencySpecification 40 let scope = detail.scope 41 let amount = detail.transferAmount 42 let formatted = amount?.formatted(specs: specs, 43 isNegative: false, 44 scope: scope, 45 useISO: false) 46 ?? (EMPTYSTRING, EMPTYSTRING) 47 let bankName = detail.bankLabel 48 let a11yLabel = bankName != nil ? (bankName! + SPACE + formatted.1) 49 : formatted.1 50 // let _ = print(amountStr) 51 VStack(spacing: 6) { 52 Text(formatted.0) 53 .talerFont(.title3) 54 if let bankName { 55 Text(bankName) 56 .talerFont(.subheadline) 57 } 58 } 59 .accessibilityElement(children: .combine) 60 .accessibilityLabel(a11yLabel) 61 .accessibilityAddTraits(.isButton) 62 .accessibilityAddTraits(index == selectedAccount ? .isSelected : []) 63 .frame(maxWidth: .infinity) 64 .onTapGesture { 65 withAnimation(.easeInOut(duration: 0.150)) { 66 selectedAccount = index 67 } 68 } 69 } 70 } 71 } 72 .onAppear() { 73 if selectedAccount != value { 74 withAnimation { selectedAccount = value } 75 } 76 } 77 .onChange(of: selectedAccount) { selected in 78 action(selected) 79 } 80 .background( 81 RoundedRectangle(cornerRadius: cornerRadius) 82 .fill(WalletColors().sideBackgroundColor) 83 ) 84 } 85 } // body 86 } 87 88 struct SegmentControl2: View { 89 @Binding var value: Int 90 let strings: [String] 91 // let action: (Int) -> Void 92 93 @Environment(\.colorScheme) private var colorScheme 94 @Environment(\.colorSchemeContrast) private var colorSchemeContrast 95 96 public var body: some View { 97 let cornerRadius: CGFloat = Bool.iOS26 ? 16.0 : 6.0 98 let count = strings.count 99 if count > 0 { 100 ZStack(alignment: .center) { 101 GeometryReader { geo in 102 RoundedRectangle(cornerRadius: cornerRadius) 103 .foregroundColor(WalletColors().pickerSelected(colorScheme, colorSchemeContrast)) 104 .cornerRadius(cornerRadius) 105 .padding(4) 106 .frame(width: geo.size.width / CGFloat(count)) 107 .offset(x: geo.size.width / CGFloat(count) * CGFloat(value), y: 0) 108 } 109 110 HStack(spacing: 0) { 111 ForEach((0..<count), id: \.self) { index in 112 let string = strings[index] 113 VStack(spacing: 6) { 114 Text(string) 115 .talerFont(.title3) 116 } 117 .padding(.vertical, 8) 118 .accessibilityElement(children: .combine) 119 // .accessibilityLabel(a11yLabel) 120 .accessibilityAddTraits(.isButton) 121 .accessibilityAddTraits(index == value ? .isSelected : []) 122 .frame(maxWidth: .infinity) 123 .onTapGesture { 124 // action(index) 125 withAnimation(.easeInOut(duration: 0.150)) { 126 value = index 127 } 128 } 129 } 130 } 131 } 132 .background( 133 RoundedRectangle(cornerRadius: cornerRadius) 134 .fill(WalletColors().sideBackgroundColor) 135 ) 136 } 137 } // body 138 }