taler-ios

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

commit a58f2d9b9369cf2e3e64081ca92cc71fd717694e
parent 7f5147ac7615f5b556449a7abc02e9ae1854c3d8
Author: Marc Stibane <marc@taler.net>
Date:   Sun, 21 Jul 2024 18:35:53 +0200

computeFee

Diffstat:
MTalerWallet1/Views/Peer2peer/RequestPayment.swift | 122++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
MTalerWallet1/Views/Peer2peer/SendAmount.swift | 116++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
MTalerWallet1/Views/Sheets/WithdrawBankIntegrated/WithdrawURIView.swift | 8++++++++
3 files changed, 169 insertions(+), 77 deletions(-)

diff --git a/TalerWallet1/Views/Peer2peer/RequestPayment.swift b/TalerWallet1/Views/Peer2peer/RequestPayment.swift @@ -22,14 +22,20 @@ struct RequestPayment: View { @State private var peerPullCheck: CheckPeerPullCreditResponse? = nil @State private var expireDays: UInt = 0 + @State private var feeAmount: Amount? = nil @State private var buttonSelected = false + @State private var shortcutSelected = false @State private var amountShortcut = Amount.zero(currency: EMPTYSTRING) // Update currency when used @State private var exchange: Exchange? = nil // wg. noFees private func shortcutAction(_ shortcut: Amount) { amountShortcut = shortcut + shortcutSelected = true + } + private func buttonAction() { buttonSelected = true } + private func feeIsNotZero() -> Bool? { if let hasNoFees = exchange?.noFees { if hasNoFees { @@ -39,6 +45,24 @@ struct RequestPayment: View { return peerPullCheck != nil ? true : false } + private func computeFeeRequest(_ amount: Amount) async -> ComputeFeeResult? { + if exchange == nil { + if let url = scopeInfo.url { + exchange = try? await model.getExchangeByUrl(url: url) + } + } + if amount.isZero { +// fee = EMPTYSTRING + } else { + let baseURL = exchange?.exchangeBaseUrl + peerPullCheck = try? await model.checkPeerPullCreditM(baseURL, amount: amount, + cancellationId: "cancel") + +// return ComputeFeeResult(insufficient: false, feeAmount: <#T##Amount?#>, feeStr: <#T##String#>) + } + return nil + } + var body: some View { #if PRINT_CHANGES let _ = Self._printChanges() @@ -52,6 +76,8 @@ struct RequestPayment: View { comment: "NavTitle: Request 'currencySymbol'") let coinData = CoinData(details: peerPullCheck) let _ = symLog.log("currency: \(currency)") + let amountVoiceOver = amountToTransfer.formatted(currencyInfo, isNegative: false) + let inputDestination = LazyView { P2PSubjectV(stack: stack.push(), currencyInfo: currencyInfo, @@ -73,26 +99,52 @@ struct RequestPayment: View { expireDays: $expireDays) } let disabled = amountToTransfer.isZero || coinData.invalid || coinData.tooMany - ScrollView { VStack(alignment: .trailing) { - CurrencyInputView(currencyInfo: currencyInfo, - amount: $amountToTransfer, - available: nil, - title: minimalistic ? String(localized: "Amount:") - : String(localized: "Amount to request:"), - shortcutAction: shortcutAction) - .padding(.top) - QuiteSomeCoins(currencyInfo: currencyInfo, - currency: currency, - coinData: coinData, - shouldShowFee: true, // always true since the requester pays fees - amountEffective: peerPullCheck?.amountEffective) - NavigationLink(destination: inputDestination) { Text("Next") } - .buttonStyle(TalerButtonStyle(type: .prominent, disabled: disabled)) - .disabled(disabled) - .background(NavigationLink(destination: shortcutDestination, isActive: $buttonSelected) - { EmptyView() }.frame(width: 0).opacity(0).hidden() - ) - }.padding(.horizontal) } // ScrollVStack + + ScrollView { + let amountLabel = minimalistic ? String(localized: "Amount:") + : String(localized: "Amount to request:") + AmountInputV(stack: stack.push(), + currencyInfo: currencyInfo, + url: nil, + amountAvailable: nil, + amountToTransfer: $amountToTransfer, + wireFee: nil, + amountLabel: amountLabel, + summaryIsEditable: true, + summary: $summary, +// insufficient: $insufficient, + feeAmount: $feeAmount, + shortcutAction: shortcutAction, + buttonAction: buttonAction, + computeFee: computeFeeRequest) + .background(NavigationLink(destination: shortcutDestination, isActive: $shortcutSelected) + { EmptyView() }.frame(width: 0).opacity(0).hidden() + ) // shortcutDestination + .background(NavigationLink(destination: inputDestination, isActive: $buttonSelected) + { EmptyView() }.frame(width: 0).opacity(0).hidden() + ) // inputDestination + +// VStack(alignment: .trailing) { +// CurrencyInputView(amount: $amountToTransfer, +// available: nil, +// title: minimalistic ? String(localized: "Amount:") +// : String(localized: "Amount to request:"), +// shortcutAction: shortcutAction) +// .padding(.top) +// QuiteSomeCoins(currencyInfo: currencyInfo, +// currency: currency, +// coinData: coinData, +// shouldShowFee: true, // always true since the requester pays fees +// amountEffective: peerPullCheck?.amountEffective) +// NavigationLink(destination: inputDestination) { Text("Next") } +// .buttonStyle(TalerButtonStyle(type: .prominent, disabled: disabled)) +// .disabled(disabled) +// .background(NavigationLink(destination: shortcutDestination, isActive: $buttonSelected) +// { EmptyView() }.frame(width: 0).opacity(0).hidden() +// ) +// }.padding(.horizontal) + + } // ScrollView .frame(maxWidth: .infinity, alignment: .leading) // .scrollBounceBehavior(.basedOnSize) needs iOS 16.4 .background(WalletColors().backgroundColor.edgesIgnoringSafeArea(.all)) @@ -106,21 +158,21 @@ struct RequestPayment: View { symLog.log("❗️ \(navTitle) onDisappear") } .navigationBarItems(trailing: QRButton(action: cameraAction)) - .task(id: amountToTransfer.value) { - if exchange == nil { - if let url = scopeInfo.url { - exchange = try? await model.getExchangeByUrl(url: url) - } - } - if amountToTransfer.isZero { -// fee = EMPTYSTRING - } else { -// peerPullCheck = try? await model.checkPeerPullCreditM(amountToTransfer, exchangeBaseUrl: nil) - let baseURL = exchange?.exchangeBaseUrl - peerPullCheck = try? await model.checkPeerPullCreditM(baseURL, amount: amountToTransfer, - cancellationId: "cancel") - } - } +// .task(id: amountToTransfer.value) { +// if exchange == nil { +// if let url = scopeInfo.url { +// exchange = try? await model.getExchangeByUrl(url: url) +// } +// } +// if amountToTransfer.isZero { +// // fee = EMPTYSTRING +// } else { +// let baseURL = exchange?.exchangeBaseUrl +// // peerPullCheck = try? await model.checkPeerPullCreditM(amountToTransfer, exchangeBaseUrl: nil) +// peerPullCheck = try? await model.checkPeerPullCreditM(baseURL, amount: amountToTransfer, +// cancellationId: "cancel") +// } +// } } } // MARK: - diff --git a/TalerWallet1/Views/Peer2peer/SendAmount.swift b/TalerWallet1/Views/Peer2peer/SendAmount.swift @@ -11,9 +11,9 @@ import SymLog // Called when tapping "Send Coins" in the balances list struct SendAmount: View { - private let symLog = SymLogV(0) + private let symLog = SymLogV() let stack: CallStack - let currencyInfo: CurrencyInfo? + let currencyInfo: CurrencyInfo let amountAvailable: Amount // TODO: GetMaxPeerPushAmount @Binding var amountToTransfer: Amount @@ -68,23 +68,54 @@ struct SendAmount: View { } private func computeFeeSend(_ amount: Amount) async -> ComputeFeeResult? { + if exchange == nil { + if let url = currencyInfo.scope.url { + exchange = try? await model.getExchangeByUrl(url: url) + } + } + do { + insufficient = try amount > amountAvailable + } catch { + print("Yikes❗️ insufficient failed❗️") + insufficient = true + } + + if insufficient { +// announce("\(amountVoiceOver), \(insufficientLabel2)") + } else if amount.isZero { + feeStr = EMPTYSTRING + } else { + if let ppCheck = try? await model.checkPeerPushDebitM(amountToTransfer) { + // TODO: set from exchange +// agePicker.setAges(ages: peerPushCheck?.ageRestrictionOptions) + if let feeAmount = fee(ppCheck: ppCheck) { + feeStr = feeAmount.formatted(currencyInfo, isNegative: false) + let feeLabel = feeLabel(feeStr) +// announce("\(amountVoiceOver), \(feeLabel)") + } else { + feeStr = EMPTYSTRING +// announce(amountVoiceOver) + } + peerPushCheck = ppCheck + } else { + peerPushCheck = nil + } + } return nil } var body: some View { -#if PRINT_CHANGES +#if true //PRINT_CHANGES let _ = Self._printChanges() let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear #endif - let currency = amountToTransfer.currencyStr - let currencyInfo = controller.info(for: currency, controller.currencyTicker) - let currencySymbol = currencyInfo.specs.altUnitNames?[0] ?? currency + let currencySymbol = currencyInfo.specs.altUnitNames?[0] ?? currencyInfo.specs.name let navTitle = String(localized: "NavTitle_Send_Currency", defaultValue: "Send \(currencySymbol)", comment: "NavTitle: Send 'currencySymbol'") let available = amountAvailable.formatted(currencyInfo, isNegative: false) // let _ = print("available: \(available)") - let _ = symLog.log("currency: \(currency), available: \(available)") + let _ = symLog.log("currency: \(currencyInfo.specs.name), available: \(available)") let amountVoiceOver = amountToTransfer.formatted(currencyInfo, isNegative: false) let insufficientLabel2 = String(localized: "but you only have \(available) to send.") @@ -108,6 +139,7 @@ struct SendAmount: View { summary: $summary, expireDays: $expireDays) } + ScrollView { let amountLabel = minimalistic ? String(localized: "Amount:") : String(localized: "Amount to send:") @@ -145,41 +177,41 @@ struct SendAmount: View { symLog.log("❗️ \(navTitle) onDisappear") } .navigationBarItems(trailing: QRButton(action: cameraAction)) - .task(id: amountToTransfer.value) { - if exchange == nil { - if let url = scopeInfo.url { - exchange = try? await model.getExchangeByUrl(url: url) - } - } - do { - insufficient = try amountToTransfer > amountAvailable - } catch { - print("Yikes❗️ insufficient failed❗️") - insufficient = true - } - - if insufficient { - announce("\(amountVoiceOver), \(insufficientLabel2)") - } else if amountToTransfer.isZero { - feeStr = EMPTYSTRING - } else { - if let ppCheck = try? await model.checkPeerPushDebitM(amountToTransfer) { - // TODO: set from exchange -// agePicker.setAges(ages: peerPushCheck?.ageRestrictionOptions) - if let feeAmount = fee(ppCheck: ppCheck) { - feeStr = feeAmount.formatted(currencyInfo, isNegative: false) - let feeLabel = feeLabel(feeStr) - announce("\(amountVoiceOver), \(feeLabel)") - } else { - feeStr = EMPTYSTRING - announce(amountVoiceOver) - } - peerPushCheck = ppCheck - } else { - peerPushCheck = nil - } - } - } +// .task(id: amountToTransfer.value) { +// if exchange == nil { +// if let url = scopeInfo.url { +// exchange = try? await model.getExchangeByUrl(url: url) +// } +// } +// do { +// insufficient = try amountToTransfer > amountAvailable +// } catch { +// print("Yikes❗️ insufficient failed❗️") +// insufficient = true +// } +// +// if insufficient { +// announce("\(amountVoiceOver), \(insufficientLabel2)") +// } else if amountToTransfer.isZero { +// feeStr = EMPTYSTRING +// } else { +// if let ppCheck = try? await model.checkPeerPushDebitM(amountToTransfer) { +// // TODO: set from exchange +//// agePicker.setAges(ages: peerPushCheck?.ageRestrictionOptions) +// if let feeAmount = fee(ppCheck: ppCheck) { +// feeStr = feeAmount.formatted(currencyInfo, isNegative: false) +// let feeLabel = feeLabel(feeStr) +// announce("\(amountVoiceOver), \(feeLabel)") +// } else { +// feeStr = EMPTYSTRING +// announce(amountVoiceOver) +// } +// peerPushCheck = ppCheck +// } else { +// peerPushCheck = nil +// } +// } +// } } } // MARK: - diff --git a/TalerWallet1/Views/Sheets/WithdrawBankIntegrated/WithdrawURIView.swift b/TalerWallet1/Views/Sheets/WithdrawBankIntegrated/WithdrawURIView.swift @@ -60,6 +60,14 @@ struct WithdrawURIView: View { } private func computeFeeWithdraw(_ amount: Amount) async -> ComputeFeeResult? { + if let exchange { + if let details = try? await model.getWithdrawalDetailsForAmountM(exchange.exchangeBaseUrl, + amount: amount) { + let fee = try? details.amountRaw - details.amountEffective + let feeStr = fee?.readableDescription ?? "nix" + symLog.log("Fee = \(feeStr)") + } + } return nil }