ScopePicker.swift (11683B)
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 fileprivate func formattedAmount(_ balance: Balance, _ currencyInfo: CurrencyInfo) -> (String, String) { 12 let amount = balance.available 13 return amount.formatted(currencyInfo, isNegative: false, useISO: false) 14 } 15 16 fileprivate func urlOrCurrency(_ balance: Balance) -> String { 17 balance.scopeInfo.url?.trimURL ?? balance.scopeInfo.currency 18 } 19 20 /// The selected index is `@State´ resp. a `@Binding´, and thus can outlive the balances 21 /// array it indexes into - e.g. when an exchange was deleted while this view is on the 22 /// navigation stack. Never subscript `controller.balances´ unchecked. 23 fileprivate func safeBalance(_ balances: [Balance], _ index: Int) -> Balance? { 24 balances.indices.contains(index) ? balances[index] 25 : balances.first 26 } 27 28 fileprivate func pickerRow(_ balance: Balance, _ currencyInfo: CurrencyInfo) -> (String, String) { 29 let formatted = formattedAmount(balance, currencyInfo) 30 let urlOrCurrency = urlOrCurrency(balance) 31 return (String("\(urlOrCurrency):\t\(formatted.0.nbs)"), 32 String("\(urlOrCurrency): \(formatted.1)")) 33 } 34 // MARK: - 35 struct AliasPicker: View { 36 let stack: CallStack 37 let title: String 38 @Binding var value: Int 39 let types: [TaldirAliasType] 40 let action: (Int) -> Void 41 42 @State private var selected = 0 43 44 var body: some View { 45 Picker(title, selection: $selected) { 46 ForEachWithIndex(array: types) { index, aliasType in 47 Text(aliasType.localized) 48 .tag(index) 49 } 50 } 51 .talerFont(.title3) 52 .task() { 53 withAnimation { selected = value } 54 } 55 .onChange(of: selected) { newValue in 56 action(newValue) 57 } 58 } 59 } 60 // MARK: - 61 struct CurrencyPicker: View { 62 let stack: CallStack 63 @Binding var value: Int 64 let exchanges: [DefaultExchange] 65 let action: (Int) -> Void 66 67 @State private var selected = 0 68 69 var body: some View { 70 Picker(EMPTYSTRING, selection: $selected) { 71 ForEachWithIndex(array: exchanges) { index, exchange in 72 if exchange.talerUri.contains("exchange.stage.taler-ops.ch") { 73 Text(exchange.currencySpec.name + " (stage)") 74 .tag(index) 75 } else { 76 Text(exchange.currencySpec.name) 77 .tag(index) 78 } 79 } 80 } 81 .talerFont(.title3) 82 .task() { 83 // FIXME: check balance for non-zero and disable-peer-payments 84 withAnimation { selected = value } 85 } 86 .onChange(of: selected) { newValue in 87 action(newValue) 88 } 89 } 90 } 91 // MARK: - 92 struct ScopePicker: View { 93 // private let symLog = SymLogV(0) 94 let stack: CallStack 95 @Binding var value: Int 96 let onlyNonZero: Bool 97 let action: (Int) -> Void 98 99 @EnvironmentObject private var controller: Controller 100 101 @State private var selected = 0 102 103 var body: some View { 104 #if PRINT_CHANGES 105 let _ = Self._printChanges() 106 #endif 107 let count = controller.balances.count 108 if let balance = safeBalance(controller.balances, selected) { 109 let scope = balance.scopeInfo 110 let currencyInfo = controller.info(for: scope, controller.currencyTicker) 111 let available = scope.isValid ? balance.available : Amount.zero(currency: scope.currency) 112 let availableA11y = available.formatted(currencyInfo, isNegative: false, 113 useISO: true, a11yDecSep: ".") 114 let url = scope.url?.trimURL ?? EMPTYSTRING 115 // let a11yLabel = url + ", " + availableA11y 116 let chooseHint = String(localized: "Choose the payment service:", comment: "a11y") 117 let disabled = (count == 1) 118 119 HStack(alignment: .firstTextBaseline) { 120 Text("via", comment: "ScopePicker") 121 .accessibilityHidden(true) 122 .foregroundColor(disabled ? .secondary : .primary) 123 124 ScopeDropDown(selection: $selected, 125 onlyNonZero: onlyNonZero, 126 disabled: disabled) 127 } 128 .talerFont(.picker) 129 // .accessibilityLabel(a11yLabel) 130 .accessibilityHint(disabled ? EMPTYSTRING : chooseHint) 131 .task() { 132 // FIXME: check balance for non-zero and disable-peer-payments 133 // never hand an out-of-range index to `action´ - the callers subscript 134 // `controller.balances´ with it 135 withAnimation { selected = min(max(value, 0), count - 1) } 136 } 137 .onChange(of: count) { newCount in 138 if selected >= newCount { // balances shrank, e.g. exchange deleted 139 selected = 0 140 } 141 } 142 .onChange(of: selected) { newValue in 143 action(newValue) 144 } 145 } 146 } 147 } 148 // MARK: - 149 struct ScopeDropDown: View { 150 @Binding var selection: Int 151 let onlyNonZero: Bool 152 let disabled: Bool 153 154 // var maxItemDisplayed: Int = 3 155 156 @EnvironmentObject private var controller: Controller 157 158 @State private var scrollPosition: Int? 159 @State private var showDropdown = false 160 161 func buttonAction() { 162 withAnimation { 163 showDropdown.toggle() 164 } 165 } 166 167 @ViewBuilder 168 func dropDownRow(_ balance: Balance, _ currencyInfo: CurrencyInfo, _ first: Bool = false) -> some View { 169 let scope = balance.scopeInfo 170 let urlOrCurrency = urlOrCurrency(balance) 171 let text = Text(urlOrCurrency) 172 let formatted = formattedAmount(balance, currencyInfo) 173 let amountString = Text(formatted.0.nbs) 174 .strikethrough(!scope.isValid, color: WalletColors().attention) 175 176 if first { 177 let a11yLabel = String(localized: "via \(urlOrCurrency)", comment: "a11y") 178 text 179 .accessibilityLabel(a11yLabel) 180 } else { 181 let a11yLabel = "\(urlOrCurrency), \(formatted.1)" 182 let hLayout = HStack(alignment: .firstTextBaseline) { 183 text 184 Spacer() 185 amountString 186 }.padding(.vertical, 4) 187 let vLayout = VStack(alignment: .leading) { 188 text 189 HStack { 190 Spacer() 191 amountString 192 Spacer() 193 } 194 } 195 if #available(iOS 16.4, *) { 196 ViewThatFits(in: .horizontal) { 197 hLayout 198 vLayout 199 } 200 .accessibilityElement(children: .combine) 201 .accessibilityLabel(a11yLabel) 202 } else { 203 vLayout 204 .accessibilityElement(children: .combine) 205 .accessibilityLabel(a11yLabel) 206 } 207 } 208 } 209 210 var body: some View { 211 let radius = 8.0 212 VStack { 213 let chevron = Image(systemName: CHEVRON_UP) // 214 let foreColor = disabled ? Color.secondary : Color.primary 215 let backColor = disabled ? WalletColors().backgroundColor : WalletColors().gray4 216 let selectedBalance = safeBalance(controller.balances, selection) 217 let otherBalances = controller.balances.filter { $0 != selectedBalance } 218 let theList = LazyVStack(spacing: 0) { 219 ForEach(0..<otherBalances.count, id: \.self) { index in 220 let balance = otherBalances[index] 221 let scope = balance.scopeInfo 222 let disablePeerPayments = scope.isValid ? (balance.disablePeerPayments ?? false) : true 223 let rowDisabled = disablePeerPayments || (onlyNonZero ? balance.available.isZero : false) 224 Button(action: { 225 withAnimation { 226 showDropdown.toggle() 227 selection = controller.balances.firstIndex(of: balance) ?? selection 228 } 229 }, label: { 230 let currencyInfo = controller.info(for: balance.scopeInfo, controller.currencyTicker) 231 HStack(alignment: .top) { 232 dropDownRow(balance, currencyInfo) // .border(.random) 233 .foregroundColor(rowDisabled ? .secondary : .primary) 234 Spacer() 235 chevron.foregroundColor(.clear) 236 .accessibilityHidden(true) 237 } 238 }) 239 .disabled(rowDisabled) 240 .padding(.horizontal, radius / 2) 241 .frame(maxWidth: .infinity, alignment: .leading) 242 } 243 } 244 VStack { 245 // selected item 246 if let balance = selectedBalance { 247 let currencyInfo = controller.info(for: balance.scopeInfo, controller.currencyTicker) 248 let noAmount = !showDropdown 249 Group { 250 if disabled { 251 dropDownRow(balance, currencyInfo, noAmount) 252 } else { 253 Button(action: buttonAction) { 254 HStack(alignment: .firstTextBaseline) { 255 dropDownRow(balance, currencyInfo, noAmount) 256 .accessibilityAddTraits(.isSelected) 257 Spacer() 258 chevron.rotationEffect(.degrees((showDropdown ? 0 : -180))) 259 .accessibilityHidden(true) 260 } 261 } 262 } 263 } 264 .padding(.vertical, 4) 265 .padding(.horizontal, radius / 2) 266 .frame(maxWidth: .infinity, alignment: .leading) 267 // .border(.red) 268 } 269 if (showDropdown) { 270 if #available(iOS 17.0, *) { 271 let count = controller.balances.count 272 // let toomany = count > maxItemDisplayed 273 // let scrollViewHeight = buttonHeight * CGFloat(toomany ? maxItemDisplayed 274 // : count) 275 ScrollView { 276 theList 277 .scrollTargetLayout() 278 } 279 // .border(.red) 280 .scrollPosition(id: $scrollPosition) 281 .scrollDisabled(count <= 3) 282 // .frame(height: scrollViewHeight) 283 .onAppear { 284 scrollPosition = selection 285 } 286 } else { 287 // Fallback on earlier versions 288 ScrollView { 289 theList 290 } 291 } 292 293 } 294 } 295 .foregroundStyle(foreColor) 296 .background(RoundedRectangle(cornerRadius: radius).fill(backColor)) 297 } 298 .frame(maxWidth: .infinity, alignment: .top) 299 .zIndex(100) 300 } 301 }