taler-ios

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

ExchangeRowView.swift (6555B)


      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 struct ExchangeRowView: View {
     13     private let symLog = SymLogV(0)
     14     let stack: CallStack
     15     let exchange: Exchange
     16     let index: Int
     17     let developerMode: Bool
     18 
     19     @Environment(\.sizeCategory) var sizeCategory
     20     @EnvironmentObject private var controller: Controller
     21     @EnvironmentObject private var model: WalletModel
     22     @EnvironmentObject private var tabBarModel: TabBarModel
     23     
     24     @State private var isUpdated: Bool = false
     25     @State private var detailView: Int? = nil
     26 
     27     var body: some View {
     28 #if PRINT_CHANGES
     29         let _ = Self._printChanges()
     30         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     31         let delay: UInt = 0     // set to 5 to test delayed currency information
     32 #else
     33         let delay: UInt = 0
     34 #endif
     35         let baseURL = exchange.exchangeBaseUrl
     36         let chevron = Text(Image(systemName: CHEVRON))
     37                         .talerFont(.table)
     38                         .foregroundColor(.secondary)
     39 
     40         let termsLine = HStack {
     41             Text("Terms of Service")  // VIEW_WITHDRAW_TOS
     42             Spacer()
     43             chevron
     44         }
     45         let rowView = VStack(alignment: .leading) {
     46             VStack(alignment: .leading) {
     47                 Text(baseURL.trimURL)
     48                     .talerFont(.headline)
     49                 ExchangeStatus(exchange: exchange)
     50             }
     51             .background {           // only needed for .onTapGesture
     52                 Color.gray.opacity(INVISIBLE)
     53             }
     54             .onTapGesture {
     55                 detailView = index
     56             }
     57             termsLine
     58                 .talerFont(.body)
     59                 .background {           // only needed for .onTapGesture
     60                     Color.gray.opacity(INVISIBLE)
     61                 }
     62                 .onTapGesture {
     63                     tabBarModel.tosView = index
     64                 }
     65         }
     66         let showToS = WithdrawTOSView(stack: stack.push(),
     67                             exchangeBaseUrl: baseURL,
     68                               shouldDismiss: true,                  // pop back to here
     69                                      viewID: VIEW_WITHDRAW_TOS)
     70         let details = ExchangeDetailView(stack: stack.push(),       // TODO: show exchange details
     71                                       exchange: exchange)
     72         let tosActions = Group {
     73 //            NavLink(index, $detailView) { details }
     74             NavLink(index, $tabBarModel.tosView) { showToS }
     75         }
     76 
     77         Group {
     78             rowView
     79                 .background(tosActions)
     80 
     81             if developerMode {
     82                 HStack {
     83                     Text(verbatim: "Update exchange")  // VIEW_WITHDRAW_TOS
     84                         .foregroundStyle(isUpdated ? .gray : .primary)
     85                 }
     86                 .background {           // only needed for .onTapGesture
     87                     Color.gray.opacity(INVISIBLE)
     88                 }
     89                 .onTapGesture {
     90                     if !isUpdated {
     91                         isUpdated = true
     92                         Task {
     93                             try await model.updateExchange(exchangeBaseUrl: baseURL, force: true)
     94                         }
     95                     }
     96                 }
     97             }
     98         }.listRowSeparator(.hidden)
     99     }
    100 }
    101 // MARK: -
    102 struct  ExchangeStatus: View {
    103     let exchange: Exchange
    104 
    105     @EnvironmentObject private var controller: Controller
    106     @AppStorage("minimalistic") var minimalistic: Bool = false
    107 
    108     var body: some View {
    109         let status = controller.isConnected ? exchange.exchangeUpdateStatus.localized
    110                                             : ExchangeUpdateStatus.unavailable.localized
    111         VStack(alignment: .leading) {
    112             HStack {
    113                 if !minimalistic {
    114                     Text("Status:")
    115                 }
    116                 Text(status)
    117             }
    118             if let lastUpdateErrorInfo = exchange.lastUpdateErrorInfo {
    119                 Text("Last Error: \(lastUpdateErrorInfo.error.code)")
    120                 if let hint = lastUpdateErrorInfo.error.hint {
    121                     Text(hint)                                                  // TODO: L10N
    122                 }
    123                 if let detail = lastUpdateErrorInfo.error.detail {
    124                     Text(detail)                                                // TODO: L10N
    125                 }
    126             }
    127         }
    128         .padding(.bottom, 4)
    129         .talerFont(.body)
    130     }
    131 }
    132 // MARK: -
    133 #if DEBUG
    134 fileprivate struct ExchangeRow_Previews: PreviewProvider {
    135     @MainActor
    136     struct BindingViewContainer : View {
    137         @State private var amountToPreview = Amount(currency: LONGCURRENCY, cent: 1234)
    138         @State private var depositIBAN = "DE1234567890"
    139         @State private var accountHolder = "Marc Stibane"
    140 //        @State private var previewL: CurrencyInfo = CurrencyInfo.zero(LONGCURRENCY)
    141 
    142 //    let amount = Amount(currency: LONGCURRENCY, cent: 123456)
    143         var body: some View {
    144             let scopeInfo = ScopeInfo(type: .exchange, currency: LONGCURRENCY)
    145             let exchange1 = Exchange(exchangeBaseUrl: ARS_AGE_EXCHANGE,
    146                                            masterPub: "masterPub",
    147                                            scopeInfo: scopeInfo,
    148                                            paytoUris: [],
    149                                            tosStatus: .pending,
    150                                  exchangeEntryStatus: .preset,
    151                                 exchangeUpdateStatus: .initial,
    152                                ageRestrictionOptions: [12,16])
    153             let exchange2 = Exchange(exchangeBaseUrl: ARS_EXP_EXCHANGE,
    154                                            masterPub: "masterPub",
    155                                            scopeInfo: scopeInfo,
    156                                            paytoUris: [],
    157                                            tosStatus: .proposed,
    158                                  exchangeEntryStatus: .ephemeral,
    159                                 exchangeUpdateStatus: .ready,
    160                                ageRestrictionOptions: [])
    161             ExchangeRowView(stack: CallStack("Preview"),
    162                          exchange: exchange1,
    163                             index: 1,
    164                     developerMode: true)
    165         }
    166     }
    167 
    168     @MainActor
    169     static var previews: some View {
    170         List {
    171             BindingViewContainer()
    172         }
    173     }
    174 }
    175 #endif