taler-ios

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

WalletEmptyView.swift (11749B)


      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 // MARK: -
    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 struct WalletEmptyHeader: View {
    130     let stack: CallStack
    131 
    132     @EnvironmentObject private var controller: Controller
    133 
    134     func refresh() async {
    135         controller.hapticNotification(.success)
    136 //        symLog.log("refreshing balances")
    137         await controller.loadBalances(stack.push("refreshing balances"))
    138     }
    139 
    140     var body: some View {
    141         let emptyView =  WalletEmptyView(stack: stack.push("isEmpty"))
    142 //                  .navigationTitle("Taler Wallet")
    143             .refreshable {
    144                 await refresh()
    145             }
    146         WalletHeader(showSettings: true, content: emptyView)
    147     }
    148 }
    149 // MARK: -
    150 struct WalletHeader<Content: View> : View {
    151     let showSettings: Bool
    152     let content: Content
    153 
    154     @Environment(\.accessibilityVoiceOverEnabled) private var voiceOverEnabled
    155 
    156     var body: some View {
    157         let talerLogo = HStack(spacing: 2) {
    158             Image(TALER_LOGO_FULL)
    159 //                .border(Color.gray, width: 1)
    160 //            Text("TALER Wallet")
    161             Text("Wallet")
    162 //                .font(.logo(27, weight: .medium))
    163                 .font(.logo("Atkinson Hyperlegible Next", size: 27, weight: .medium))
    164                 .kerning(1.0)
    165                 .fixedSize(horizontal: true, vertical: true)
    166         }
    167             .accessibilityElement(children: .combine)
    168             .accessibilityAddTraits(.isHeader)
    169             .accessibilityLabel(Text("Taler Wallet", comment: "a11y"))
    170 
    171 
    172         let logoItem = ToolbarItem(placement: .principal) {
    173             talerLogo
    174                 .padding(.top, 10)
    175         }
    176         if showSettings, #available(iOS 26.0, *) {
    177             if voiceOverEnabled {
    178                 content
    179                     .toolbar { logoItem }
    180             } else {
    181                 content
    182                     .toolbar {
    183                         ToolbarItem(placement: .principal) {
    184                             HStack {
    185                                 SettingsButton26(sortPriority: 0)
    186                                     .hidden()
    187                                 Spacer()
    188                                 talerLogo
    189                                     .padding(.top, 10)
    190                                 Spacer()
    191                                 SettingsButton26(sortPriority: 0)
    192                                     .offset(x: 16, y: 0)
    193                             }
    194                         }
    195                     }
    196             }
    197         } else { // iOS 15..18 Settings is the 3rd tab
    198             content
    199                 .toolbar { logoItem }
    200         }
    201     }
    202 }
    203 // MARK: -
    204 struct WalletEmptyView: View {
    205     private let symLog = SymLogV(0)
    206     let stack: CallStack
    207 
    208     @EnvironmentObject private var controller: Controller
    209     @EnvironmentObject private var model: WalletModel
    210     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
    211     @AppStorage("preferredColorScheme") var preferredColorScheme: Int = 0
    212     @State private var withDrawStarted = false
    213     @State private var urlToOpen: URL? = nil
    214     @State private var showRealCashHint = true
    215 
    216     var body: some View {
    217         let list = List {
    218             if !controller.defaultExchanges.isEmpty {
    219                 ProdSectionView(stack: stack.push(),
    220                      showRealCashHint: $showRealCashHint,
    221                               isEmpty: true, disabled: withDrawStarted)
    222             }
    223 #if GNU_TALER
    224                 Text("If you don't have a Swiss bank account, you can simply try out the demo…")
    225                     .talerFont(.body)
    226 #endif
    227 
    228 #if DEBUG
    229 //              if developerMode {
    230 //                Text("Either deposit or send your money to a friend before you delete the app, or it will be lost.")
    231 //                    .talerFont(.body)
    232 //              }
    233 #endif
    234             let section = Section {
    235 //                Text("Demo")
    236 //                    .talerFont(.headline)
    237 //                Text("Get digital cash to experience how easy you can pay online, and send money to friends & family.")
    238                 Text("Get demo cash to experience how to pay with digital cash.")
    239 //                Text("Get digital cash to experience how to pay with the money of the future.")
    240                     .talerFont(.body)
    241                     .listRowSeparator(.hidden)
    242                 let title = String(localized: "LinkTitle_Test_Money", defaultValue: "Get demo cash")
    243                 Button(title) {
    244                     withDrawStarted = true    // don't run twice
    245                     Task { // runs on MainActor
    246                         let amount = Amount(currency:  DEMOCURRENCY, cent: 2500)
    247                         symLog.log("Withdraw 25 KUDOS")
    248                         try? await model.loadTestKudos(0, amount: amount)
    249                         DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
    250                             withDrawStarted = false
    251                         }
    252                     }
    253                 }
    254                 .buttonStyle(TalerButtonStyle(type: .prominent, narrow: false, disabled: withDrawStarted, aligned: .center))
    255                 .disabled(withDrawStarted)
    256 //            } header: {
    257 //                let secondHeader = String(localized: "Demo", comment: "section header")
    258 //                Text(secondHeader)
    259 //                    .talerFont(.title3)
    260 //                    .foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
    261             }
    262             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    263                 section.listRowBackground(Color.white.opacity(0.1))
    264             } else {
    265                 section
    266             }
    267         }
    268             .listStyle(myListStyle.style).anyView
    269             .talerFont(.title2)
    270             .background(FullBackground())
    271         ZStack {
    272             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    273                 list.scrollContentBackground(.hidden)
    274             } else {
    275                 list
    276             }
    277             if withDrawStarted {
    278                 RotatingTaler(size: 150, progress: true, once: false, rotationEnabled: $withDrawStarted)
    279             }
    280         }
    281         .onAppear() {
    282             DebugViewC.shared.setViewID(VIEW_EMPTY_WALLET, stack: stack.push("onAppear"))     // 10
    283         }
    284     }
    285 }
    286 // MARK: -
    287 struct WalletEmptyView_Previews: PreviewProvider {
    288     static var previews: some View {
    289         WalletEmptyView(stack: CallStack("Preview"))
    290     }
    291 }