taler-ios

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

WithdrawExchangeV.swift (3283B)


      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 // Called either when scanning a QR code or tapping the provided link, both from the bank's website.
     13 // We show the user the manual withdrawal details in a sheet - but first the ToS must be accepted.
     14 struct WithdrawExchangeV: View {
     15     private let symLog = SymLogV(0)
     16     let stack: CallStack
     17     let selectedBalance: Balance?
     18     var url: URL
     19 
     20     @EnvironmentObject private var controller: Controller
     21     @EnvironmentObject private var model: WalletModel
     22     @State private var amountToTransfer = Amount.zero(currency: EMPTYSTRING)
     23     @State private var exchange: Exchange? = nil
     24     @State private var currencyInfo: CurrencyInfo = CurrencyInfo.zero(UNKNOWN)
     25     @State private var amountLastUsed = Amount.zero(currency: EMPTYSTRING)
     26 
     27     @MainActor
     28     private func viewDidLoad() async {
     29         if exchange == nil {
     30             symLog.log(".task")
     31             if let withdrawExchange = try? await model.prepareWithdrawExchange(url.absoluteString) {
     32                 let baseUrl = withdrawExchange.exchangeBaseUrl
     33                 controller.updateBase(baseUrl, forSaved: url)
     34                 symLog.log("getExchangeByUrl(\(baseUrl))")
     35                 if let exc = try? await model.getExchangeByUrl(url: baseUrl) {
     36                     // let the controller collect CurrencyInfo from this formerly unknown exchange
     37                     let _ = try? await controller.getInfo(from: baseUrl, model: model)
     38                     if let amount = withdrawExchange.amount {
     39                         amountToTransfer = amount
     40                     } else {
     41                         let currency = exc.scopeInfo.currency
     42                         amountToTransfer.setCurrency(currency)
     43                         // is already Amount.zero()
     44                     }
     45                     amountLastUsed.setCurrency(amountToTransfer.currencyStr)
     46                     exchange = exc
     47                 } else {
     48                     exchange = nil
     49                 }
     50             }
     51         }
     52     }
     53 
     54     var body: some View {
     55 #if PRINT_CHANGES
     56         let _ = Self._printChanges()
     57         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     58 #endif
     59         if let exchange {
     60             let scopeInfo = exchange.scopeInfo
     61             Group {
     62                 ManualWithdraw(stack: stack.push(),
     63                                  url: url,
     64                      selectedBalance: selectedBalance,
     65                       amountLastUsed: $amountLastUsed,
     66                     amountToTransfer: $amountToTransfer,
     67                             exchange: exchange,                     // only for withdraw-exchange
     68                  maySwitchCurrencies: false,
     69                              isSheet: true)
     70             }
     71             .task(id: controller.currencyTicker) {
     72                 currencyInfo = controller.info(for: scopeInfo, controller.currencyTicker)
     73             }
     74         } else {
     75             let message = String(localized: "No payment service...", comment: "loading")
     76             LoadingView(stack: stack.push(), scopeInfo: nil, message: message)
     77                 .task { await viewDidLoad() }
     78         }
     79     }
     80 }