commit d6cdaf72291144bf73ef00aab2545bdda5a75a53
parent 1ae02f3ae0d556e9a1481c2f88149d753641ef24
Author: Marc Stibane <marc@taler.net>
Date: Sat, 23 Mar 2024 18:50:57 +0100
withdraw-exchange fetches currency from the exchange if it wasn't in the URL
Diffstat:
4 files changed, 56 insertions(+), 30 deletions(-)
diff --git a/TalerWallet1/Views/Banking/ExchangeRowView.swift b/TalerWallet1/Views/Banking/ExchangeRowView.swift
@@ -24,6 +24,7 @@ struct ExchangeRowView: View {
@EnvironmentObject private var model: WalletModel
@AppStorage("minimalistic") var minimalistic: Bool = false
@State private var buttonSelected: Int? = nil
+ @State var myExchange: Exchange? = nil
func selectAndUpdate(_ button: Int) {
amountToTransfer.setCurrency(currency)
@@ -57,7 +58,7 @@ struct ExchangeRowView: View {
let withdrawTitle1 = String(localized: "Withdraw\t\(currency)",
comment: "Button `Withdraw (currency)ยด, must have ONE \\t and ONE %@")
let baseURL = exchange.exchangeBaseUrl
-
+
let deposit = LazyView {
DepositIbanV(stack: stack.push(),
feeLabel: nil,
@@ -70,6 +71,7 @@ struct ExchangeRowView: View {
let manualWithdraw = LazyView {
ManualWithdraw(stack: stack.push(), isSheet: false,
exchangeBaseUrl: baseURL,
+ exchange: $myExchange,
amountToTransfer: $amountToTransfer)
}
let showToS = LazyView {
@@ -117,6 +119,9 @@ struct ExchangeRowView: View {
}
}
.task {
+ if myExchange == nil {
+ myExchange = exchange
+ }
if controller.hasInfo(for: currency) == nil {
symLog.log("fetching info for \(currency)")
// FIXME: remove fake ScopeInfo once the REAL one is in exchange.scopeInfo
diff --git a/TalerWallet1/Views/Banking/ExchangeSectionView.swift b/TalerWallet1/Views/Banking/ExchangeSectionView.swift
@@ -21,15 +21,17 @@ struct ExchangeSectionView: View {
@State private var shouldReloadBalances: Int = 0
- func amountAvailable(_ exchangeBaseUrl: String, currency: String?) -> Amount? {
- for balance in balances {
- if let baseUrl = balance.scopeInfo.url {
- if baseUrl == exchangeBaseUrl {
- return balance.available
- }
- } else if let currency {
- if currency == balance.scopeInfo.currency {
- return balance.available
+ func amountAvailable(_ exchange: Exchange?, currency: String?) -> Amount? {
+ if let exchange {
+ for balance in balances {
+ if let baseUrl = balance.scopeInfo.url {
+ if baseUrl == exchange.exchangeBaseUrl {
+ return balance.available
+ }
+ } else if let currency {
+ if currency == balance.scopeInfo.currency {
+ return balance.available
+ }
}
}
}
@@ -51,8 +53,7 @@ struct ExchangeSectionView: View {
// ForEach(exchanges) { exchange in
ExchangeRowView(stack: stack.push(),
exchange: exchange,
- amountAvailable: amountAvailable(exchange.exchangeBaseUrl,
- currency: currency),
+ amountAvailable: amountAvailable(exchange, currency: currency),
// depositIBAN: $depositIBAN,
// accountHolder: $accountHolder,
currency: currencyName, // TODO: (balance.available) amount.isZero to disable Deposit-button
diff --git a/TalerWallet1/Views/Banking/ManualWithdraw.swift b/TalerWallet1/Views/Banking/ManualWithdraw.swift
@@ -16,6 +16,7 @@ struct ManualWithdraw: View {
let stack: CallStack
let isSheet: Bool
let exchangeBaseUrl: String
+ @Binding var exchange: Exchange?
@Binding var amountToTransfer: Amount
@EnvironmentObject private var controller: Controller
@@ -23,7 +24,6 @@ struct ManualWithdraw: View {
@AppStorage("minimalistic") var minimalistic: Bool = false
@State private var withdrawalAmountDetails: WithdrawalAmountDetails? = nil
- @State private var exchange: Exchange? = nil
// @State var ageMenuList: [Int] = []
// @State var selectedAge = 0
@@ -133,10 +133,18 @@ struct ManualWithdraw: View {
struct ManualWithdraw_Previews: PreviewProvider {
struct StateContainer : View {
@State private var amountToPreview = Amount(currency: LONGCURRENCY, cent: 510)
+ @State private var exchange: Exchange? = Exchange(exchangeBaseUrl: DEMOEXCHANGE,
+ scopeInfo: ScopeInfo(type: .exchange, currency: LONGCURRENCY),
+ paytoUris: [],
+ tosStatus: .accepted,
+ exchangeEntryStatus: .ephemeral,
+ exchangeUpdateStatus: .ready,
+ ageRestrictionOptions: [])
var body: some View {
ManualWithdraw(stack: CallStack("Preview"), isSheet: false,
exchangeBaseUrl: DEMOEXCHANGE,
+ exchange: $exchange,
amountToTransfer: $amountToPreview)
}
}
diff --git a/TalerWallet1/Views/Sheets/WithdrawExchangeV.swift b/TalerWallet1/Views/Sheets/WithdrawExchangeV.swift
@@ -17,8 +17,8 @@ struct WithdrawExchangeV: View {
@EnvironmentObject private var controller: Controller
@EnvironmentObject private var model: WalletModel
- @State private var exchangeBaseUrl: String?
@State private var amountToTransfer = Amount.zero(currency: "")
+ @State private var exchange: Exchange? = nil
var body: some View {
#if PRINT_CHANGES
@@ -26,9 +26,10 @@ struct WithdrawExchangeV: View {
let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear
#endif
Group {
- if let exchangeBaseUrl {
+ if exchange != nil {
ManualWithdraw(stack: stack.push(), isSheet: true,
- exchangeBaseUrl: exchangeBaseUrl,
+ exchangeBaseUrl: exchange!.exchangeBaseUrl,
+ exchange: $exchange,
amountToTransfer: $amountToTransfer)
} else {
// TODO: Error
@@ -36,21 +37,32 @@ struct WithdrawExchangeV: View {
}
}
.task {
- do { // TODO: cancelled
- symLog.log(".task")
- let withdrawExchange = try await model.loadWithdrawalExchangeForUriM(url.absoluteString)
- let baseUrl = withdrawExchange.exchangeBaseUrl
- exchangeBaseUrl = baseUrl
- if let amount = withdrawExchange.amount {
- amountToTransfer = amount
- } else {
- // is already Amount.zero(currency: "")
+ if exchange == nil {
+ do { // TODO: cancelled
+ symLog.log(".task")
+ let withdrawExchange = try await model.loadWithdrawalExchangeForUriM(url.absoluteString)
+ let baseUrl = withdrawExchange.exchangeBaseUrl
+ symLog.log("getExchangeByUrl(\(baseUrl))")
+ if let exc = await model.getExchangeByUrl(url: baseUrl) {
+ // let the controller collect CurrencyInfo from this formerly unknown exchange
+ let _ = await controller.getInfo(from: baseUrl, model: model)
+ if let amount = withdrawExchange.amount {
+ amountToTransfer = amount
+ } else {
+ let currency = exc.scopeInfo?.currency
+ ?? exc.currency
+ ?? String(localized: "Unknown", comment: "unknown currency")
+ amountToTransfer.setCurrency(currency)
+ // is already Amount.zero(currency: "")
+ }
+ exchange = exc
+ } else {
+ // TODO: Error "Can't get Exchange / Payment Service Provider Info"
+ }
+ } catch { // TODO: error
+ symLog.log(error.localizedDescription)
+ exchange = nil
}
- // let the controller collect CurrencyInfo from this formerly unknown exchange
- let _ = await controller.getInfo(from: baseUrl, model: model)
- } catch { // TODO: error
- symLog.log(error.localizedDescription)
- exchangeBaseUrl = nil
}
}
}