taler-ios

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

OIMcurrencyDrawer.swift (4517B)


      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 
     11 // renders all banknotes and coins in 1 horizontal scrollview
     12 struct OIMcurrencyDrawer: View {
     13     let stack: CallStack
     14     let cash: OIMcash
     15     @Binding var availableVal: UInt64
     16     @Binding var tappedVal: UInt64
     17     let scrollPosition: UInt64
     18     let canEdit: Bool
     19 
     20     @EnvironmentObject private var wrapper: NamespaceWrapper
     21 
     22     var body: some View {
     23         var debugTick = 0
     24 //        let _ = Self._printChanges()
     25 
     26         let currency = cash.currency
     27         let taskID = currency.noteBase + String(scrollPosition)
     28         ScrollViewReader { proxy in
     29           ScrollView(.horizontal) {
     30             HStack(alignment: .bottom, spacing: 10) {
     31                 ForEach(currency.bankNotes, id: \.self) { value in
     32                     let sourceID = -Int(value)
     33                     let fund = OIMfund(id: sourceID,
     34                                     value: value,
     35 //                          currencyIndex: cash.currencyIndex
     36                               currencyStr: currency.currencyStr)
     37                     OIMcurrencyButton(stack: stack.push(),
     38                                        fund: fund,
     39                                    currency: currency,
     40                                availableVal: availableVal,
     41                                     canEdit: canEdit,
     42                                    isDrawer: true,
     43                                         pct: 0.8,                       // hover a bit above the desk
     44                                      action: { tappedVal = value }
     45                     )
     46                     .id(currency.noteBase + String(sourceID))
     47                     .matchedGeometryEffect(id: String(sourceID), in: wrapper.namespace, isSource: true)
     48                 }
     49                 ForEach(currency.bankCoins, id: \.self) { value in
     50                     let sourceID = -Int(value)
     51                     let fund = OIMfund(id: sourceID,
     52                                     value: value,
     53 //                            currencyIndex: cash.currencyIndex
     54                                 currencyStr: currency.currencyStr)
     55                     OIMcurrencyButton(stack: stack.push(),
     56                                        fund: fund,
     57                                    currency: currency,
     58                                availableVal: availableVal,
     59                                     canEdit: canEdit,
     60                                    isDrawer: true,
     61                                         pct: 0.8,
     62                                      action: { tappedVal = value }
     63                     )
     64                     .id(currency.noteBase + String(sourceID))
     65                     .matchedGeometryEffect(id: String(sourceID), in: wrapper.namespace, isSource: true)
     66                 }
     67             }.padding(.trailing, UIScreen.hasNotch ? UIScreen.horzInsets : 0)   // ensure scrolling over the FaceID notch
     68         }
     69           .onAppear {
     70               if scrollPosition > 0 {
     71 //                  print(">>> OIMcurrencyScroller.onAppear scrolling to", scrollPosition, canEdit)
     72                   proxy.scrollTo(currency.noteBase + String(-Int(scrollPosition)), anchor: .leading)
     73 //                  DispatchQueue.main.async {
     74 //                      print(">>> OIMcurrencyScroller.onAppear scrolling AGAIN to", scrollPosition, canEdit)
     75 //                      proxy.scrollTo(-Int(scrollPosition), anchor: .leading)
     76 //                  }
     77                   debugTick += 1
     78               } else {      // should only happen if available is zero
     79                   print("❗️❗️ OIMcurrencyScroller.onAppear scrolling to", scrollPosition, canEdit) // , stack.peek()?.file)
     80               }
     81           }
     82           .task(id: taskID) {
     83               if !canEdit && scrollPosition > 0 {
     84                   debugTick += 1
     85 //                  print(">>❗️ OIMcurrencyScroller.task", cash.currency.noteBase, availableVal, scrollPosition)
     86                   proxy.scrollTo(currency.noteBase + String(-Int(scrollPosition)), anchor: .leading)
     87                   debugTick += 1
     88               }
     89           }
     90         .viewExtractor { view in
     91             if let scrollView = view as? UIScrollView {
     92                 if #available(iOS 17.4, *) {
     93                     scrollView.bouncesVertically = false
     94                 } else {    // Fallback on earlier versions
     95                     scrollView.bounces = false
     96                 }
     97             }
     98         }
     99       }
    100     }
    101 }