taler-ios

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

BalancesListView.swift (5745B)


      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     @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                 let talerLogo = HStack(spacing: 2) {
     50                     Image(TALER_LOGO_FULL)
     51 //                        .border(Color.gray, width: 1)
     52 //                    Text("TALER Wallet")
     53                     Text("Wallet")
     54 //                        .font(.logo(27, weight: .medium))
     55                         .font(.logo("Atkinson Hyperlegible Next", size: 27, weight: .medium))
     56                         .kerning(1.0)
     57                 }
     58                     .accessibilityElement(children: .combine)
     59                     .accessibilityAddTraits(.isHeader)
     60                     .accessibilityLabel(Text("Taler Wallet", comment: "a11y"))
     61 
     62                 WalletEmptyView(stack: stack.push("isEmpty"))
     63 //                    .navigationTitle("Taler Wallet")
     64                     .toolbar {
     65                         ToolbarItem(placement: .principal) {
     66                             talerLogo
     67                                 .padding(.top, 10)
     68                         }
     69                     }
     70                     .refreshable {
     71                         await refresh()
     72                     }
     73             } else {
     74                 /// In standard mode, selectedBalance just sets a "preference" which balance to pre-select for Actions.
     75                 ///   However, the user can select another balance (with the picker) in each action
     76                 /// In OIM mode, the user selects a balance 'here' (in OIMView) when tapping on a savings box (representing the balance)
     77                 Group {
     78                     List(Array(controller.balances.enumerated()), id: \.element) { index, balance in
     79                         BalancesSectionView(stack: stack.push("\(balance.scopeInfo.currency)"),
     80                                           balance: balance,                     // this is the currency to be used
     81                                   selectedBalance: $selectedBalance,            // set in TransactionsListView
     82                                      balanceIndex: index,
     83                                      sectionCount: count,
     84                                  amountToTransfer: $amountToTransfer,           // does still have the wrong currency
     85                                           summary: $summary,
     86                                     historyTapped: $historyTapped,
     87                                reloadTransactions: $reloadTransactions)
     88                     }
     89                     .onAppear() {
     90                         DebugViewC.shared.setViewID(VIEW_BALANCES, stack: stack.push("onAppear"))
     91                         if !controller.oimModeActive {
     92                             print("🚩BalancesListView.onAppear() reset selectedBalance")
     93 //                            if controller.balances.count > 1 {
     94                                 selectedBalance = nil                           // reset
     95 //                            }
     96                         }
     97                     }
     98                     .listStyle(myListStyle.style).anyView
     99                     .refreshable {
    100                         await refresh()
    101                     }
    102                 }.navigationTitle(title)
    103                 .onChange(of: controller.oimModeActive) { oimModeActive in
    104                     if !oimModeActive {
    105                         print("🚩BalancesListView.onChange(of: oimModeActive) reset selectedBalance")
    106 //                        if controller.balances.count > 1 {
    107                             selectedBalance = nil                               // reset
    108 //                        }
    109                     }
    110                 }
    111 #if OIM
    112                 .overlay { if #available(iOS 16.4, *) {
    113                     if controller.oimModeActive {
    114                         OIMbalances(stack: stack.push(),
    115                           selectedBalance: $selectedBalance,                    // set to user choice
    116                            qrButtonTapped: $qrButtonTapped,
    117                             historyTapped: $historyTapped,
    118                                   oimEuro: oimEuro)
    119                         .environmentObject(NamespaceWrapper(namespace))         // keep OIMviews apart
    120                     }
    121                 } }
    122 #endif
    123             } // not empty
    124 //        }
    125     }
    126 }