WithdrawAcceptView.swift (7729B)
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 import SymLog 11 12 struct WithdrawAcceptView: View { 13 private let symLog = SymLogV(0) 14 let stack: CallStack 15 let navTitle = String(localized: "Withdrawal") 16 17 // the URL from the bank website 18 let url: URL 19 let scope: ScopeInfo 20 @Binding var amountToTransfer: Amount 21 @Binding var wireFee: Amount? 22 @Binding var exchange: Exchange? // user can select one from possibleExchanges 23 24 @EnvironmentObject private var controller: Controller 25 @EnvironmentObject private var model: WalletModel 26 @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic 27 28 @State private var withdrawalDetails: WithdrawalDetailsForAmount? = nil 29 30 @MainActor 31 private func viewDidLoad() async { 32 symLog.log(".task \(exchange?.id ?? "nil")") 33 if !amountToTransfer.isZero, let exchange { 34 if let details = try? await model.getWithdrawalDetailsForAmount(amountToTransfer, 35 baseUrl: exchange.exchangeBaseUrl, 36 scope: nil) { // TODO: scope 37 withdrawalDetails = details 38 } 39 // agePicker.setAges(ages: details?.ageRestrictionOptions) 40 } else { // TODO: error 41 symLog.log("no exchangeBaseUrl or no exchange") 42 withdrawalDetails = nil 43 } 44 } 45 46 var body: some View { 47 #if PRINT_CHANGES 48 let _ = Self._printChanges() 49 let _ = symLog.vlog(scope.url ?? amountToTransfer.readableDescription) // just to get the # 50 #endif 51 ZStack { // there is only _one_ Z item ever - use ZStack to ensure .task is run only _once_ when exchange is switched 52 if let exchange2 = exchange { // there should always be an exchange... 53 VStack { 54 // missingTos could mean "not known yet" - only accept it after exchange is ready 55 let isFetched = exchange2.exchangeUpdateStatus == .ready 56 || exchange2.exchangeUpdateStatus == .readyUpdate 57 let tosAccepted = exchange2.tosStatus == .accepted 58 || (exchange2.tosStatus == .missingTos && isFetched) // no ToS to accept 59 if !tosAccepted { 60 ToSButtonView(stack: stack.push(), 61 exchangeBaseUrl: exchange2.exchangeBaseUrl, 62 viewID: SHEET_WITHDRAW_TOS, 63 shouldDismiss: false, // pop back to here 64 p2p: false) 65 } 66 List { 67 if let withdrawalDetails { 68 let raw = withdrawalDetails.amountRaw 69 let effective = withdrawalDetails.amountEffective 70 let currency = raw.currencyStr 71 let fee = try! Amount.diff(raw, effective) 72 let outColor = WalletColors().transactionColor(false) 73 let inColor = WalletColors().transactionColor(true) 74 75 ThreeAmountsSection(stack: stack.push(), 76 scope: scope, 77 topTitle: String(localized: "Chosen amount to withdraw:"), 78 topAbbrev: String(localized: "Withdraw:", comment: "Chosen amount to withdraw:"), 79 topAmount: raw, 80 noFees: exchange2.noFees, 81 fee: fee, 82 feeIsNegative: true, 83 bottomTitle: String(localized: "Amount to obtain:"), 84 bottomAbbrev: String(localized: "Obtain:", comment: "Amount to obtain:"), 85 bottomAmount: effective, 86 large: false, 87 pendingDialog: false, 88 isDone: false, 89 incoming: true, 90 baseURL: exchange2.exchangeBaseUrl, 91 txStateLcl: nil, // common.txState.major.localizedState 92 header: nil) 93 if let wireFee { 94 if !wireFee.isZero { 95 let currencyInfo = controller.info(for: scope, controller.currencyTicker) 96 let feeStr = wireFee.formatted(currencyInfo, isNegative: false) 97 let feeTitle = String(localized: "FEE_WITHDRAW_BANK_WIRE", 98 defaultValue: "Your bank's wire fee: \(feeStr.0)") 99 let feeA11y = String(localized: "FEE_WITHDRAW_BANK_WIRE_A11Y", 100 defaultValue: "Your bank's wire fee: \(feeStr.1)", comment: "a11y") 101 Text(feeTitle) 102 .accessibilityLabel(Text(feeA11y)) 103 } 104 } 105 } else { 106 Section { 107 Text("The amount will be determined by the Cash Acceptor.") 108 } 109 } 110 } 111 .listStyle(myListStyle.style).anyView 112 .onChange(of: controller.exchanges) { _ in 113 Task { 114 if let someExchange = await controller.exchange(for: exchange2.exchangeBaseUrl) { 115 self.exchange = someExchange 116 } 117 } 118 } 119 .navigationTitle(navTitle) 120 .safeAreaInset(edge: .bottom) { 121 if tosAccepted { 122 let destination = WithdrawAcceptDone(stack: stack.push(), 123 // scope: scope, 124 exchangeBaseUrl: exchange2.exchangeBaseUrl, 125 url: url, 126 amountToTransfer: amountToTransfer.isZero ? nil : amountToTransfer) 127 NavigationLink(destination: destination) { 128 Text("Confirm Withdrawal") // SHEET_WITHDRAW_ACCEPT 129 } 130 .buttonStyle(TalerButtonStyle(type: .prominent)) 131 .accessibilityIdentifier(A11yID.withdrawAcceptButton) 132 .padding(.horizontal) 133 } 134 } 135 } 136 .onAppear() { 137 symLog.log("onAppear") 138 DebugViewC.shared.setSheetID(SHEET_WITHDRAW_ACCEPT, // 132 WithdrawAcceptView 139 stack: stack.push()) 140 } 141 } else { // no exchange - should not happen 142 #if DEBUG 143 let message = url.host 144 #else 145 let message: String? = nil 146 #endif 147 let _ = symLog.log("Loading") 148 LoadingView(stack: stack.push(), scopeInfo: nil, message: message) 149 } 150 } 151 .task(id: exchange?.id) { // re-run this whenever the user switches the exchange 152 await viewDidLoad() 153 } 154 } 155 }