taler-ios

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

BalancesListView.swift (5776B)


      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 import AVFoundation
     12 
     13 /// This view shows the list of balances / currencies, each in its own section
     14 struct BalancesListView: View {
     15     private let symLog = SymLogV(0)
     16     let stack: CallStack
     17     let title: String
     18     @Binding var selectedBalance: Balance?                      // set in TransactionsListView
     19     @Binding var reloadTransactions: Int
     20 
     21     @EnvironmentObject private var model: WalletModel
     22     @EnvironmentObject private var controller: Controller
     23     @Environment(\.accessibilityVoiceOverEnabled) private var voiceOverEnabled
     24     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     25     @AppStorage("demoHints") var demoHints: Bool = true
     26     @AppStorage("preferredColorScheme") var preferredColorScheme: Int = 0
     27     @AppStorage("oimEuro") var oimEuro: Bool = false
     28 
     29     @State private var amountToTransfer = Amount.zero(currency: EMPTYSTRING)    // Update currency when used
     30     @State private var summary = EMPTYSTRING
     31     @State private var showRealCashHint = true
     32 
     33     @State private var historyTapped: Int? = nil
     34     @Namespace var namespace
     35 
     36     private static func className() -> String {"\(self)"}
     37 
     38     func refresh() async {
     39         controller.hapticNotification(.success)
     40         symLog.log("refreshing balances")
     41         await controller.loadBalances(stack.push("refreshing balances"), model)
     42     }
     43 
     44     @ViewBuilder
     45     func balancesList() -> some View {
     46         let count = controller.balances.count
     47         let list = List {
     48             if !controller.haveProdBalance && !controller.defaultExchanges.isEmpty && demoHints {
     49                 ProdSectionView(stack: stack.push(),
     50                      showRealCashHint: $showRealCashHint,
     51                               isEmpty: false,
     52                              disabled: false)
     53             }
     54             ForEachWithIndex(array: controller.balances) { index, balance in
     55                 BalancesSectionView(stack: stack.push("\(balance.scopeInfo.currency)"),
     56                                   balance: balance,                     // this is the currency to be used
     57                           selectedBalance: $selectedBalance,            // set in TransactionsListView
     58                              balanceIndex: index,
     59                              sectionCount: count,
     60                          amountToTransfer: $amountToTransfer,           // does still have the wrong currency
     61                                   summary: $summary,
     62                             historyTapped: $historyTapped,
     63                        reloadTransactions: $reloadTransactions)
     64             }
     65             if count > 1 {      // otherwise it comes before recentTransactions
     66                 DiscountPassesSection(stack: stack.push())
     67             }
     68         }
     69             .listStyle(myListStyle.style).anyView
     70             .background(FullBackground())
     71             .onAppear() {
     72                 DebugViewC.shared.setViewID(VIEW_BALANCES, stack: stack.push("onAppear"))
     73                 if !controller.oimModeActive {
     74                     print("🚩BalancesListView.onAppear() reset selectedBalance")
     75 //                  if count > 1 {
     76                         selectedBalance = nil                           // reset
     77 //                  }
     78                 }
     79             }
     80             .refreshable {
     81                 await refresh()
     82             }
     83             .navigationTitle(title)
     84         if voiceOverEnabled {
     85             list
     86         } else if #available(iOS 26.0, *) {
     87             let list26 = list
     88                 .toolbar {
     89                     ToolbarItem(placement: .topBarTrailing) {   //secondaryAction
     90                         SettingsButton26(sortPriority: 0)
     91                     }
     92                 }
     93 #if OIM
     94             if controller.oimModeActive || voiceOverEnabled {
     95                 list    // hide SettingsButton (in Toolbar)
     96             } else {
     97                 list26
     98             }
     99 #else
    100             list26
    101 #endif
    102         } else {
    103             list
    104         }
    105     }
    106 
    107     var body: some View {
    108 #if PRINT_CHANGES
    109         let _ = Self._printChanges()
    110         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
    111 #endif
    112         /// In standard mode, selectedBalance just sets a "preference" which balance to pre-select for Actions.
    113         ///   However, the user can select another balance (with the picker) in each action
    114         /// In OIM mode, the user selects a balance 'here' (in OIMView) when tapping on a savings box (representing the balance)
    115 
    116         let list = balancesList()
    117             .onChange(of: controller.oimModeActive) { oimModeActive in
    118                 if !oimModeActive {
    119                     print("🚩BalancesListView.onChange(of: oimModeActive) reset selectedBalance")
    120 //                    if count > 1 {
    121                         selectedBalance = nil                               // reset
    122 //                    }
    123                  }
    124             }
    125         ZStack {
    126             if preferredColorScheme == 3, #available(iOS 17.0, *) {
    127                 list.scrollContentBackground(.hidden)
    128             } else {
    129                 list
    130             }
    131         }
    132             .overlay { if #available(iOS 16.4, *) {
    133                 if controller.oimModeActive {
    134 #if OIM
    135                     OIMbalances(stack: stack.push(),
    136                       selectedBalance: $selectedBalance,                    // set to user choice
    137                         historyTapped: $historyTapped,
    138                               oimEuro: oimEuro)
    139                         .environmentObject(NamespaceWrapper(namespace))         // keep OIMviews apart
    140 #endif // OIM
    141                 }
    142             } }
    143     }
    144 }