taler-ios

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

TransactionTypeDetail.swift (16461B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-26 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 import taler_swift
     10 import SymLog
     11 
     12 
     13 struct TransactionTypeDetail: View {
     14     private let symLog = SymLogV(0)
     15     let stack: CallStack
     16     @Binding var transaction: TalerTransaction
     17     let tosAccepted: Bool?
     18     @Binding var tosBaseUrl: String         // might be empty
     19 #if GNU_TALER
     20     @Binding var payNow: Bool
     21     @Binding var selectedChoice: Int?
     22     @Binding var effective: Amount?
     23 #else
     24     @Binding var isFinished: Bool?
     25 #endif
     26     @Binding var scope: ScopeInfo?
     27     @Binding var possible: Bool?
     28     let hasDone: Bool
     29 
     30     @EnvironmentObject private var controller: Controller
     31     @EnvironmentObject private var model: WalletModel
     32     @Environment(\.colorScheme) private var colorScheme
     33     @Environment(\.colorSchemeContrast) private var colorSchemeContrast
     34     @AppStorage("minimalistic") var minimalistic: Bool = false
     35 #if DEBUG
     36     @AppStorage("developerMode") var developerMode: Bool = true
     37 #else
     38     @AppStorage("developerMode") var developerMode: Bool = false
     39 #endif
     40     @State private var rotationEnabled = true
     41     @State private var ignoreAccept: Bool = false         // accept could be set by OIM to trigger accept
     42 
     43     func refreshFee(input: Amount, output: Amount) -> Amount? {
     44         do {
     45             let fee = try input - output
     46             return fee
     47         } catch {
     48 
     49         }
     50         return nil
     51     }
     52 
     53     func abortedHint(_ delay: Duration?) -> UInt? {
     54         if let delay {
     55             if let microseconds = try? delay.microseconds() {
     56                 let days = microseconds / (24 * 3600 * 1000 * 1000)
     57                 if days > 0 {
     58                     return UInt(days)
     59                 }
     60             }
     61             return 0
     62         }
     63         return nil
     64     }
     65 
     66     func errorHintStack(_ hint: String?, _ stack: String?) -> String? {
     67         if let hint, !hint.isEmpty {
     68             if let stack, !stack.isEmpty {
     69                 return hint + LINEFEED + stack
     70             }
     71             return hint
     72         }
     73         if let stack, !stack.isEmpty {
     74             return stack
     75         }
     76         return nil
     77     }
     78 
     79     @ViewBuilder func dummyTransactionV(_ common: TransactionCommon) -> some View {
     80 #if DEBUG
     81         let background = developerMode ? Color.orange : nil
     82 #else
     83         let background: Color? = nil
     84 #endif
     85         Section {
     86             if common.isWorking {
     87                 HStack {
     88                     Spacer()
     89                     RotatingTaler(size: 100, progress: true, once: false,
     90                             background: background,
     91                        rotationEnabled: $rotationEnabled)
     92                     .frame(maxWidth: .infinity, alignment: .center)
     93                     // has its own accessibilityLabel
     94                 }
     95             } else {
     96                 let title = EMPTYSTRING
     97                 Text(title)
     98                     .talerFont(.body)
     99             }
    100         }
    101     }
    102 
    103     @ViewBuilder func withdrawalTransactionV(_ common: TransactionCommon,
    104                                             _ details: WithdrawalTransactionDetails) -> some View {
    105         if common.isAborted && details.withdrawalDetails.type == .manual {
    106             if let days = abortedHint(details.withdrawalDetails.reserveClosingDelay) {
    107                 let wireBack = days > 0 ? String(localized: "If you have already sent money to the payment service, it will wire it back in \(days) days.")
    108                                         : String(localized: "If you have already sent money to the payment service, it will wire it back in a few days.")
    109                 Text("The withdrawal was aborted.\n\n\(wireBack)")
    110                     .talerFont(.callout)
    111             }
    112         }
    113         if common.isPending {
    114             PendingWithdrawalDetails(stack: stack.push(),
    115                                      transaction: $transaction,
    116                                      details: details)
    117         } // ManualDetails or Confirm now (with bank)
    118         ThreeAmountsSheet(stack: stack.push(),
    119                           scope: scope,
    120                          common: common,
    121                       topAbbrev: String(localized: "Chosen:", comment: "mini"),
    122                        topTitle: String(localized: "Chosen amount to withdraw:"),
    123                         baseURL: details.exchangeBaseUrl,
    124                          noFees: nil,               // TODO: noFees
    125                   feeIsNegative: true,
    126                           large: false,
    127                          header: nil)
    128     }
    129 
    130     @ViewBuilder func depositTransactionV(_ common: TransactionCommon,
    131                                           _ details: DepositTransactionDetails) -> some View {
    132         if common.isPendingKYCauth {
    133             KYCauth(stack: stack.push(), common: common)
    134         } else if transaction.isPendingKYC {
    135             KYCbutton(kycUrl: common.kycUrl)
    136         }
    137         ThreeAmountsSheet(stack: stack.push(),
    138                           scope: scope,
    139                          common: common,
    140                       topAbbrev: String(localized: "Deposit:", comment: "mini"),
    141                        topTitle: String(localized: "Amount to deposit:"),
    142                         baseURL: nil,               // TODO: baseURL
    143                          noFees: nil,               // TODO: noFees
    144                   feeIsNegative: false,
    145                           large: true,
    146                          header: nil)
    147     }
    148     var body: some View {
    149 #if PRINT_CHANGES
    150         let _ = Self._printChanges()
    151         let _ = symLog.vlog()
    152 #endif
    153         let common = transaction.common
    154         let pending = transaction.isPending
    155         let isDialog = transaction.isDialog
    156         Group {
    157             switch transaction {
    158                 case .dummy(_):     dummyTransactionV(common)
    159                 case .withdrawal(let withdrawalTransaction):
    160                     withdrawalTransactionV(common, withdrawalTransaction.details)
    161                 case .deposit(let depositTransaction):
    162                     depositTransactionV(common, depositTransaction.details)
    163 
    164                 case .payment(let paymentTransaction):
    165 #if GNU_TALER
    166                     PaymentTransactionView(stack: stack.push(),
    167                                           common: common,
    168                               paymentTransaction: paymentTransaction,
    169                                            scope: $scope,
    170                                         possible: $possible,
    171                                        effective: $effective,
    172                                           payNow: $payNow,
    173                                   selectedChoice: $selectedChoice)
    174 #else
    175                     PaymentTransactionView(stack: stack.push(),
    176                                           common: common,
    177                               paymentTransaction: paymentTransaction,
    178                                            scope: $scope,
    179                                         possible: $possible,
    180                                       isFinished: $isFinished)
    181 #endif
    182                 case .refund(let refundTransaction): Group {
    183                     let details = refundTransaction.details                 // TODO: more details, details.info?.merchant.name
    184                     ThreeAmountsSheet(stack: stack.push(),
    185                                       scope: scope,
    186                                      common: common,
    187                                   topAbbrev: String(localized: "Refunded:", comment: "mini"),
    188                                    topTitle: String(localized: "Refunded amount:"),
    189                                     baseURL: nil,               // TODO: baseURL
    190                                      noFees: nil,               // TODO: noFees
    191                               feeIsNegative: true,
    192                                       large: true,
    193                                      header: details.paymentInfo?.summary)
    194                 }
    195                 case .refresh(let refreshTransaction): Group {
    196                     let labelColor = WalletColors().labelColor
    197                     let errorColor = WalletColors().errorColor
    198                     let details = refreshTransaction.details
    199                     Section {
    200                         Text(details.refreshReason.localizedRefreshReason)
    201                             .talerFont(.title)
    202                         let input = details.refreshInputAmount
    203                         AmountRowV(stack: stack.push(),
    204                                    title: minimalistic ? "Refreshed:" : "Refreshed amount:",
    205                                   amount: input,
    206                                    scope: scope,
    207                               isNegative: nil,
    208                                    color: labelColor,
    209                                    large: true)
    210                         if let fee = refreshFee(input: input, output: details.refreshOutputAmount) {
    211                             AmountRowV(stack: stack.push(),
    212                                        title: minimalistic ? "Fee:" : "Refreshed fee:",
    213                                       amount: fee,
    214                                        scope: scope,
    215                                   isNegative: fee.isZero ? nil : true,
    216                                        color: labelColor,
    217                                        large: true)
    218                         }
    219                         if let error = details.error {
    220                             HStack {
    221                                 VStack(alignment: .leading) {
    222                                     if let hint = error.hint, !hint.isEmpty {
    223                                         Text(hint)
    224                                             .talerFont(.headline)
    225                                             .foregroundColor(errorColor)
    226                                             .listRowSeparator(.hidden)
    227                                     }
    228                                     if let stack = error.stack, !stack.isEmpty {
    229                                         Text(stack)
    230                                             .talerFont(.body)
    231                                             .foregroundColor(errorColor)
    232                                             .listRowSeparator(.hidden)
    233                                     }
    234                                 }
    235                                 if let errorStr = errorHintStack(error.hint, error.stack) {
    236                                     CopyButton(errorStr, vertical: true)
    237                                         .accessibilityLabel(Text("Copy the error", comment: "a11y"))
    238                                         .disabled(false)
    239                                 }
    240                             }
    241                         }
    242                     }
    243                 }
    244 
    245                 case .peer2peer(let p2pTransaction): Group {
    246                     let details = p2pTransaction.details
    247                     if transaction.isPendingKYC {
    248                         KYCbutton(kycUrl: common.kycUrl)
    249                     }
    250                     if !transaction.isDone {
    251                         ExpiresView(expiration: details.info.expiration)
    252                     }
    253                     if transaction.isRcvCoins && common.isDialog {
    254                         PeerPushCreditView(stack: stack.push(),
    255                                              raw: common.amountRaw,
    256                                        effective: common.amountEffective,
    257                                           isDone: common.isDone,
    258                                            scope: scope,
    259                                           header: details.info.summary)
    260                         .task {      // tell caller which exchange to check for ToS-accepted
    261                             tosBaseUrl = details.exchangeBaseUrl
    262                         }
    263                         if tosAccepted == true {
    264                             PeerPushCreditAccept(stack: stack.push(), url: nil,
    265                                          transactionId: common.transactionId,
    266                                                 accept: $ignoreAccept)
    267                         }
    268                     } else if transaction.isPayInvoice && common.isDialog {
    269                         PeerPullDebitView(stack: stack.push(),
    270                                             raw: common.amountRaw,
    271                                       effective: common.amountEffective,
    272                                          isDone: common.isDone,
    273                                           scope: scope,
    274                                          header: details.info.summary)
    275                         PeerPullDebitConfirm(stack: stack.push(), url: nil,
    276                                      transactionId: common.transactionId)
    277                     } else {
    278                     // TODO: isSendCoins should show QR only while not yet expired  - either set timer or wallet-core should do so and send a state-changed notification
    279                         // TODO: details.info.summary
    280                     if pending {
    281                         if transaction.isPendingReady {
    282                             QRCodeDetails(stack: stack.push(), transaction: transaction)
    283                             if hasDone {
    284                                 Text("QR code and link can also be scanned or copied / shared from Transactions later.")
    285                                     .multilineTextAlignment(.leading)
    286                                     .talerFont(.subheadline)
    287 //                                    .padding(.top)
    288                             }
    289                         } else {
    290                             Text("This transaction is not yet ready...")
    291                                 .multilineTextAlignment(.leading)
    292                                 .talerFont(.subheadline)
    293                         }
    294                     }
    295                     let colon = ":"     // TODO: French l10n has a space before the colon
    296                     let localizedType = transaction.isDone ? transaction.localizedTypePast
    297                                                            : transaction.localizedType
    298                     ThreeAmountsSheet(stack: stack.push(),
    299                                       scope: scope,
    300                                      common: common,
    301                                   topAbbrev: localizedType + colon,
    302                                    topTitle: localizedType + colon,
    303                                     baseURL: details.exchangeBaseUrl,
    304                                      noFees: nil,         // TODO: noFees
    305                               feeIsNegative: true,
    306                                       large: false,
    307                                      header: details.info.summary)
    308                     } // else
    309                 } // p2p
    310 
    311                 case .recoup(let recoupTransaction): Group {
    312                     let details = recoupTransaction.details     // TODO: details.recoupReason
    313                     ThreeAmountsSheet(stack: stack.push(),
    314                                       scope: scope,
    315                                      common: common,
    316                                   topAbbrev: String(localized: "Recoup:", comment: "mini"),
    317                                    topTitle: String(localized: "Recoup:"),
    318                                     baseURL: nil,               // TODO: baseURL, noFees
    319                                      noFees: nil,
    320                               feeIsNegative: nil,
    321                                       large: true,
    322                                      header: details.recoupReason)
    323                 }
    324 
    325                 case .denomLoss(let denomLossTransaction): Group {
    326                     let details = denomLossTransaction.details              // TODO: more details, details.lossEventType.rawValue
    327                     ThreeAmountsSheet(stack: stack.push(),
    328                                       scope: scope,
    329                                      common: common,
    330                                   topAbbrev: String(localized: "Lost:", comment: "mini"),
    331                                    topTitle: String(localized: "Money lost:"),
    332                                     baseURL: details.exchangeBaseUrl,
    333                                      noFees: nil,               // TODO: noFees
    334                               feeIsNegative: nil,
    335                                       large: true,
    336                                      header: details.lossEventType.rawValue)
    337                 }
    338             } // switch
    339         } // Group
    340     }
    341 }