taler-ios

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

WalletEmptyView.swift (11577B)


      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 developerMode {
     83                     if defaultExchanges.count > 1 {
     84                         CurrencyPicker(stack: stack.push(), value: $selectedCurrency,
     85                                    exchanges: defaultExchanges) { index in
     86                             selectedCurrency = index
     87                         }
     88                     }
     89                 }
     90                 let defaultExchange = defaultExchanges[selectedCurrency < count ? selectedCurrency : 0]
     91                 let currency = defaultExchange.currency
     92                 let title = String(localized: "LinkTitle_Withdraw", defaultValue: "Withdraw \(currency)")
     93                 Button(title) {
     94                     if let talerUri = URL(string: defaultExchange.talerUri) {
     95                         controller.talerURI = talerUri
     96                     }
     97                 }
     98                 .buttonStyle(TalerButtonStyle(type: isEmpty ? .prominent : .bordered,
     99                                             narrow: false,
    100                                           disabled: disabled,
    101                                            aligned: .center))
    102                 .padding(.top, -6)
    103                 .padding(.bottom, 6)
    104             } // showDropdown
    105         } header: {
    106             if isEmpty {
    107                 let firstHeader = EMPTYSTRING
    108                 Text(firstHeader)
    109                     .talerFont(.badge)
    110 //                    .foregroundColor(.primary)
    111                 /// YIKES! when not specifying this color, the Text in the BarGraphHeader vanishes!!!
    112                     .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
    113 //                    .listRowInsets(EdgeInsets())  // << here !!
    114             }
    115         }
    116         .listRowSeparator(.hidden)
    117 //        .listSectionSpacing(.compact) iOS 17
    118 
    119         if preferredColorScheme == 3, #available(iOS 17.0, *) {
    120             section.listRowBackground(Color.white.opacity(0.1))
    121         } else {
    122             section
    123         }
    124     }
    125 }
    126 
    127 /// This view shows hints if a wallet is empty
    128 /// It is the very first thing the user sees after installing the app
    129 
    130 struct WalletEmptyHeader: View {
    131     let stack: CallStack
    132 
    133     @EnvironmentObject private var model: WalletModel
    134     @EnvironmentObject private var controller: Controller
    135     @Environment(\.accessibilityVoiceOverEnabled) private var voiceOverEnabled
    136 
    137     func refresh() async {
    138         controller.hapticNotification(.success)
    139 //        symLog.log("refreshing balances")
    140         await controller.loadBalances(stack.push("refreshing balances"), model)
    141     }
    142 
    143     var body: some View {
    144         let talerLogo = HStack(spacing: 2) {
    145             Image(TALER_LOGO_FULL)
    146 //                .border(Color.gray, width: 1)
    147 //            Text("TALER Wallet")
    148             Text("Wallet")
    149 //                .font(.logo(27, weight: .medium))
    150                 .font(.logo("Atkinson Hyperlegible Next", size: 27, weight: .medium))
    151                 .kerning(1.0)
    152                 .fixedSize(horizontal: true, vertical: true)
    153         }
    154             .accessibilityElement(children: .combine)
    155             .accessibilityAddTraits(.isHeader)
    156             .accessibilityLabel(Text("Taler Wallet", comment: "a11y"))
    157 
    158         let emptyView =  WalletEmptyView(stack: stack.push("isEmpty"))
    159         //                  .navigationTitle("Taler Wallet")
    160             .refreshable {
    161                 await refresh()
    162             }
    163 
    164         let logoItem = ToolbarItem(placement: .principal) {
    165             talerLogo
    166                 .padding(.top, 10)
    167         }
    168         if #available(iOS 26.0, *) {
    169             if voiceOverEnabled {
    170                 emptyView
    171                     .toolbar { logoItem }
    172             } else {
    173                 emptyView
    174                     .toolbar {
    175                         ToolbarItem(placement: .principal) {
    176                             HStack {
    177                                 SettingsButton26(sortPriority: 0)
    178                                     .hidden()
    179                                 Spacer()
    180                                 talerLogo
    181                                     .padding(.top, 10)
    182                                 Spacer()
    183                                 SettingsButton26(sortPriority: 0)
    184                                     .offset(x: 16, y: 0)
    185                             }
    186                         }
    187                     }
    188             }
    189         } else { // iOS 15..18 Settings is the 3rd tab
    190             emptyView
    191                 .toolbar { logoItem }
    192         }
    193     }
    194 }
    195 
    196 struct WalletEmptyView: View {
    197     private let symLog = SymLogV(0)
    198     let stack: CallStack
    199 
    200     @EnvironmentObject private var controller: Controller
    201     @EnvironmentObject private var model: WalletModel
    202     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
    203     @AppStorage("preferredColorScheme") var preferredColorScheme: Int = 0
    204     @State private var withDrawStarted = false
    205     @State private var urlToOpen: URL? = nil
    206     @State private var showRealCashHint = true
    207 
    208     var body: some View {
    209         let list = List {
    210             if !controller.defaultExchanges.isEmpty {
    211                 ProdSectionView(stack: stack.push(),
    212                      showRealCashHint: $showRealCashHint,
    213                               isEmpty: true, disabled: withDrawStarted)
    214             }
    215 #if GNU_TALER
    216                 Text("If you don't have a Swiss bank account, you can simply try out the demo…")
    217                     .talerFont(.body)
    218 #endif
    219 
    220 #if DEBUG
    221 //              if developerMode {
    222 //                Text("Either deposit or send your money to a friend before you delete the app, or it will be lost.")
    223 //                    .talerFont(.body)
    224 //              }
    225 #endif
    226             let section = Section {
    227 //                Text("Demo")
    228 //                    .talerFont(.headline)
    229 //                Text("Get digital cash to experience how easy you can pay online, and send money to friends & family.")
    230                 Text("Get demo cash to experience how to pay with digital cash.")
    231 //                Text("Get digital cash to experience how to pay with the money of the future.")
    232                     .talerFont(.body)
    233                     .listRowSeparator(.hidden)
    234                 let title = String(localized: "LinkTitle_Test_Money", defaultValue: "Get demo cash")
    235                 Button(title) {
    236                     withDrawStarted = true    // don't run twice
    237                     Task { // runs on MainActor
    238                         let amount = Amount(currency:  DEMOCURRENCY, cent: 2500)
    239                         symLog.log("Withdraw 25 KUDOS")
    240                         try? await model.loadTestKudos(0, amount: amount)
    241                         DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
    242                             withDrawStarted = false
    243                         }
    244                     }
    245                 }
    246                 .buttonStyle(TalerButtonStyle(type: .prominent, narrow: false, disabled: withDrawStarted, aligned: .center))
    247                 .disabled(withDrawStarted)
    248 //            } header: {
    249 //                let secondHeader = String(localized: "Demo", comment: "section header")
    250 //                Text(secondHeader)
    251 //                    .talerFont(.title3)
    252 //                    .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
    253             }
    254             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    255                 section.listRowBackground(Color.white.opacity(0.1))
    256             } else {
    257                 section
    258             }
    259         }
    260             .listStyle(myListStyle.style).anyView
    261             .talerFont(.title2)
    262             .background(FullBackground())
    263         ZStack {
    264             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    265                 list.scrollContentBackground(.hidden)
    266             } else {
    267                 list
    268             }
    269             if withDrawStarted {
    270                 RotatingTaler(size: 150, progress: true, once: false, rotationEnabled: $withDrawStarted)
    271             }
    272         }
    273         .onAppear() {
    274             DebugViewC.shared.setViewID(VIEW_EMPTY_WALLET, stack: stack.push("onAppear"))     // 10
    275         }
    276     }
    277 }
    278 
    279 struct WalletEmptyView_Previews: PreviewProvider {
    280     static var previews: some View {
    281         WalletEmptyView(stack: CallStack("Preview"))
    282     }
    283 }