taler-ios

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

AgePicker.swift (1826B)


      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 
     10 struct AgePicker: View {
     11     @Binding var ageMenuList: [Int]
     12     @Binding var selectedAge: Int
     13 
     14     func setAges(ages: [Int]?) {
     15         if let ages {
     16             var zero: [Int] = []
     17             if ages.count > 0 {        // need at least 1 value from exchange which is not 0
     18                 if ages[0] != 0 {               // ensure that the first age is "0"
     19                     zero.insert(0, at: 0)       // if not, insert "0" at position 0
     20                 }
     21                 zero += ages
     22                 if selectedAge >= zero.count {  // check for out of bounds
     23                     selectedAge = 0
     24                 }
     25             } else {
     26                 selectedAge = 0                 // first ensure that selected is not out of bounds
     27             }
     28             ageMenuList = zero                  // set State (will update view)
     29         }
     30     }
     31 
     32     var body: some View {
     33         if ageMenuList.count > 1 {
     34             VStack {
     35                 Text("If this wallet belongs to a child or teenager, the generated digital cash should be age-restricted:")
     36                     .frame(maxWidth: .infinity, alignment: .leading)
     37                     .multilineTextAlignment(.leading)
     38                     .talerFont(.footnote)
     39                     .padding(.top)
     40                 Picker("Select age", selection: $selectedAge) {
     41                     ForEach($ageMenuList, id: \.self) { item in
     42                         let index = item.wrappedValue
     43                         Text((index == 0) ? "unrestricted"
     44                              : "\(index) years", comment: "Age Picker").tag(index)
     45                     }
     46                 }
     47                 .talerFont(.body)
     48             }
     49         }
     50     }
     51 }
     52