taler-ios

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

SelectDays.swift (4029B)


      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 import SymLog
     11 
     12 struct SelectDays: View {
     13     private let symLog = SymLogV(0)
     14     @Environment(\.isEnabled) private var isEnabled: Bool
     15     @AppStorage("minimalistic") var minimalistic: Bool = false
     16 #if DEBUG
     17     @AppStorage("developerMode") var developerMode: Bool = true
     18 #else
     19     @AppStorage("developerMode") var developerMode: Bool = false
     20 #endif
     21 
     22     @Binding var selected: UInt
     23     let maxExpiration: UInt
     24     let outgoing: Bool
     25 
     26     func oneDayAction() -> Void {
     27         selected = ONEDAY
     28         symLog.log(selected)
     29     }
     30 
     31     func sevenDayAction() -> Void {
     32         selected = SEVENDAYS
     33         symLog.log(selected)
     34     }
     35 
     36     func thirtyDayAction() -> Void {
     37         selected = THIRTYDAYS
     38         symLog.log(selected)
     39     }
     40 
     41     var body: some View {
     42         Section {   // (alignment: .leading)
     43             Text("Expires in:")
     44                 .accessibilityLabel(Text("Choose the expiration duration", comment: "a11y"))
     45                 .accessibilityAddTraits(.isHeader)
     46                 .accessibilityRemoveTraits(.isStaticText)
     47                 .talerFont(.title3)
     48             HStack {
     49                 Button(action: oneDayAction) {
     50                     if developerMode {
     51                         Text(verbatim: "3 Min.")
     52                     } else {
     53                         Text("\(ONEDAY) Day", comment: "1 Day, might get plural (e.g. 2..3 Days), 4 letters max., abbreviate if longer")     // TODO: Plural
     54                     }
     55                 }.buttonStyle(TalerButtonStyle(type: (selected == ONEDAY) ? .prominent : .bordered,
     56                                              dimmed: true, disabled: !isEnabled))
     57                     .accessibilityAddTraits(selected == ONEDAY ? .isSelected : [])
     58                     .disabled(!isEnabled)
     59 
     60                 Button(action: sevenDayAction) {
     61                     if developerMode {
     62                         Text(verbatim: "1 Hour")
     63                     } else {
     64                         Text("\(SEVENDAYS) Days", comment: "7 Days, always plural (3..9), 4 letters max., abbreviate if longer")
     65                     }
     66                 }.buttonStyle(TalerButtonStyle(type: (selected == SEVENDAYS) ? .prominent : .bordered, dimmed: true,
     67                                               disabled: !isEnabled || maxExpiration < SEVENDAYS))
     68                     .accessibilityAddTraits(selected == SEVENDAYS ? .isSelected : [])
     69                     .disabled(!isEnabled || maxExpiration < SEVENDAYS)
     70 
     71                 Button(action: thirtyDayAction) {
     72                     if developerMode {
     73                         Text(verbatim: "1 Day")
     74                     } else {
     75                         let thirtyDays = String(localized: "thirtyDays", defaultValue: "\(THIRTYDAYS) Days", comment: "30 Days, always plural (10..30), 4 letters max., abbreviate if longer")
     76                         Text(thirtyDays)
     77                     }
     78                 }.buttonStyle(TalerButtonStyle(type: (selected == THIRTYDAYS) ? .prominent : .bordered, dimmed: true,
     79                                               disabled: !isEnabled || maxExpiration < THIRTYDAYS))
     80                     .accessibilityAddTraits(selected == THIRTYDAYS ? .isSelected : [])
     81                     .disabled(!isEnabled || maxExpiration < THIRTYDAYS)
     82             } // 3 buttons
     83             if !minimalistic {
     84                 Text(outgoing ? "The payment service will send your money back if it won't get collected on time, or when you abort the operation."
     85                               : "This request will be cancelled if it doesn't get paid on time, or when you abort the operation.")
     86                 .talerFont(.body)
     87             }
     88         }
     89     }
     90 }
     91 // MARK: -
     92 #if DEBUG
     93 struct SelectDays_Previews: PreviewProvider {
     94     static var previews: some View {
     95         @State var expireDays: UInt = 1
     96         SelectDays(selected: $expireDays, maxExpiration: 20, outgoing: false)
     97     }
     98 }
     99 #endif