SelectDays.swift (4905B)
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 @AppStorage("minimalistic") var minimalistic: Bool = false 15 #if DEBUG 16 @AppStorage("developerMode") var developerMode: Bool = true 17 #else 18 @AppStorage("developerMode") var developerMode: Bool = false 19 #endif 20 21 @Binding var selected: UInt 22 let maxExpirationHours: UInt64 23 let defaultExpirationHours: UInt64 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 #if PRINT_CHANGES 43 let _ = Self._printChanges() 44 // let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear 45 #endif 46 let maxExpiration = maxExpirationHours / 24 47 let chooseExpiration = Section { // (alignment: .leading) 48 Text("Expires in:") 49 .accessibilityLabel(Text("Choose the expiration duration", comment: "a11y")) 50 .accessibilityAddTraits(.isHeader) 51 .accessibilityRemoveTraits(.isStaticText) 52 .talerFont(.title3) 53 HStack { 54 Button(action: oneDayAction) { 55 if developerMode { 56 Text(verbatim: "3 Min.") 57 } else { 58 Text("\(ONEDAY) Day", comment: "1 Day, might get plural (e.g. 2..3 Days), 4 letters max., abbreviate if longer") // TODO: Plural 59 } 60 }.buttonStyle(TalerButtonStyle(type: (selected == ONEDAY) ? .prominent : .bordered, 61 dimmed: true)) 62 .accessibilityAddTraits(selected == ONEDAY ? .isSelected : []) 63 64 Button(action: sevenDayAction) { 65 if developerMode { 66 Text(verbatim: "1 Hour") 67 } else { 68 Text("\(SEVENDAYS) Days", comment: "7 Days, always plural (3..9), 4 letters max., abbreviate if longer") 69 } 70 }.buttonStyle(TalerButtonStyle(type: (selected == SEVENDAYS) ? .prominent : .bordered, dimmed: true, 71 disabled: maxExpiration < SEVENDAYS)) 72 .accessibilityAddTraits(selected == SEVENDAYS ? .isSelected : []) 73 .disabled(maxExpiration < SEVENDAYS) 74 75 Button(action: thirtyDayAction) { 76 if developerMode { 77 Text(verbatim: "1 Day") 78 } else { 79 let thirtyDays = String(localized: "thirtyDays", defaultValue: "\(THIRTYDAYS) Days", comment: "30 Days, always plural (10..30), 4 letters max., abbreviate if longer") 80 Text(thirtyDays) 81 } 82 }.buttonStyle(TalerButtonStyle(type: (selected == THIRTYDAYS) ? .prominent : .bordered, dimmed: true, 83 disabled: maxExpiration < THIRTYDAYS)) 84 .accessibilityAddTraits(selected == THIRTYDAYS ? .isSelected : []) 85 .disabled(maxExpiration < THIRTYDAYS) 86 } // 3 buttons 87 if !minimalistic { 88 Text(outgoing ? "The payment service will send your money back if it won't get collected on time, or when you abort the operation." 89 : "This request will be cancelled if it doesn't get paid on time, or when you abort the operation.") 90 .talerFont(.body) 91 } 92 } 93 #if TALER_WALLET 94 chooseExpiration 95 #else 96 if developerMode { 97 chooseExpiration 98 } else if outgoing { 99 if !minimalistic { 100 Section { // (alignment: .leading) 101 Text(defaultExpirationHours < 49 ? "The payment service will send your money back if it won't get collected in the next \(defaultExpirationHours) hours, or when you abort the operation." 102 : "The payment service will send your money back if it won't get collected in the next \(defaultExpirationHours/24) days, or when you abort the operation.") 103 .talerFont(.body) 104 } 105 } 106 } else { 107 chooseExpiration 108 } 109 #endif 110 } 111 } 112 // MARK: - 113 #if DEBUG 114 struct SelectDays_Previews: PreviewProvider { 115 static var previews: some View { 116 @State var expireDays: UInt = 1 117 SelectDays(selected: $expireDays, maxExpirationHours: 30*24, defaultExpirationHours: 7, outgoing: false) 118 } 119 } 120 #endif