taler-ios

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

commit c420e3c5399f0b59fa9b80a4da5e904cb5603734
parent e514a6473c9da86ba795447b186f64384a58f94b
Author: Marc Stibane <marc@taler.net>
Date:   Fri, 27 Nov 2026 16:15:31 +0100

implement enhanced Payment Choices

Diffstat:
MTalerWallet1/Views/Sheets/Payment/ChoicesView.swift | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
MTalerWallet1/Views/Sheets/Payment/PaymentScan.swift | 62+++++++++++++++++++++++++++++++++++++++++---------------------
MTalerWallet1/Views/Transactions/PaymentTransactionView.swift | 169++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
MTalerWallet1/Views/Transactions/ThreeAmountsSection.swift | 6------
MTalerWallet1/Views/Transactions/TransactionSummaryList.swift | 86++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
MTalerWallet1/Views/Transactions/TransactionTypeDetail.swift | 18+++++++++++++++---
6 files changed, 321 insertions(+), 110 deletions(-)

diff --git a/TalerWallet1/Views/Sheets/Payment/ChoicesView.swift b/TalerWallet1/Views/Sheets/Payment/ChoicesView.swift @@ -7,6 +7,7 @@ */ import SwiftUI import taler_swift +import SymLog struct SelectedBackground: View, Sendable { let useColor: Bool? @@ -96,16 +97,42 @@ struct InputTokenView: View, Sendable { } // MARK: - struct ChoicesView: View, Sendable { + private let symLog = SymLogV(0) let stack: CallStack let choiceTriples: [ChoiceTriple] - let tokenFamilies: [String: ContractTokenFamily]? // token_family_slug: String + let terms: MerchantContractTerms let showHeader: Bool let automaticIndex: Int? @Binding var selectedChoice: Int? - +#if !GNU_TALER + let formatted: (String, String)? + @Binding var payNow: Bool + @Binding var isFinished: Bool? + @Namespace private var namespace +#endif @Environment(\.colorScheme) private var colorScheme @Environment(\.colorSchemeContrast) private var colorSchemeContrast + @ViewBuilder + func headerV(_ contractChoice: ContractChoice) -> some View { + if showHeader { // V1 + if let description = contractChoice.locDescription { + Text(description) + .talerFont(.body) + } } + } + @ViewBuilder + func amountV(_ amount: Amount, scope: ScopeInfo?, possible: Bool) -> some View { + if !amount.isZero { + HStack { + Spacer() + AmountV(stack: stack.push(), + scope: scope, + amount: amount) + .foregroundColor(possible ? .primary : .secondary) + } + } + } var body: some View { Section { ForEach(choiceTriples, id: \.2) { triple in @@ -116,39 +143,63 @@ struct ChoicesView: View, Sendable { let payAutomatic = automaticIndex == index let scopeInfo = selectionDetail.scopeInfo let amount = selectionDetail.amountRaw - /// If it contains money, always show it. Disable but don’t hide it if insufficient - that serves as advertisement what you could buy if you had more money / tokens. + /// Disable but don’t hide it if insufficient - that serves as advertisement what you could buy if you had more money / tokens. /// If it only contains tokens, and insufficient, then hide it. Most probably there is a money payment option to acquire those tokens, which is advertisement enough. /// Tokens only, paymentPossible. If automaticIndex, then also hide it (and auto-pay it). Otherwise show it, so the user can select it. - if (!amount.isZero || isPaymentPossible) && !payAutomatic { +// if (!amount.isZero || isPaymentPossible) && !payAutomatic { VStack(alignment: .leading) { - if showHeader { // V1 - if let description = contractChoice.locDescription { - Text(description) - .talerFont(.body) - } } - - HStack { - Spacer() - AmountV(stack: stack.push(), - scope: scopeInfo, - amount: contractChoice.amount) - .foregroundColor(isPaymentPossible ? .primary : .secondary) + let header = headerV(contractChoice) + let amount = amountV(contractChoice.amount, scope: scopeInfo, possible: isPaymentPossible) + if #available(iOS 16.0, *) { + ViewThatFits(in: .horizontal) { + HStack(alignment: .firstTextBaseline) { + header + amount + } + Group { + header + amount + } + } + } else { // Fallback on earlier versions + header + amount } - if let tokenFamilies { +#if GNU_TALER + let isSelected = false +#else + let isSelected = selectedChoice == index +#endif + if let tokenFamilies = terms.tokenFamilies { // Spend Input tokens let inputs = contractChoice.inputs if !inputs.isEmpty { Text("Spend now:") .talerFont(.body) .padding(.bottom, -4) + .padding(.top, 0.1) InputTokenView(inputs: inputs, tokenFamilies: tokenFamilies) } + } +#if !GNU_TALER + if isSelected { + PaymentButton(symLog: symLog, + stack: stack.push(), + terms: terms, + formatted: formatted, + isFinished: $isFinished, + payNow: $payNow) + .matchedGeometryEffect(id: "PaymentButton", in: namespace) + } +#endif + if let tokenFamilies = terms.tokenFamilies { // Receive output tokens let outputs = contractChoice.outputs if !outputs.isEmpty { Text("Get with your purchase:") .talerFont(.body) .padding(.bottom, -4) + .padding(.top, isSelected ? 6 : 0.1) OutputTokenView(outputs: outputs, tokenFamilies: tokenFamilies) } } @@ -156,13 +207,16 @@ struct ChoicesView: View, Sendable { .contentShape(.rect) .onTapGesture { if isPaymentPossible { + withAnimation { selectedChoice = index + } } } + .listRowBackground( SelectedBackground(useColor: index == selectedChoice ? isPaymentPossible : nil) ) - } +// } }.listRowSeparator(.hidden) } header: { Text("Payment Options") diff --git a/TalerWallet1/Views/Sheets/Payment/PaymentScan.swift b/TalerWallet1/Views/Sheets/Payment/PaymentScan.swift @@ -229,6 +229,7 @@ struct PaymentView2: View, Sendable { header: nil, ) } else { +// let _ = print("No ThreeAmounts") // TODO: Error - neither effective nor balanceDetails // actually this is shown when effective is not yet loaded for a brief moment // - so don't show an error here @@ -238,12 +239,11 @@ struct PaymentView2: View, Sendable { } } // MARK: - -struct PaySafeArea: View, Sendable { +struct PaymentButton: View, Sendable { let symLog: SymLogV? let stack: CallStack let terms: MerchantContractTerms - let amountString: String - let amountA11y: String + let formatted: (String, String)? @Binding var isFinished: Bool? @Binding var payNow: Bool @@ -252,29 +252,40 @@ struct PaySafeArea: View, Sendable { @State private var wasTapped: Bool = false @State private var timeToPay: Int? - var body: some View { - let fiveMinutes = 5 * 60 - let button = Button("Pay \(amountString) now") { - if !wasTapped { - wasTapped = true - controller.hapticNotification(.success) - symLog?.log("paying \(amountString) now") + func buttonAction() { + if !wasTapped { + wasTapped = true + controller.hapticNotification(.success) + symLog?.log("paying now") #if DEBUG // 1 second delay - DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { - payNow = true - } -#else + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { payNow = true + } +#else + payNow = true #endif + } + } + + var body: some View { + let fiveMinutes = 5 * 60 + let disabled = wasTapped || isFinished != false + let button = Button(action: buttonAction) { + if let formatted { + let amountString = formatted.0 + let amountA11y = formatted.1 + Text("Pay \(amountString) now") + } else { + Text("Spend discounts now") } }.id("payNow") - .accessibilityLabel(Text("Pay \(amountA11y) now", comment: "a11y")) - .buttonStyle(TalerButtonStyle(type: .prominent, disabled: wasTapped)) - .disabled(wasTapped) +// .accessibilityLabel(Text("Pay \(amountA11y) now", comment: "a11y")) + .buttonStyle(TalerButtonStyle(type: .prominent, disabled: disabled)) + .disabled(disabled) .onChange(of: payNow) { newValue in if !newValue { wasTapped = false } // this attempt finished (success or failure) - allow retry } - .padding(.horizontal) +// .padding(.horizontal) .task { timeToPay = terms.timeToPay(true) if let timeToPay { @@ -291,10 +302,13 @@ struct PaySafeArea: View, Sendable { if let timeToPay { if timeToPay < fiveMinutes { + let timeView = TimeView(String(localized: "Time to pay:"), + seconds: timeToPay, + isFinished: $isFinished) + .id("timeView") +#if GNU_TALER let view = VStack { - TimeView(String(localized: "Time to pay:"), - seconds: timeToPay, - isFinished: $isFinished) + timeView .padding(.top) button } @@ -304,6 +318,12 @@ struct PaySafeArea: View, Sendable { } else { view } +#else + VStack { + button + timeView + } +#endif } else { let _ = symLog?.log("timeToPay \(timeToPay) seconds not shown") button diff --git a/TalerWallet1/Views/Transactions/PaymentTransactionView.swift b/TalerWallet1/Views/Transactions/PaymentTransactionView.swift @@ -43,10 +43,6 @@ struct MerchantHeader: View { var body: some View { if let terms { - Section { - Text(terms.locSummary) - .talerFont(.title3) - } header: { VStack(alignment: .leading) { HStack { if terms.merchant.logo != nil { @@ -81,11 +77,10 @@ struct MerchantHeader: View { image = Image(imageBase64: imageBase64) } } - Text("Summary") - .talerFont(.title3) - .foregroundColor(WalletColors().secondary2(colorScheme, colorSchemeContrast)) +// Text("Summary") +// .talerFont(.title3) +// .foregroundColor(WalletColors().secondary2(colorScheme, colorSchemeContrast)) } - } } } } @@ -96,14 +91,26 @@ struct PaymentTransactionView: View { let common: TransactionCommon let paymentTransaction: PaymentTransaction @Binding var scope: ScopeInfo? - @Binding var effective: Amount? @Binding var possible: Bool? +#if GNU_TALER + @Binding var effective: Amount? @Binding var payNow: Bool @Binding var selectedChoice: Int? - +#else + @Binding var isFinished: Bool? +#endif @EnvironmentObject private var controller: Controller @EnvironmentObject private var model: WalletModel + @State private var showProducts: Bool = false +#if !GNU_TALER + @State private var effective: Amount? = nil + @State private var payNow: Bool = false + @State private var selectedChoice: Int? = nil + @State private var currencyInfo: CurrencyInfo = CurrencyInfo.zero(UNKNOWN) + @State private var formatted: (String, String)? = nil +#endif + func selectIndex(_ index: Int?, of choices: [ChoiceTriple]) -> Int? { let index1 = index ?? 0 let select = index1 < choices.count ? index1 : 0 @@ -163,6 +170,62 @@ struct PaymentTransactionView: View { return nil } + func buttonAction() { + withAnimation { + showProducts.toggle() + } + } + +#if !GNU_TALER + private func payTransaction() async { + if let confirmPayResult = try? await model.confirmPay(common.transactionId, + choiceIndex: selectedChoice) { +// symLog.log(confirmPayResult as Any) + if confirmPayResult.type == "done" { +// if let url { +// controller.removeURL(url) +// } + } else { +// if let url { +// controller.removeURL(url) // TODO: pending might fail - in which case we might want to try again +// } + } + let txId = confirmPayResult.transactionId + if let matched = try? await model.waitTransactionState(txId, token: txId, major: .done), matched { + } else { + print("payTransaction: waitTransactionState bail") // TODO: ??? + } + } + } +#endif + + @ViewBuilder + func choicesView(choices: [ChoiceTriple], + terms: MerchantContractTerms, + showHeader: Bool, + automaticIndex: Int? + ) -> some View { +#if GNU_TALER + ChoicesView(stack: stack.push(), + choiceTriples: choices, + terms: terms, + showHeader: showHeader, + automaticIndex: automaticIndex, + selectedChoice: $selectedChoice) +#else + ChoicesView(stack: stack.push(), + choiceTriples: choices, + terms: terms, + showHeader: showHeader, + automaticIndex: automaticIndex, + selectedChoice: $selectedChoice, + formatted: formatted, + payNow: $payNow, + isFinished: $isFinished + ) +#endif + } + var body: some View { #if PRINT_CHANGES let _ = Self._printChanges() @@ -171,37 +234,61 @@ struct PaymentTransactionView: View { let details = paymentTransaction.details if common.isDialog { // show payment confirmation dialog let state = common.txState - MerchantHeader(terms: details.contractTerms) + let terms1 = details.contractTerms + let choicesTuple = controller.choicesTuple + let txId = choicesTuple.0 + let choicesForPayment = choicesTuple.1 + + Section { + let summary = Text(terms1?.locSummary ?? "Summary") + .talerFont(.title3) + + if let choicesForPayment { + if txId == common.transactionId { + let terms2 = choicesForPayment.contractTerms + if let products = terms2.products { + let chevron = Image(systemName: CHEVRON_UP) // 􀆇 + Button(action: buttonAction) { + HStack(alignment: .firstTextBaseline) { + summary + Spacer() + chevron.rotationEffect(.degrees((showProducts ? 0 : -180))) + .accessibilityHidden(true) + } + } + if showProducts { + ProductsView(stack: stack.push(), + scope: scope, + products: products) + } + } else { + summary + } + } + } + } header: { + MerchantHeader(terms: details.contractTerms) + } .task(id: state.hashValue) { - let choicesTuple = controller.choicesTuple - let choicesForPayment = choicesTuple.1 - if choicesTuple.0 != common.transactionId || choicesForPayment == nil { + if txId != common.transactionId || choicesForPayment == nil { symLog.log("❗️❗️ task: \(state)") await controller.loadChoicesForPayment(stack.push(), - txId: common.transactionId) + txId: common.transactionId) } else { symLog.log("❗️❗️ choicesForPayment exists, no need for task: \(state)") } } // loadChoicesForPayment - let choicesTuple = controller.choicesTuple - if let txId = choicesTuple.0, let choicesForPayment = choicesTuple.1 { + if let choicesForPayment { if txId == common.transactionId { - let terms = choicesForPayment.contractTerms - if let products = terms.products { - ProductsView(stack: stack.push(), - scope: scope, - products: products) - } + let terms2 = choicesForPayment.contractTerms if let (choices, showHeader) = choicesForPayment.choiceTriple() { let hasAutomatic = choicesForPayment.automaticExecution ?? false let automaticIndex = hasAutomatic ? choicesForPayment.automaticExecutableIndex : nil - ChoicesView(stack: stack.push(), - choiceTriples: choices, - tokenFamilies: terms.tokenFamilies, - showHeader: showHeader, - automaticIndex: automaticIndex, - selectedChoice: $selectedChoice) + choicesView(choices: choices, + terms: terms2, + showHeader: showHeader, + automaticIndex: automaticIndex) .task { automaticOrDefault(automaticIndex, choicesForPayment.defaultChoiceIndex, of: choices) } @@ -211,16 +298,36 @@ struct PaymentTransactionView: View { effective = newChoice.0.amountEffective possible = newChoice.0.status == .paymentPossible scope = newChoice.0.scopeInfo +#if !GNU_TALER + if let scope, let effective, !effective.isZero { + currencyInfo = controller.info(for: scope) ?? CurrencyInfo.zero(UNKNOWN) + formatted = effective.formatted(currencyInfo) + } else { + effective = nil + scope = nil + formatted = nil + } +#endif } else { effective = nil possible = nil scope = nil } } + .onChange(of: payNow) { payNow2 in +#if !GNU_TALER + if payNow2 { + Task { + await payTransaction() + payNow = false // re-enable "Pay now" if this attempt did not lead to a state transition + } + } +#endif + } if let choice = selectedOrFirst(choices) { - let summary = details.info?.locSummary ?? - String(localized: "No summary", comment: "OrderShortInfo.summary") +// let summary = details.info?.locSummary ?? +// String(localized: "No summary", comment: "OrderShortInfo.summary") let selectionDetail: ChoiceSelectionDetail = choice.0 // let contractChoice: ContractChoice = choice.1 PaymentView2(stack: stack.push(), // TODO: details.info.merchant.name diff --git a/TalerWallet1/Views/Transactions/ThreeAmountsSection.swift b/TalerWallet1/Views/Transactions/ThreeAmountsSection.swift @@ -121,12 +121,6 @@ struct ThreeAmountsSection: View { : WalletColors().transactionColor(incoming) let hasNoFees = noFees ?? false Section { -// if let summary { -// Text(summary) -// .talerFont(.title3) -// .padding(.bottom) -// } - if currency != "UNKNOWN" { if pendingDialog || isDone { Text(pendingDialog ? "Payment will be made in \(currency)" diff --git a/TalerWallet1/Views/Transactions/TransactionSummaryList.swift b/TalerWallet1/Views/Transactions/TransactionSummaryList.swift @@ -55,12 +55,14 @@ struct TransactionSummaryList: View { @State private var didDelete: Bool = false @State private var jsonTransaction: String = EMPTYSTRING @State private var viewId = UUID() - @State private var selectedChoice: Int? = nil - @State private var effective: Amount? = nil @State private var paymentPossible: Bool? = nil @State private var scope: ScopeInfo? = nil @State private var isFinished: Bool? = false +#if GNU_TALER + @State private var effective: Amount? = nil @State private var payNow: Bool = false + @State private var selectedChoice: Int? = nil +#endif @State private var isWorking: Bool = false @State private var tosAccepted: Bool? = nil @State private var tosBaseUrl: String = EMPTYSTRING @@ -96,6 +98,7 @@ struct TransactionSummaryList: View { } } +#if GNU_TALER private func payTransaction() async { if let confirmPayResult = try? await model.confirmPay(transactionId, choiceIndex: selectedChoice) { @@ -117,7 +120,7 @@ struct TransactionSummaryList: View { } } } - +#endif @MainActor @discardableResult func checkDismiss(_ notification: Notification, _ logStr: String = EMPTYSTRING) -> Bool { @@ -330,6 +333,15 @@ struct TransactionSummaryList: View { : talerTX.localizedType } + func checkToS(for newBaseUrl: String) { + print(" 🫆 onChange of tosBaseUrl: \(newBaseUrl)") + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { + Task { + tosAccepted = await controller.checkToS(for: newBaseUrl) + } + } + } + var body: some View { #if PRINT_CHANGES let _ = Self._printChanges() @@ -348,24 +360,33 @@ struct TransactionSummaryList: View { dateSection(common) } if !controller.stalledConnection { +#if GNU_TALER TransactionTypeDetail(stack: stack.push(), transaction: $talerTX, tosAccepted: tosAccepted, // only for ToSButtonView above List tosBaseUrl: $tosBaseUrl, payNow: $payNow, selectedChoice: $selectedChoice, - scope: $scope, effective: $effective, // return amount for "Pay <amount> now" + scope: $scope, possible: $paymentPossible, hasDone: hasDone) .onChange(of: tosBaseUrl) { newBaseUrl in - print(" 🫆 onChange of tosBaseUrl: \(newBaseUrl)") - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - Task { - tosAccepted = await controller.checkToS(for: newBaseUrl) - } - } + checkToS(for: newBaseUrl) } +#else + TransactionTypeDetail(stack: stack.push(), + transaction: $talerTX, + tosAccepted: tosAccepted, // only for ToSButtonView above List + tosBaseUrl: $tosBaseUrl, + isFinished: $isFinished, + scope: $scope, + possible: $paymentPossible, + hasDone: hasDone) + .onChange(of: tosBaseUrl) { newBaseUrl in + checkToS(for: newBaseUrl) + } +#endif if withActions { abortFailDelete(common) } } }.id(viewId) // change viewId to enforce a draw update @@ -392,17 +413,30 @@ struct TransactionSummaryList: View { .interactiveDismissDisabled(hasDone && !isPayDialog) // can only use "Done" button to dismiss .safeAreaInset(edge: .bottom) { let cancelButton = cancelButton() - let bottomButton = ZStack { + if controller.stalledConnection { + VStack { + StalledView() + cancelButton + .padding(.bottom) + } + .background { + SelectedBackground(useColor: false) + } + .padding(Bool.iOS26 ? .horizontal : .all) + .transition(.move(edge: .bottom)) + } else { + ZStack { // we only ever have one item - but need to apply padding let major = common.txState.major.rawValue let minor = common.txState.minor if isPayDialog { if case .payment(let paymentTransaction) = talerTX { let details = paymentTransaction.details if let terms = details.contractTerms, isFinished == false { +#if GNU_TALER // else pay button is shown inside each payment option if let effective { let formatted = effective.formatted(currencyInfo) -// let _ = print("💰 PaySafeArea") - PaySafeArea(symLog: symLog, +// let _ = print("💰 PaymentButton") + PaymentButton(symLog: symLog, stack: stack.push(), terms: terms, amountString: formatted.0, @@ -412,10 +446,11 @@ struct TransactionSummaryList: View { } else { // let _ = print("💰 no Cancel") } // wait for $effective - don't show Cancel +#endif } else { - let eff = effective != nil ? "effective" : "-" - let ter = details.contractTerms != nil ? "terms" : "-" - let fin = isFinished == false ? "not finished" : "-" +// let eff = effective != nil ? "effective" : "-" +// let ter = details.contractTerms != nil ? "terms" : "-" +// let fin = isFinished == false ? "not finished" : "-" // let _ = print("💰 Cancel \(eff), \(ter), \(fin)") cancelButton } // Cancel @@ -432,22 +467,9 @@ struct TransactionSummaryList: View { .padding(Bool.iOS26 ? .horizontal : .all) } } - } - if controller.stalledConnection { - VStack { - StalledView() - cancelButton - .padding(.bottom) - } - .background { - SelectedBackground(useColor: false) - } + } .padding(Bool.iOS26 ? .horizontal : .all) - .transition(.move(edge: .bottom)) - } else { - bottomButton - .padding(Bool.iOS26 ? .horizontal : .all) - .transition(.scale.combined(with: .opacity)) +// .transition(.scale.combined(with: .opacity)) } } .navigationTitle(navTitle ?? navTitleStr) @@ -465,6 +487,7 @@ struct TransactionSummaryList: View { } } } +#if GNU_TALER .onChange(of: payNow) { payNow2 in if payNow2 { Task { @@ -473,6 +496,7 @@ struct TransactionSummaryList: View { } } } +#endif .onNotification(.TransactionExpired) { notification in // TODO: Alert user that this tx just expired if checkDismiss(notification, "newTxState.major == expired => dismiss sheet") { diff --git a/TalerWallet1/Views/Transactions/TransactionTypeDetail.swift b/TalerWallet1/Views/Transactions/TransactionTypeDetail.swift @@ -16,10 +16,14 @@ struct TransactionTypeDetail: View { @Binding var transaction: TalerTransaction let tosAccepted: Bool? @Binding var tosBaseUrl: String // might be empty +#if GNU_TALER @Binding var payNow: Bool @Binding var selectedChoice: Int? - @Binding var scope: ScopeInfo? @Binding var effective: Amount? +#else + @Binding var isFinished: Bool? +#endif + @Binding var scope: ScopeInfo? @Binding var possible: Bool? let hasDone: Bool @@ -158,15 +162,23 @@ struct TransactionTypeDetail: View { depositTransactionV(common, depositTransaction.details) case .payment(let paymentTransaction): +#if GNU_TALER PaymentTransactionView(stack: stack.push(), common: common, paymentTransaction: paymentTransaction, scope: $scope, - effective: $effective, possible: $possible, + effective: $effective, payNow: $payNow, selectedChoice: $selectedChoice) - +#else + PaymentTransactionView(stack: stack.push(), + common: common, + paymentTransaction: paymentTransaction, + scope: $scope, + possible: $possible, + isFinished: $isFinished) +#endif case .refund(let refundTransaction): Group { let details = refundTransaction.details // TODO: more details, details.info?.merchant.name ThreeAmountsSheet(stack: stack.push(),