gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taler-ios] 05/35: insufficientBalance


From: gnunet
Subject: [taler-taler-ios] 05/35: insufficientBalance
Date: Thu, 27 Jul 2023 09:09:38 +0200

This is an automated email from the git hooks/post-receive script.

marc-stibane pushed a commit to branch master
in repository taler-ios.

commit 407d4a7cd017e66c830bb49752d49fdd5c7169d7
Author: Marc Stibane <marc@taler.net>
AuthorDate: Mon Jul 10 13:27:59 2023 +0200

    insufficientBalance
---
 TalerWallet1/Model/Model+Payment.swift             |  8 +--
 TalerWallet1/Views/Payment/PaymentURIView.swift    | 71 ++++++++++++++--------
 TalerWallet1/Views/Transactions/ThreeAmounts.swift | 12 ++--
 3 files changed, 58 insertions(+), 33 deletions(-)

diff --git a/TalerWallet1/Model/Model+Payment.swift 
b/TalerWallet1/Model/Model+Payment.swift
index ac1d421..51e8d57 100644
--- a/TalerWallet1/Model/Model+Payment.swift
+++ b/TalerWallet1/Model/Model+Payment.swift
@@ -125,11 +125,11 @@ struct PayMerchantInsufficientBalanceDetails: Codable {
 }
 
 /// The result from PreparePayForUri
-struct PaymentDetailsForUri: Codable {
+struct PreparePayResult: Codable {
     let status: PreparePayResultType
     let transactionId: String
     let contractTerms: MerchantContractTerms
-    let contractTermsHash: String
+    let contractTermsHash: String?                              // only if 
status != insufficientBalance
     let amountRaw: Amount
     let amountEffective: Amount?                                // only if 
status != insufficientBalance
     let balanceDetails: PayMerchantInsufficientBalanceDetails?  // only if 
status == insufficientBalance
@@ -138,7 +138,7 @@ struct PaymentDetailsForUri: Codable {
 }
 /// A request to get an exchange's payment contract terms.
 fileprivate struct PreparePayForUri: WalletBackendFormattedRequest {
-    typealias Response = PaymentDetailsForUri
+    typealias Response = PreparePayResult
     func operation() -> String { return "preparePayForUri" }
     func args() -> Args { return Args(talerPayUri: talerPayUri) }
 
@@ -170,7 +170,7 @@ extension WalletModel {
     /// load payment details. Networking involved
     @MainActor
     func preparePayForUriM(_ talerPayUri: String)       // M for MainActor
-      async throws -> PaymentDetailsForUri {
+      async throws -> PreparePayResult {
           let request = PreparePayForUri(talerPayUri: talerPayUri)
           let response = try await sendRequest(request, ASYNCDELAY)
           return response
diff --git a/TalerWallet1/Views/Payment/PaymentURIView.swift 
b/TalerWallet1/Views/Payment/PaymentURIView.swift
index a42ddc2..b1fb0bf 100644
--- a/TalerWallet1/Views/Payment/PaymentURIView.swift
+++ b/TalerWallet1/Views/Payment/PaymentURIView.swift
@@ -20,11 +20,11 @@ struct PaymentURIView: View {
 
     @EnvironmentObject private var model: WalletModel
 
-    func acceptAction(detailsForUri: PaymentDetailsForUri) {
+    func acceptAction(preparePayResult: PreparePayResult) {
         Task {
             do {
-                let confirmPayResult = try await 
model.confirmPayM(detailsForUri.transactionId)
-                symLog.log(confirmPayResult as Any)
+                let confirmPayResult = try await 
model.confirmPayM(preparePayResult.transactionId)
+//                symLog.log(confirmPayResult as Any)
                 if confirmPayResult.type != "done" {
                     controller.playSound(0)
                     // TODO: show error
@@ -38,30 +38,51 @@ struct PaymentURIView: View {
         }
     }
 
-    @State var detailsForUri: PaymentDetailsForUri? = nil
+    @State var preparePayResult: PreparePayResult? = nil
 
     var body: some View {
-        if let detailsForUri {
+        if let preparePayResult {
           ScrollViewReader { scrollView in
+            let effective = preparePayResult.amountEffective
             List {
-                let baseURL = detailsForUri.contractTerms.exchanges.first?.url
-                let raw = detailsForUri.amountRaw
-                let effective = detailsForUri.amountEffective
+                let baseURL = 
preparePayResult.contractTerms.exchanges.first?.url
+                let raw = preparePayResult.amountRaw
                 let currency = raw.currencyStr
-                let fee = try! Amount.diff(raw, effective)      // TODO: 
different currencies
-                ThreeAmountsView(topTitle: String(localized: "Amount to pay:"),
-                                topAmount: raw, fee: fee,
-                              bottomTitle: String(localized: "\(currency) to 
be spent:"),
-                             bottomAmount: effective,
-                                    large: false, pending: false, incoming: 
false,
-                                  baseURL: baseURL)
-                // TODO: payment: popup with all possible exchanges, check fees
+                let topTitle = String(localized: "Amount to pay:")
+                if let effective {
+                    // TODO: already paid
+                    let fee = try! Amount.diff(raw, effective)      // TODO: 
different currencies
+                    ThreeAmountsView(topTitle: topTitle,
+                                     topAmount: raw, fee: fee,
+                                     bottomTitle: String(localized: 
"\(currency) to be spent:"),
+                                     bottomAmount: effective,
+                                     large: false, pending: false, incoming: 
false,
+                                     baseURL: baseURL)
+                    // TODO: payment: popup with all possible exchanges, check 
fees
+                } else if let balanceDetails = preparePayResult.balanceDetails 
{    // Insufficient
+                    Text("You don't have enough \(currency)")
+                    ThreeAmountsView(topTitle: topTitle,
+                                     topAmount: raw, fee: nil,
+                                     bottomTitle: String(localized: 
"\(currency) available:"),
+                                     bottomAmount: 
balanceDetails.balanceAvailable,
+                                     large: false, pending: false, incoming: 
false,
+                                     baseURL: baseURL)
+                } else {
+                    // TODO: Error - neither effective nor balanceDetails
+                    Text("Error")
+                }
             }
             .listStyle(myListStyle.style).anyView
             .safeAreaInset(edge: .bottom) {
-                Button(navTitle, action: { acceptAction(detailsForUri: 
detailsForUri) })
-                    .buttonStyle(TalerButtonStyle(type: .prominent))
-                    .padding(.horizontal)
+                if let effective {
+                    Button(navTitle, action: { acceptAction(preparePayResult: 
preparePayResult) })
+                        .buttonStyle(TalerButtonStyle(type: .prominent))
+                        .padding(.horizontal)
+                } else {
+                    Button("Cancel", action: { dismissTop() })
+                        .buttonStyle(TalerButtonStyle(type: .bordered))
+                        .padding(.horizontal)
+                }
             }
             .navigationTitle(navTitle)
           } // ScrollViewReader
@@ -72,8 +93,8 @@ struct PaymentURIView: View {
                 .task {
                     do {
                         symLog.log(".task")
-                        let details = try await 
model.preparePayForUriM(url.absoluteString)
-                        detailsForUri = details
+                        let result = try await 
model.preparePayForUriM(url.absoluteString)
+                        preparePayResult = result
                     } catch {    // TODO: error
                         symLog.log(error.localizedDescription)
                     }
@@ -116,17 +137,19 @@ struct PaymentURIView_Previews: PreviewProvider {
                                           extra: extra
 //                                          auditors: [],
                                   )
-        let details = PaymentDetailsForUri(
+        let details = PreparePayResult(
             status: PreparePayResultType.paymentPossible,
             transactionId: "txn:payment:012345",
             contractTerms: terms,
             contractTermsHash: "termsHash",
             amountRaw: try! Amount(fromString: LONGCURRENCY + ":2.2"),
             amountEffective: try! Amount(fromString: LONGCURRENCY + ":2.4"),
-            talerUri: "talerURI"
+            balanceDetails: nil,
+            paid: nil
+//        ,   talerUri: "talerURI"
         )
         let url = URL(string: "taler://pay/some_amount")!
         
-        PaymentURIView(url: url, detailsForUri: details)
+        PaymentURIView(url: url, preparePayResult: details)
     }
 }
diff --git a/TalerWallet1/Views/Transactions/ThreeAmounts.swift 
b/TalerWallet1/Views/Transactions/ThreeAmounts.swift
index 8d85c04..2c9e94f 100644
--- a/TalerWallet1/Views/Transactions/ThreeAmounts.swift
+++ b/TalerWallet1/Views/Transactions/ThreeAmounts.swift
@@ -32,7 +32,7 @@ struct ThreeAmountsSheet: View {
 struct ThreeAmountsView: View {
     var topTitle: String
     var topAmount: Amount
-    var fee: Amount
+    var fee: Amount?
     var bottomTitle: String
     var bottomAmount: Amount
     let large: Bool
@@ -54,11 +54,13 @@ struct ThreeAmountsView: View {
                        color: labelColor,
                        large: large)
                 .padding(.bottom, 4)
-            AmountView(title: "Exchange fee:",
-                       value: feeSign + fee.readableDescription,
-                       color: fee.isZero ? labelColor : feeColor,
-                       large: false)
+            if let fee {
+                AmountView(title: "Exchange fee:",
+                           value: feeSign + fee.readableDescription,
+                           color: fee.isZero ? labelColor : feeColor,
+                           large: false)
                 .padding(.bottom, 4)
+            }
             AmountView(title: bottomTitle,
                        value: bottomAmount.readableDescription,
                        color: foreColor,

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]