taler-ios

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

TextFieldAlert.swift (2646B)


      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 TextFieldAlert: ViewModifier {
     11     @Binding var isPresented: Bool
     12     let title: String
     13     let doneText: String
     14     @Binding var text: String
     15     let placeholder: String
     16     let action: (String) -> Void
     17     func body(content: Content) -> some View {
     18         ZStack(alignment: .center) {
     19             content
     20                 .disabled(isPresented)
     21                 .accessibilityElement(children: isPresented ? .ignore : .contain)
     22             if isPresented {
     23                 VStack {
     24                     Text(title)
     25                         .talerFont(.headline)
     26                         .accessibilityAddTraits(.isHeader)
     27                         .accessibilityRemoveTraits(.isStaticText)
     28                         .padding()
     29                     TextField(placeholder, text: $text).textFieldStyle(.roundedBorder).padding()
     30                     Divider()
     31                     HStack {
     32                         Spacer()
     33                         Button(role: .cancel) {
     34                             withAnimation { isPresented.toggle() }
     35                         } label: {
     36                             Text("Cancel")
     37                         }
     38                         Spacer()
     39                         Divider()
     40                         Spacer()
     41                         Button(doneText) {
     42                             action(text)
     43                             withAnimation { isPresented.toggle() }
     44                         }
     45 //                        .talerFont(.talerBody)     TODO: check
     46                         Spacer()
     47                     }
     48                 }
     49                     .accessibility(addTraits: .isModal)
     50                     .background(.background)
     51                     .frame(width: 300, height: 200)
     52                     .cornerRadius(20)
     53                     .overlay {
     54                         RoundedRectangle(cornerRadius: 20)
     55                             .stroke(.quaternary, lineWidth: 1)
     56                     }
     57             }
     58         }
     59     }
     60 }
     61 
     62 extension View {
     63     public func textFieldAlert(isPresented: Binding<Bool>,
     64                                      title: String,
     65                                   doneText: String,
     66                                       text: Binding<String>,
     67                                placeholder: String = EMPTYSTRING,
     68                                     action: @escaping (String) -> Void
     69     ) -> some View {
     70         self.modifier(TextFieldAlert(isPresented: isPresented, title: title, doneText: doneText, text: text, placeholder: placeholder, action: action))
     71     }
     72 }