Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active December 3, 2021 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/68450a9cbc1c498313011d47ccb0d847 to your computer and use it in GitHub Desktop.
Save zentrope/68450a9cbc1c498313011d47ccb0d847 to your computer and use it in GitHub Desktop.
Use enum to indicate state in SwiftUI
//
// 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() {
if username.count < 8 {
status = .unknown
return
}
if namesInUse.contains(username.lowercased()) {
status = .invalid
return
}
// username is good to go
status = .valid
}
@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(spacing: 20) {
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)
if status == .valid {
Button("Continue") {
print("continue pressed")
}
.buttonStyle(.bordered)
}
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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment