taler-ios

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

TabBarView.swift (7428B)


      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 
     11 struct ActionItem: View {
     12     private let symLog = SymLogV(0)
     13     let stack: CallStack
     14     let tab: TalerTab
     15     let onTap: () -> Void
     16     let onDrag: (CallStack) -> Void
     17 
     18     @EnvironmentObject private var controller: Controller
     19     @AppStorage("tapped") var tapped: Int = 0
     20     @AppStorage("dragged") var dragged: Int = 0
     21 
     22     @State private var offset = CGSize.zero
     23     @State private var didDrag = false
     24 
     25     private func hopAction(_ newValue: Int) {
     26         if tapped >= TAPPED && dragged < DRAGGED {
     27             withAnimation(Animation.easeOut(duration: DRAGDURATION).delay(DRAGDELAY)) {
     28                 offset.height = -50
     29             }
     30             withAnimation(Animation.easeOut(duration: DRAGSPEED).delay(DRAGDELAY + DRAGDURATION + DRAGSPEED)) {
     31                 offset.height = 10
     32             }
     33             withAnimation(Animation.easeOut(duration: DRAGSPEED/2).delay(DRAGDELAY + DRAGDURATION + 2.5*DRAGSPEED)) {
     34                 offset.height = 0
     35             }
     36         }
     37     }
     38 
     39     private func dragGesture() -> some Gesture {
     40         DragGesture(minimumDistance: 6)
     41             .onChanged { gesture in
     42                 var trans = gesture.translation
     43                 trans.width = .zero
     44                 if trans.height < -20 {
     45                     symLog.log(".onChanged: didDrag \(trans.height)")
     46                     withAnimation {
     47                         offset = .zero
     48                     }
     49                     didDrag = true
     50                     onDrag(stack.push())    // switch to camera
     51                     if tapped >= TAPPED {
     52                         dragged += 1
     53                     }
     54                 } else if trans.height < 0 {    // only drag up
     55                     symLog.log(".onChanged: \(trans.height)")
     56                     withAnimation {
     57                         offset = trans
     58                     }
     59                 }
     60             }
     61             .onEnded { gesture in
     62                 let trans = gesture.translation
     63                 if didDrag {
     64                     symLog.log(".onEnded: didDrag \(trans.height)")
     65                     didDrag = false
     66                 } else {
     67                     symLog.log(".onActionTab: \(trans.height)")
     68                     onTap()
     69                 }
     70                 withAnimation {
     71                     offset = .zero
     72                 }
     73             }
     74     }
     75 
     76     var body: some View {
     77         let vStack = VStack(spacing: 0) {
     78     // show floating actions
     79             let withText = if #available(iOS 26.0, *) { false } else { tapped < TAPPED }
     80             let width = withText ? 48 : 72.0
     81             let height = withText ? 36 : 57.6
     82             let image = tab.image
     83                 .resizable()
     84                 .scaledToFill()
     85                 .frame(width: width, height: height)
     86                 .clipped() // Crop the image to the frame size
     87 //                .opacity(1 - Double(abs(offset.height / 35)))
     88                 .gesture(dragGesture())
     89                 .onLongPressGesture(minimumDuration: 0.4) {
     90                     onDrag(stack.push())
     91                 }
     92                 .onTapGesture {
     93                     onTap()
     94                     tapped += 1
     95                 }
     96             if #available(iOS 26.0, *) {
     97                 image
     98             } else {
     99                 image
    100                     .padding(.bottom, 4)
    101                     .offset(offset)
    102             }
    103             if withText {
    104                 Text(tab.title)
    105                     .lineLimit(1)
    106                     .talerFont(.body)
    107             }
    108         }   .id(tab)
    109 //            .foregroundColor(isActive ? WalletColors().primaryAccent : .secondary)
    110             .padding(.vertical, 4)
    111             .accessibilityElement(children: .combine)
    112             .accessibility(label: Text(tab.title))
    113             .accessibility(addTraits: [.isButton])
    114             .accessibility(removeTraits: [.isImage])
    115             .onChange(of: controller.userAction) { newValue in
    116                 hopAction(newValue)         // make Action button jump to indicate it can be dragged
    117             }
    118 
    119         if #available(iOS 26.0, *) {
    120             vStack
    121                 .padding(.horizontal, 6)
    122                 .glassEffect(.clear.interactive())
    123                 .offset(offset)
    124         } else {
    125             vStack
    126                 .frame(maxWidth: .infinity)
    127         }
    128     }
    129 }
    130 // MARK: -
    131 struct TabBarView: View {
    132     private let symLog = SymLogV(0)
    133     let stack: CallStack
    134     @Binding var selection: TalerTab
    135     @Binding var hidden: Int
    136     let onActionTab: () -> Void
    137     let onActionDrag: (CallStack) -> Void
    138 
    139     @Environment(\.keyboardShowing) var keyboardShowing
    140     @EnvironmentObject private var controller: Controller
    141 
    142     @AppStorage("minimalistic") var minimalistic: Bool = false
    143     @AppStorage("tapped") var tapped: Int = 0
    144 
    145     @Namespace private var namespace
    146 
    147     private func tabBarItem(for tab: TalerTab) -> some View {
    148         
    149         let isActive = selection == tab
    150         let vStack = VStack(spacing: 0) {
    151             let withText = tapped < TAPPED || !minimalistic
    152             let size = withText ? 24.0 : 36.0
    153             tab.image
    154                 .resizable()
    155                 .renderingMode(.template)
    156                 .tint(.black)
    157                 .aspectRatio(contentMode: .fit)
    158                 .frame(width: size, height: size)
    159 
    160             if withText {
    161                 if isActive {
    162                     Text(tab.title)
    163                         .bold()
    164                         .lineLimit(1)
    165                         .talerFont(.picker)
    166                 } else {
    167                     Text(tab.title)
    168                         .lineLimit(1)
    169                         .talerFont(.body)
    170                 }
    171             }
    172         }   .id(tab)
    173             .foregroundColor(isActive ? WalletColors().primaryAccent : .secondary)
    174             .padding(.vertical, 8)
    175             .accessibilityElement(children: .combine)
    176             .accessibility(label: Text(tab.title))
    177             .accessibility(addTraits: [.isButton])
    178             .accessibility(removeTraits: [.isImage])
    179         return vStack
    180             .frame(maxWidth: .infinity)
    181             .contentShape(Rectangle())
    182     }
    183 
    184     var body: some View {
    185         Group {
    186             if keyboardShowing || hidden > 0 {
    187                 EmptyView()
    188             } else {
    189                 let actionTab = ActionItem(stack: stack.push(),
    190                                              tab: TalerTab.actions,
    191                                            onTap: onActionTab,
    192                                           onDrag: onActionDrag)
    193                 let balanceTab = tabBarItem(for: TalerTab.balances)
    194                     .onTapGesture {
    195                         selection = .balances
    196                         controller.userAction += 1
    197                     }
    198                 let settingsTab = tabBarItem(for: TalerTab.settings)
    199                     .onTapGesture {
    200                         selection = .settings
    201                         controller.userAction += 1
    202                     }
    203                 HStack(alignment: .bottom) {
    204                     balanceTab
    205                     actionTab
    206                     settingsTab
    207                 }
    208                     .background(WalletColors().backgroundColor.ignoresSafeArea(edges: .bottom))
    209             }
    210         }
    211     }
    212 }
    213 // MARK: -
    214 //#Preview {
    215 //    TabBarView()
    216 //}