taler-ios

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

NavLink.swift (1697B)


      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 
      9 import SwiftUI
     10 
     11 /// invisible NavigationLink triggered by a Bool or Int?
     12 /// call either like this
     13 ///     .background( NavLink($buttonSelected) { destination } )
     14 /// or
     15 ///     let actions = Group {
     16 ///         NavLink(1, $actionSelected) { dest1 }
     17 ///         NavLink(2, $actionSelected) { dest2 }
     18 ///     }
     19 /// and then
     20 ///    .background(actions)
     21 
     22 
     23 struct NavLink <Content : View> : View {
     24     let tag: Int?
     25     @Binding var selection: Int?
     26     @Binding var isActive: Bool
     27     let content: Content
     28 
     29     init(_ tag: Int,
     30          _ selection: Binding<Int?>,
     31          _ isActive: Binding<Bool> = .constant(false),
     32          @ViewBuilder contentBuilder: () -> Content
     33     ) {
     34         self.tag = tag
     35         self._selection = selection
     36         self.content = contentBuilder()
     37         self._isActive = isActive
     38     }
     39 
     40     init(_ isActive: Binding<Bool>,
     41          _ selection: Binding<Int?> = .constant(nil),
     42          @ViewBuilder contentBuilder: () -> Content
     43     ) {
     44         self.tag = nil
     45         self._selection = selection
     46         self.content = contentBuilder()
     47         self._isActive = isActive
     48     }
     49 
     50     var body: some View {
     51         if let tag {        // actions: $tabBarModel.actionSelected will hide the tabBar
     52             NavigationLink(destination: content, tag: tag, selection: $selection)
     53                 { EmptyView() }.frame(width: 0).opacity(0).hidden()
     54         } else {            // shortcuts, AddButton
     55             NavigationLink(destination: content, isActive: $isActive)
     56             { EmptyView() }.frame(width: 0).opacity(0).hidden()
     57         }
     58     }
     59 }