Last active
December 3, 2021 23:26
Revisions
-
zentrope revised this gist
Dec 3, 2021 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -74,7 +74,7 @@ struct ContentView: View { } var body: some View { VStack(spacing: 20) { Text("Username") TextField("", text: $username) .multilineTextAlignment(.center) @@ -94,6 +94,13 @@ struct ContentView: View { .border(Color.blue) ValidUsername() .font(.largeTitle) if status == .valid { Button("Continue") { print("continue pressed") } .buttonStyle(.bordered) } Spacer() } -
zentrope revised this gist
Dec 3, 2021 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,18 @@ struct ContentView: View { // } func validateUsername() { if username.count < 8 { status = .unknown return } if namesInUse.contains(username.lowercased()) { status = .invalid return } // username is good to go status = .valid } @ViewBuilder -
zentrope created this gist
Dec 3, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,141 @@ // // ContentView.swift // Test5 // //import SwiftUI // //struct ContentView: View { // var body: some View { // Text("Hello, world!") // .padding() // } //} import SwiftUI import Combine struct ContentView: View { init() { UITableView.appearance().backgroundColor = .clear } enum UserStatus { case valid, invalid, unknown } // Form Variables @FocusState private var isFocused: Bool @State private var username = "" @State private var status: UserStatus = .unknown let namesInUse = Set([ "foobar23", "blah1234", ]) // func validUsername() -> String { // if (username.count < 8) { // return "" // } // return namesInUse.contains(username.lowercased()) ? "❌" : "✅" // } func validateUsername() { status = username.count < 8 ? .unknown : namesInUse.contains(username.lowercased()) ? .valid : .invalid } @ViewBuilder func ValidUsername() -> some View { switch status { case .unknown: EmptyView() case .valid: Image(systemName: "checkmark.circle") .foregroundColor(Color.green) case .invalid: Image(systemName: "x.square") .foregroundColor(Color.red) } } var body: some View { VStack { Text("Username") TextField("", text: $username) .multilineTextAlignment(.center) .textCase(.lowercase) .autocapitalization(.none) .focused($isFocused) .keyboardType(.default) .onReceive(Just(username)) { newValue in let filtered = newValue.filter { "abcdefghijklmnopqrstuvwxyz0123456789".contains($0) } if filtered != newValue { self.username = filtered } validateUsername() } // End of onReceive .submitLabel(.done) .border(Color.blue) ValidUsername() .font(.largeTitle) Spacer() } // List { // Group { // // Section { // // HStack { // Text("Username") // ZStack(alignment: .trailing) { // if username.isEmpty { // Text("johnsmith") // .foregroundColor(Color("GrayText")) // } // TextField("", text: $username) // .multilineTextAlignment(.trailing) // .textCase(.lowercase) // .autocapitalization(.none) // .focused($isFocused) // .keyboardType(.default) // .onReceive(Just(username)) { newValue in // let filtered = newValue.filter { "abcdefghijklmnopqrstuvwxyz0123456789".contains($0) } // if filtered != newValue { // self.username = filtered // } // } // End of onReceive // // .submitLabel(.done) // ValidUsername() // //Text("\(validUsername())") // // } // End of ZStack // // } // End of HStack // // } // End Section // // } // End Group // .listRowBackground(Color("FieldBkg")) // .listRowSeparatorTint(Color("sepLine")) // .listRowSeparator(.visible) // // } // End List // .listStyle(GroupedListStyle()) // .foregroundColor(Color("WhiteText")) // .accentColor(Color("WhiteText")) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }