taler-ios

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

BalancesListView.swift (4877B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 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     @Binding var qrButtonTapped: Bool
     21 
     22     @EnvironmentObject private var model: WalletModel
     23     @EnvironmentObject private var controller: Controller
     24     @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
     25     @AppStorage("oimEuro") var oimEuro: Bool = false
     26 
     27     @State private var amountToTransfer = Amount.zero(currency: EMPTYSTRING)    // Update currency when used
     28     @State private var summary = EMPTYSTRING
     29 
     30     @State private var historyTapped: Int? = nil
     31     @Namespace var namespace
     32 
     33     private static func className() -> String {"\(self)"}
     34 
     35     func refresh() async {
     36         controller.hapticNotification(.success)
     37         symLog.log("refreshing balances")
     38         await controller.loadBalances(stack.push("refreshing balances"), model)
     39     }
     40 
     41     var body: some View {
     42 #if PRINT_CHANGES
     43         let _ = Self._printChanges()
     44         let _ = symLog.vlog()       // just to get the # to compare it with .onAppear & onDisappear
     45 #endif
     46 //        Group {
     47             let count = controller.balances.count
     48             if controller.balances.isEmpty {
     49                 WalletEmptyView(stack: stack.push("isEmpty"))
     50                     .navigationTitle("Empty Wallet")
     51                     .refreshable {
     52                         await refresh()
     53                     }
     54             } else {
     55                 /// In standard mode, selectedBalance just sets a "preference" which balance to pre-select for Actions.
     56                 ///   However, the user can select another balance (with the picker) in each action
     57                 /// In OIM mode, the user selects a balance 'here' (in OIMView) when tapping on a savings box (representing the balance)
     58                 Group {
     59                     List(Array(controller.balances.enumerated()), id: \.element) { index, balance in
     60                         BalancesSectionView(stack: stack.push("\(balance.scopeInfo.currency)"),
     61                                           balance: balance,                     // this is the currency to be used
     62                                   selectedBalance: $selectedBalance,            // set in TransactionsListView
     63                                      balanceIndex: index,
     64                                      sectionCount: count,
     65                                  amountToTransfer: $amountToTransfer,           // does still have the wrong currency
     66                                           summary: $summary,
     67                                     historyTapped: $historyTapped,
     68                                reloadTransactions: $reloadTransactions)
     69                     }
     70                     .onAppear() {
     71                         DebugViewC.shared.setViewID(VIEW_BALANCES, stack: stack.push("onAppear"))
     72                         if !controller.oimModeActive {
     73                             print("🚩BalancesListView.onAppear() reset selectedBalance")
     74 //                            if controller.balances.count > 1 {
     75                                 selectedBalance = nil                           // reset
     76 //                            }
     77                         }
     78                     }
     79                     .listStyle(myListStyle.style).anyView
     80                     .refreshable {
     81                         await refresh()
     82                     }
     83                 }.navigationTitle(title)
     84                 .onChange(of: controller.oimModeActive) { oimModeActive in
     85                     if !oimModeActive {
     86                         print("🚩BalancesListView.onChange(of: oimModeActive) reset selectedBalance")
     87 //                        if controller.balances.count > 1 {
     88                             selectedBalance = nil                               // reset
     89 //                        }
     90                     }
     91                 }
     92 #if OIM
     93                 .overlay { if #available(iOS 16.4, *) {
     94                     if controller.oimModeActive {
     95                         OIMbalances(stack: stack.push(),
     96                           selectedBalance: $selectedBalance,                    // set to user choice
     97                            qrButtonTapped: $qrButtonTapped,
     98                             historyTapped: $historyTapped,
     99                                   oimEuro: oimEuro)
    100                         .environmentObject(NamespaceWrapper(namespace))         // keep OIMviews apart
    101                     }
    102                 } }
    103 #endif
    104             } // not empty
    105 //        }
    106     }
    107 }