taler-ios

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

WalletEmptyView.swift (11599B)


      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 SymLog
     10 import taler_swift
     11 
     12 struct ProdSectionView: View {
     13     let stack: CallStack
     14     @Binding var showRealCashHint: Bool
     15     let isEmpty: Bool
     16     let disabled: Bool
     17 
     18     @Environment(\.colorScheme) private var colorScheme
     19     @Environment(\.colorSchemeContrast) private var colorSchemeContrast
     20     @EnvironmentObject private var controller: Controller
     21     @EnvironmentObject private var model: WalletModel
     22 #if DEBUG
     23     @AppStorage("developerMode") var developerMode: Bool = true
     24 #else
     25     @AppStorage("developerMode") var developerMode: Bool = false
     26 #endif
     27     @AppStorage("preferredColorScheme") var preferredColorScheme: Int = 0
     28     @AppStorage("minimalistic") var minimalistic: Bool = false
     29 
     30     @State private var selectedCurrency: Int = 0
     31 
     32     func buttonAction() {
     33         withAnimation {
     34             showRealCashHint.toggle()
     35         }
     36     }
     37 
     38     var body: some View {
     39         let defaultExchanges = controller.defaultExchanges
     40         let count = defaultExchanges.count
     41         let section = Section {
     42             let chevron = Image(systemName: CHEVRON_UP)     // 􀆇
     43             if isEmpty {
     44                 Text("Welcome to Taler Wallet!")
     45                     .talerFont(.headline)
     46                 Text("To make your first payment, withdraw digital cash.")
     47                     .talerFont(.body)
     48             } else {
     49                 let withdraw = Text("Withdraw real digital cash:")
     50                     .talerFont(.body)
     51                 Button(action: buttonAction) {
     52                     HStack(alignment: .firstTextBaseline) {
     53                         if minimalistic {
     54                             withdraw
     55                         } else {
     56                             Text("Currently you have only demo cash.")
     57                                 .talerFont(.body)
     58                         }
     59                         Spacer()
     60                         chevron.rotationEffect(.degrees((showRealCashHint ?  -180 : 0)))
     61                             .accessibilityHidden(true)
     62                     }
     63                     .accessibilityElement(children: .combine)
     64                     .accessibilityHint(Text(showRealCashHint ? "Hide available real money withdrawals"
     65                                                              : "Show available real money withdrawals"))
     66                     .contentShape(.rect)    // can tap in background also
     67                 }   .buttonStyle(.plain)
     68                 if showRealCashHint && !minimalistic {
     69                     withdraw
     70                         .padding(.vertical, -16)
     71                 }
     72             }
     73 #if false
     74             let qrButton = Image(systemName: QRBUTTON)                      // 􀎻 "qrcode.viewfinder"
     75             let settings = Image(systemName: SETTINGS)                      // 􀍟 "gear"
     76             Text("Use «\(qrButton) Scan QR code» in the Actions menu to start a withdrawal if your bank already supports Taler payments.", comment: "« 􀎻 » 'qrcode.viewfinder'")
     77                 .talerFont(.body)
     78             Text("You can also add a payment service manually in the \(settings) Settings tab.", comment: "« 􀍟 » 'gear'")
     79                 .talerFont(.body)
     80 #endif
     81             if (showRealCashHint) {
     82 #if !TALER_WALLET
     83                 if developerMode {
     84                     if defaultExchanges.count > 1 {
     85                         CurrencyPicker(stack: stack.push(), value: $selectedCurrency,
     86                                    exchanges: defaultExchanges) { index in
     87                             selectedCurrency = index
     88                         }
     89                     }
     90                 }
     91 #endif
     92                 let defaultExchange = defaultExchanges[selectedCurrency < count ? selectedCurrency : 0]
     93                 let currency = defaultExchange.currency
     94                 let title = String(localized: "LinkTitle_Withdraw", defaultValue: "Withdraw \(currency)")
     95                 Button(title) {
     96                     if let talerUri = URL(string: defaultExchange.talerUri) {
     97                         controller.talerURI = talerUri
     98                     }
     99                 }
    100                 .buttonStyle(TalerButtonStyle(type: isEmpty ? .prominent : .bordered,
    101                                             narrow: false,
    102                                           disabled: disabled,
    103                                            aligned: .center))
    104                 .padding(.top, -6)
    105                 .padding(.bottom, 6)
    106             } // showDropdown
    107         } header: {
    108             if isEmpty {
    109                 let firstHeader = EMPTYSTRING
    110                 Text(firstHeader)
    111                     .talerFont(.badge)
    112 //                    .foregroundColor(.primary)
    113                 /// YIKES! when not specifying this color, the Text in the BarGraphHeader vanishes!!!
    114                     .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
    115 //                    .listRowInsets(EdgeInsets())  // << here !!
    116             }
    117         }
    118         .listRowSeparator(.hidden)
    119 //        .listSectionSpacing(.compact) iOS 17
    120 
    121         if preferredColorScheme == 3, #available(iOS 17.0, *) {
    122             section.listRowBackground(Color.white.opacity(0.1))
    123         } else {
    124             section
    125         }
    126     }
    127 }
    128 
    129 /// This view shows hints if a wallet is empty
    130 /// It is the very first thing the user sees after installing the app
    131 
    132 struct WalletEmptyHeader: View {
    133     let stack: CallStack
    134 
    135     @EnvironmentObject private var model: WalletModel
    136     @EnvironmentObject private var controller: Controller
    137     @Environment(\.accessibilityVoiceOverEnabled) private var voiceOverEnabled
    138 
    139     func refresh() async {
    140         controller.hapticNotification(.success)
    141 //        symLog.log("refreshing balances")
    142         await controller.loadBalances(stack.push("refreshing balances"), model)
    143     }
    144 
    145     var body: some View {
    146         let talerLogo = HStack(spacing: 2) {
    147             Image(TALER_LOGO_FULL)
    148 //                .border(Color.gray, width: 1)
    149 //            Text("TALER Wallet")
    150             Text("Wallet")
    151 //                .font(.logo(27, weight: .medium))
    152                 .font(.logo("Atkinson Hyperlegible Next", size: 27, weight: .medium))
    153                 .kerning(1.0)
    154                 .fixedSize(horizontal: true, vertical: true)
    155         }
    156             .accessibilityElement(children: .combine)
    157             .accessibilityAddTraits(.isHeader)
    158             .accessibilityLabel(Text("Taler Wallet", comment: "a11y"))
    159 
    160         let emptyView =  WalletEmptyView(stack: stack.push("isEmpty"))
    161         //                  .navigationTitle("Taler Wallet")
    162             .refreshable {
    163                 await refresh()
    164             }
    165 
    166         let logoItem = ToolbarItem(placement: .principal) {
    167             talerLogo
    168                 .padding(.top, 10)
    169         }
    170         if #available(iOS 26.0, *) {
    171             if voiceOverEnabled {
    172                 emptyView
    173                     .toolbar { logoItem }
    174             } else {
    175                 emptyView
    176                     .toolbar {
    177                         ToolbarItem(placement: .principal) {
    178                             HStack {
    179                                 SettingsButton26(sortPriority: 0)
    180                                     .hidden()
    181                                 Spacer()
    182                                 talerLogo
    183                                     .padding(.top, 10)
    184                                 Spacer()
    185                                 SettingsButton26(sortPriority: 0)
    186                                     .offset(x: 16, y: 0)
    187                             }
    188                         }
    189                     }
    190             }
    191         } else { // iOS 15..18 Settings is the 3rd tab
    192             emptyView
    193                 .toolbar { logoItem }
    194         }
    195     }
    196 }
    197 
    198 struct WalletEmptyView: View {
    199     private let symLog = SymLogV(0)
    200     let stack: CallStack
    201 
    202     @EnvironmentObject private var controller: Controller
    203     @EnvironmentObject private var model: WalletModel
    204     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
    205     @AppStorage("preferredColorScheme") var preferredColorScheme: Int = 0
    206     @State private var withDrawStarted = false
    207     @State private var urlToOpen: URL? = nil
    208     @State private var showRealCashHint = true
    209 
    210     var body: some View {
    211         let list = List {
    212             if !controller.defaultExchanges.isEmpty {
    213                 ProdSectionView(stack: stack.push(),
    214                      showRealCashHint: $showRealCashHint,
    215                               isEmpty: true, disabled: withDrawStarted)
    216             }
    217 #if GNU_TALER
    218                 Text("If you don't have a Swiss bank account, you can simply try out the demo…")
    219                     .talerFont(.body)
    220 #endif
    221 
    222 #if DEBUG
    223 //              if developerMode {
    224 //                Text("Either deposit or send your money to a friend before you delete the app, or it will be lost.")
    225 //                    .talerFont(.body)
    226 //              }
    227 #endif
    228             let section = Section {
    229 //                Text("Demo")
    230 //                    .talerFont(.headline)
    231 //                Text("Get digital cash to experience how easy you can pay online, and send money to friends & family.")
    232                 Text("Get demo cash to experience how to pay with digital cash.")
    233 //                Text("Get digital cash to experience how to pay with the money of the future.")
    234                     .talerFont(.body)
    235                     .listRowSeparator(.hidden)
    236                 let title = String(localized: "LinkTitle_Test_Money", defaultValue: "Get demo cash")
    237                 Button(title) {
    238                     withDrawStarted = true    // don't run twice
    239                     Task { // runs on MainActor
    240                         let amount = Amount(currency:  DEMOCURRENCY, cent: 2500)
    241                         symLog.log("Withdraw KUDOS")
    242                         try? await model.loadTestKudos(0, amount: amount)
    243                         DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    244                             withDrawStarted = false
    245                         }
    246                     }
    247                 }
    248                 .buttonStyle(TalerButtonStyle(type: .prominent, narrow: false, disabled: withDrawStarted, aligned: .center))
    249                 .disabled(withDrawStarted)
    250 //            } header: {
    251 //                let secondHeader = String(localized: "Demo", comment: "section header")
    252 //                Text(secondHeader)
    253 //                    .talerFont(.title3)
    254 //                    .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
    255             }
    256             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    257                 section.listRowBackground(Color.white.opacity(0.1))
    258             } else {
    259                 section
    260             }
    261         }
    262             .listStyle(myListStyle.style).anyView
    263             .talerFont(.title2)
    264             .background(FullBackground())
    265         ZStack {
    266             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    267                 list.scrollContentBackground(.hidden)
    268             } else {
    269                 list
    270             }
    271             if withDrawStarted {
    272                 RotatingTaler(size: 150, progress: true, once: false, rotationEnabled: $withDrawStarted)
    273             }
    274         }
    275         .onAppear() {
    276             DebugViewC.shared.setViewID(VIEW_EMPTY_WALLET, stack: stack.push("onAppear"))     // 10
    277         }
    278     }
    279 }
    280 
    281 struct WalletEmptyView_Previews: PreviewProvider {
    282     static var previews: some View {
    283         WalletEmptyView(stack: CallStack("Preview"))
    284     }
    285 }