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