taler-ios

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

commit 4f2b20ca9bd0559742f20a418f5f03f72f521165
parent a383bd58a186b7200d13fb088aa7faf6ffd3f998
Author: Marc Stibane <marc@taler.net>
Date:   Wed, 22 Feb 2023 16:15:57 +0100

Transaction list and details

Diffstat:
MTalerWallet1/Backend/Transaction.swift | 39++++++++++++++++++++++++++++++---------
MTalerWallet1/Views/Transactions/TransactionDetail.swift | 72+++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
MTalerWallet1/Views/Transactions/TransactionRow.swift | 13++++++++++---
3 files changed, 97 insertions(+), 27 deletions(-)

diff --git a/TalerWallet1/Backend/Transaction.swift b/TalerWallet1/Backend/Transaction.swift @@ -35,6 +35,14 @@ struct TransactionCommon: Decodable { var extendedStatus: String // TODO: enum with some fixed values? var pending: Bool var frozen: Bool + + func fee() -> Amount { + do { + return try Amount.diff(amountRaw, amountEffective) + } catch { + return Amount(currency: amountRaw.currencyStr, integer: 0, fraction: 0) + } + } } struct WithdrawalDetails: Decodable { @@ -197,18 +205,31 @@ enum Transaction: Decodable, Hashable, Identifiable { #if DEBUG extension Transaction { // for PreViews - init(id: String, time: Timestamp) { - let common = TransactionCommon(type: WITHDRAWAL, - amountEffective: try! Amount(fromString: "Taler:4.8"), + init(incoming: Bool, id: String, time: Timestamp) { + let effective = incoming ? "Taler:4.8" : "Taler:5.2" + let common = TransactionCommon(type: incoming ? WITHDRAWAL : PAYMENT, + amountEffective: try! Amount(fromString: effective), amountRaw: try! Amount(fromString: "Taler:5"), transactionId: id, timestamp: time, extendedStatus: "done", pending: false, frozen: false) - let withdrawalDetails = WithdrawalDetails(type: WithdrawalDetails.WithdrawalType.bankIntegrated, - reservePub: "Public Key of the Exchange", - confirmed: true) - let details = WithdrawalTransactionDetails(exchangeBaseUrl: "Exchange.Demo.Taler.net", - withdrawalDetails: withdrawalDetails) - self = .withdrawal(WithdrawalTransaction(common: common, details: details)) + if incoming { + let withdrawalDetails = WithdrawalDetails(type: WithdrawalDetails.WithdrawalType.bankIntegrated, + reservePub: "Public Key of the Exchange", + confirmed: true) + let wDetails = WithdrawalTransactionDetails(exchangeBaseUrl: "Exchange.Demo.Taler.net", + withdrawalDetails: withdrawalDetails) + self = .withdrawal(WithdrawalTransaction(common: common, details: wDetails)) + } else { + let merchant = Merchant(name: "some random shop") + let info = OrderShortInfo(orderId: "some order ID", + merchant: merchant, summary: "some product summary", products: []) + let pDetails = PaymentTransactionDetails(proposalId: "some proposal ID", + status: "paid", + totalRefundRaw: try! Amount(fromString: "Taler:3.2"), + totalRefundEffective: try! Amount(fromString: "Taler:3"), + info: info) + self = .payment(PaymentTransaction(common: common, details: pDetails)) + } } } #endif diff --git a/TalerWallet1/Views/Transactions/TransactionDetail.swift b/TalerWallet1/Views/Transactions/TransactionDetail.swift @@ -21,26 +21,32 @@ struct TransactionDetail: View { var body: some View { let common = transaction.common() - let raw = common.amountRaw - let effective = common.amountEffective - let fee = try! Amount.diff(raw, effective) // TODO: different currencies + let details = transaction.detailsToShow() let dateString = TalerDater.dateString(from: common.timestamp) VStack() { Spacer() - Text("\(dateString)") + Text("\(common.type)") // TODO: translation .font(.title) .fontWeight(.medium) .padding(.bottom) - AmountView(title: "Chosen amount to withdraw:", - value: raw.readableDescription, color: Color(UIColor.label)) - .padding(.bottom) - AmountView(title: "Exchange fee:", - value: fee.readableDescription, color: Color("Outgoing")) - .padding(.bottom) - AmountView(title: "Obtained coins:", - value: effective.readableDescription, color: Color("Incoming")) - .padding(.bottom) + Text("\(dateString)") + .font(.title) + .fontWeight(.medium) + .padding(.vertical) + switch transaction { + case .withdrawal(let withdrawalTransaction): + threeAmounts(common: common, topTitle: "Chosen amount to withdraw:", bottomTitle: "Obtained coins:", incoming: true) + case .payment(let paymentTransaction): + threeAmounts(common: common, topTitle: "Sum to be paid:", bottomTitle: "Paid coins:", incoming: false) + case .refund(let refundTransaction): + threeAmounts(common: common, topTitle: "Refunded amount:", bottomTitle: "Obtained coins:", incoming: true) + case .tip(let tipTransaction): + threeAmounts(common: common, topTitle: "Tip to be paid:", bottomTitle: "Paid coins:", incoming: false) + case .refresh(let refreshTransaction): + threeAmounts(common: common, topTitle: "Refreshed amount:", bottomTitle: "Paid coins:", incoming: false) + } + // if let baseURL = transaction.exchangeBaseUrl { // VStack { // Text("From exchange:") @@ -70,11 +76,47 @@ struct TransactionDetail: View { } } +extension TransactionDetail { + struct threeAmounts: View { + var common: TransactionCommon + var topTitle: String + var bottomTitle: String + var incoming: Bool + + var body: some View { + let raw = common.amountRaw + let effective = common.amountEffective + let fee = common.fee() + let labelColor = Color(UIColor.label) + let outColor = Color("Outgoing") + let inColor = Color("Incoming") + + AmountView(title: topTitle, + value: raw.readableDescription, color: labelColor) + .padding(.bottom) + AmountView(title: "Exchange fee:", + value: fee.readableDescription, color: fee.isZero ? labelColor : outColor) + .padding(.bottom) + AmountView(title: bottomTitle, + value: effective.readableDescription, color: incoming ? inColor : outColor) + .padding(.bottom) + } + } +} + #if DEBUG struct TransactionDetail_Previews: PreviewProvider { - static var transaction = Transaction(id:"some transActionID", time: Timestamp(from: 1_666_000_000_000)) + static var withdrawal = Transaction(incoming: true, + id: "some withdrawal ID", + time: Timestamp(from: 1_666_000_000_000)) + static var payment = Transaction(incoming: false, + id: "some payment ID", + time: Timestamp(from: 1_666_666_000_000)) static var previews: some View { - TransactionDetail(transaction: transaction) + Group { + TransactionDetail(transaction: withdrawal) + TransactionDetail(transaction: payment) + } } } #endif diff --git a/TalerWallet1/Views/Transactions/TransactionRow.swift b/TalerWallet1/Views/Transactions/TransactionRow.swift @@ -76,10 +76,17 @@ struct TransactionRow: View { #if DEBUG struct TransactionRow_Previews: PreviewProvider { - static var transaction = Transaction(id: "some transActionID", - time: Timestamp(from: 1_666_000_000_000)) + static var withdrawal = Transaction(incoming: true, + id: "some withdrawal ID", + time: Timestamp(from: 1_666_000_000_000)) + static var payment = Transaction(incoming: false, + id: "some payment ID", + time: Timestamp(from: 1_666_666_000_000)) static var previews: some View { - TransactionRow(transaction: transaction) + VStack { + TransactionRow(transaction: withdrawal) + TransactionRow(transaction: payment) + } } } #endif