taler-ios

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

WalletEmptyView.swift (12412B)


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