diff options
author | Marc Stibane <marc@taler.net> | 2023-12-05 16:21:53 +0100 |
---|---|---|
committer | Marc Stibane <marc@taler.net> | 2023-12-05 16:21:53 +0100 |
commit | 390fce1f0a45c667701d252bdb31ac91fd7a9b31 (patch) | |
tree | c3054ee88088d7015ada86cad9a5237f8c525d8d | |
parent | fa2041e1ba656f66077acaedb52d4234c4fc40f4 (diff) | |
download | taler-ios-390fce1f0a45c667701d252bdb31ac91fd7a9b31.tar.gz taler-ios-390fce1f0a45c667701d252bdb31ac91fd7a9b31.tar.bz2 taler-ios-390fce1f0a45c667701d252bdb31ac91fd7a9b31.zip |
shortcutAction
-rw-r--r-- | TalerWallet1/Views/Exchange/ManualWithdraw.swift | 3 | ||||
-rw-r--r-- | TalerWallet1/Views/HelperViews/CurrencyInputView.swift | 16 | ||||
-rw-r--r-- | TalerWallet1/Views/Peer2peer/P2PSubjectV.swift | 57 | ||||
-rw-r--r-- | TalerWallet1/Views/Peer2peer/RequestPayment.swift | 41 | ||||
-rw-r--r-- | TalerWallet1/Views/Peer2peer/SendAmount.swift | 51 |
5 files changed, 119 insertions, 49 deletions
diff --git a/TalerWallet1/Views/Exchange/ManualWithdraw.swift b/TalerWallet1/Views/Exchange/ManualWithdraw.swift index a50f64d..da6e34b 100644 --- a/TalerWallet1/Views/Exchange/ManualWithdraw.swift +++ b/TalerWallet1/Views/Exchange/ManualWithdraw.swift @@ -53,7 +53,8 @@ struct ManualWithdraw: View { CurrencyInputView(amount: $amountToTransfer, available: nil, title: iconOnly ? String(localized: "How much:") - : String(localized: "Amount to withdraw:")) + : String(localized: "Amount to withdraw:"), + shortcutAction: nil) .padding(.top) QuiteSomeCoins(someCoins: someCoins, shouldShowFee: true, // TODO: set to false if we never charge withdrawal fees diff --git a/TalerWallet1/Views/HelperViews/CurrencyInputView.swift b/TalerWallet1/Views/HelperViews/CurrencyInputView.swift index 8c3f7c2..3c9cbae 100644 --- a/TalerWallet1/Views/HelperViews/CurrencyInputView.swift +++ b/TalerWallet1/Views/HelperViews/CurrencyInputView.swift @@ -55,6 +55,7 @@ struct CurrencyInputView: View { @Binding var amount: Amount // the `value´ let available: Amount? let title: String + let shortcutAction: ((_ amount: Amount) -> Void)? @EnvironmentObject private var controller: Controller @@ -63,11 +64,15 @@ struct CurrencyInputView: View { @State private var useShortcut = 0 func action(shortcut: Int, currencyField: CurrencyField) { - useShortcut = shortcut let shortie = Amount(currency: amount.currencyStr, cent: UInt64(shortcut)) // TODO: adapt for ¥ - currencyField.updateText(amount: shortie) - amount = shortie - currencyField.resignFirstResponder() + if let shortcutAction { + shortcutAction(shortie) + } else { + useShortcut = shortcut + currencyField.updateText(amount: shortie) + amount = shortie + currencyField.resignFirstResponder() + } } var body: some View { @@ -168,7 +173,8 @@ fileprivate struct Previews: PreviewProvider { // Preview_Content() CurrencyInputView(amount: $amountToTransfer, available: nil, - title: "Amount to withdraw:") + title: "Amount to withdraw:", + shortcutAction: nil) .environmentObject(controller) } } diff --git a/TalerWallet1/Views/Peer2peer/P2PSubjectV.swift b/TalerWallet1/Views/Peer2peer/P2PSubjectV.swift index 5637946..d6a788c 100644 --- a/TalerWallet1/Views/Peer2peer/P2PSubjectV.swift +++ b/TalerWallet1/Views/Peer2peer/P2PSubjectV.swift @@ -6,33 +6,61 @@ import SwiftUI import taler_swift import SymLog +func p2pFee(ppCheck: CheckPeerPushDebitResponse) -> Amount? { + do { + // Outgoing: fee = effective - raw + let fee = try ppCheck.amountEffective - ppCheck.amountRaw + return fee + } catch {} + return nil +} + struct P2PSubjectV: View { private let symLog = SymLogV(0) let stack: CallStack - let navTitle: String - let buttonTitle: String - let feeLabel: String + let feeLabel: String? let currencyInfo: CurrencyInfo let amountToSend: Bool @Binding var amountToTransfer: Amount @Binding var summary: String @Binding var expireDays: UInt + @EnvironmentObject private var model: WalletModel @AppStorage("iconOnly") var iconOnly: Bool = false + @State private var myFeeLabel: String = EMPTYSTRING @State private var transactionStarted: Bool = false @FocusState private var isFocused: Bool + private func buttonTitle(_ amount: Amount, _ currencyInfo: CurrencyInfo) -> String { + let amountWithCurrency = amount.string(currencyInfo, useSymbol: false) + return amountToSend ? String(localized: "Send \(amountWithCurrency) now", + comment: "amount with currency") + : String(localized: "Request \(amountWithCurrency)", + comment: "amount with currency") + } + + private func subjectTitle(_ amount: Amount, _ currencyInfo: CurrencyInfo) -> String { + let amountStr = amount.string(currencyInfo) + return amountToSend ? String(localized: "NavTitle_Send_AmountStr", + defaultValue: "Send \(amountStr)", + comment: "NavTitle: Send 'amountStr'") + : String(localized: "NavTitle_Request_AmountStr", + defaultValue: "Request \(amountStr)", + comment: "NavTitle: Request 'amountStr'") + } + var body: some View { #if DEBUG let _ = Self._printChanges() - let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear + let _ = symLog.vlog(amountToTransfer.readableDescription) // just to get the # to compare it with .onAppear & onDisappear #endif ScrollView { VStack (alignment: .leading, spacing: 6) { - if feeLabel.count > 0 { + let label = feeLabel ?? myFeeLabel + if label.count > 0 { HStack { Spacer() - Text(feeLabel) + Text(label) .foregroundColor(.red) .accessibilityFont(.body) } @@ -85,13 +113,13 @@ struct P2PSubjectV: View { amountToTransfer: amountToTransfer, transactionStarted: $transactionStarted) }) { - Text(buttonTitle) + Text(buttonTitle(amountToTransfer, currencyInfo)) } .buttonStyle(TalerButtonStyle(type: .prominent)) .disabled(disabled) .accessibilityHint(disabled ? "enabled when subject and expiration are set" : EMPTYSTRING) }.padding(.horizontal) } // ScrollVStack - .navigationTitle(navTitle) + .navigationTitle(subjectTitle(amountToTransfer, currencyInfo)) .background(WalletColors().backgroundColor.edgesIgnoringSafeArea(.all)) .onAppear { DebugViewC.shared.setViewID(VIEW_P2P_SUBJECT, stack: stack.push()) @@ -100,6 +128,19 @@ struct P2PSubjectV: View { .onDisappear { // print("❗️ P2PSubjectV onDisappear") } + .task(id: amountToTransfer.value) { + if feeLabel == nil { + do { + let ppCheck = try await model.checkPeerPushDebitM(amountToTransfer) + if let feeAmount = p2pFee(ppCheck: ppCheck) { + let feeStr = feeAmount.string(currencyInfo) + myFeeLabel = String(localized: "+ \(feeStr) send fee") + } else { myFeeLabel = EMPTYSTRING } + } catch { // TODO: error + symLog.log(error.localizedDescription) + } + } + } } } // MARK: - diff --git a/TalerWallet1/Views/Peer2peer/RequestPayment.swift b/TalerWallet1/Views/Peer2peer/RequestPayment.swift index 74205af..45c0441 100644 --- a/TalerWallet1/Views/Peer2peer/RequestPayment.swift +++ b/TalerWallet1/Views/Peer2peer/RequestPayment.swift @@ -20,6 +20,13 @@ struct RequestPayment: View { @State private var peerPullCheck: CheckPeerPullCreditResponse? = nil @State private var expireDays: UInt = 0 + @State private var buttonSelected = false + @State private var amountShortcut = Amount.zero(currency: EMPTYSTRING) // Update currency when used + + private func shortcutAction(_ shortcut: Amount) { + amountShortcut = shortcut + buttonSelected = true + } var body: some View { #if DEBUG @@ -32,41 +39,45 @@ struct RequestPayment: View { let navTitle = String(localized: "NavTitle_Request_Currency", defaultValue: "Request \(currencySymbol)", comment: "NavTitle: Request 'currencySymbol'") - let amountStr = amountToTransfer.string(currencyInfo) - let amountWithCurrency = amountToTransfer.string(currencyInfo, useSymbol: false) - let buttonTitle = String(localized: "Request \(amountWithCurrency)", comment: "amount with currency") - let navTitle2 = String(localized: "NavTitle_Request_AmountStr", - defaultValue: "Request \(amountStr)", - comment: "NavTitle: Request 'amountStr'") - let someCoins = SomeCoins(details: peerPullCheck) - let p2pSubjectV = LazyView { + let _ = symLog.log("currency: \(currency)") + let inputDestination = LazyView { P2PSubjectV(stack: stack.push(), - amountToSend: nil, - amountToReceive: amountToTransfer, - navTitle: navTitle2, - buttonTitle: buttonTitle, feeLabel: someCoins.feeLabel(currencyInfo), currencyInfo: currencyInfo, + amountToSend: false, + amountToTransfer: $amountToTransfer, + summary: $summary, + expireDays: $expireDays) + } + let shortcutDestination = LazyView { + P2PSubjectV(stack: stack.push(), + feeLabel: nil, + currencyInfo: currencyInfo, + amountToSend: false, + amountToTransfer: $amountShortcut, summary: $summary, expireDays: $expireDays) } - let _ = symLog.log("currency: \(currency)") let disabled = amountToTransfer.isZero || someCoins.invalid || someCoins.tooMany ScrollView { VStack(alignment: .trailing) { CurrencyInputView(amount: $amountToTransfer, available: nil, title: iconOnly ? String(localized: "How much:") - : String(localized: "Amount to request:")) + : String(localized: "Amount to request:"), + shortcutAction: shortcutAction) .padding(.top) QuiteSomeCoins(someCoins: someCoins, shouldShowFee: true, // always true since the requester pays fees currency: currency, currencyInfo: currencyInfo, amountEffective: peerPullCheck?.amountEffective) - NavigationLink(destination: p2pSubjectV) { Text("Next") } + NavigationLink(destination: inputDestination) { Text("Next") } .buttonStyle(TalerButtonStyle(type: .prominent)) .disabled(disabled) + .background(NavigationLink(destination: shortcutDestination, isActive: $buttonSelected) + { EmptyView() }.frame(width: 0).opacity(0).hidden() + ) } } // ScrollVStack .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal) // Lists do this automatically, but this is a VStack diff --git a/TalerWallet1/Views/Peer2peer/SendAmount.swift b/TalerWallet1/Views/Peer2peer/SendAmount.swift index 99de703..d66f41c 100644 --- a/TalerWallet1/Views/Peer2peer/SendAmount.swift +++ b/TalerWallet1/Views/Peer2peer/SendAmount.swift @@ -23,6 +23,8 @@ struct SendAmount: View { @State private var expireDays = SEVENDAYS @State private var insufficient = false @State private var feeStr: String = EMPTYSTRING + @State private var buttonSelected = false + @State private var amountShortcut = Amount.zero(currency: EMPTYSTRING) // Update currency when used private func fee(ppCheck: CheckPeerPushDebitResponse?) -> Amount? { do { @@ -37,6 +39,11 @@ struct SendAmount: View { var feeLabel: String { feeStr.count > 0 ? String(localized: "+ \(feeStr) send fee") : EMPTYSTRING } + private func shortcutAction(_ shortcut: Amount) { + amountShortcut = shortcut + buttonSelected = true + } + var body: some View { #if DEBUG let _ = Self._printChanges() @@ -48,13 +55,6 @@ struct SendAmount: View { let navTitle = String(localized: "NavTitle_Send_Currency", defaultValue: "Send \(currencySymbol)", comment: "NavTitle: Send 'currencySymbol'") - let amountStr = amountToTransfer.string(currencyInfo) - let amountWithCurrency = amountToTransfer.string(currencyInfo, useSymbol: false) - let buttonTitle = String(localized: "Send \(amountWithCurrency) now", comment: "amount with currency") - let navTitle2 = String(localized: "NavTitle_Send_AmountStr", - defaultValue: "Send \(amountStr)", - comment: "NavTitle: Send 'amountStr'") - let available = amountAvailable.string(currencyInfo) // let _ = print("available: \(available)") let _ = symLog.log("currency: \(currency), available: \(available)") @@ -62,6 +62,24 @@ struct SendAmount: View { let insufficientLabel = String(localized: "You don't have enough \(currency).") let insufficientLabel2 = String(localized: "but you only have \(available) to send.") + let inputDestination = LazyView { + P2PSubjectV(stack: stack.push(), + feeLabel: feeLabel, + currencyInfo: currencyInfo, + amountToSend: true, + amountToTransfer: $amountToTransfer, + summary: $summary, + expireDays: $expireDays) + } + let shortcutDestination = LazyView { + P2PSubjectV(stack: stack.push(), + feeLabel: nil, + currencyInfo: currencyInfo, + amountToSend: true, + amountToTransfer: $amountShortcut, + summary: $summary, + expireDays: $expireDays) + } let disabled = insufficient || amountToTransfer.isZero ScrollView { VStack(alignment: .trailing) { Text("Available:\t\(available)") @@ -70,26 +88,19 @@ struct SendAmount: View { CurrencyInputView(amount: $amountToTransfer, available: amountAvailable, title: iconOnly ? String(localized: "How much:") - : String(localized: "Amount to send:")) + : String(localized: "Amount to send:"), + shortcutAction: shortcutAction) Text(insufficient ? insufficientLabel : feeLabel) .accessibilityFont(.body) .foregroundColor(.red) .padding(4) - NavigationLink(destination: LazyView { - P2PSubjectV(stack: stack.push(), - navTitle: navTitle2, - buttonTitle: buttonTitle, - feeLabel: feeLabel, - currencyInfo: currencyInfo, - amountToSend: true, - amountToTransfer: $amountToTransfer, - summary: $summary, - expireDays: $expireDays) - }) { Text("Next") } + NavigationLink(destination: inputDestination) { Text("Next") } .buttonStyle(TalerButtonStyle(type: .prominent)) .disabled(disabled) -// Spacer() + .background(NavigationLink(destination: shortcutDestination, isActive: $buttonSelected) + { EmptyView() }.frame(width: 0).opacity(0).hidden() + ) } } // ScrollVStack .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal) |