Skip to content

Instantly share code, notes, and snippets.

@zentrope
Last active December 3, 2021 23:26

Revisions

  1. zentrope revised this gist Dec 3, 2021. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Login.swift
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@ struct ContentView: View {
    }

    var body: some View {
    VStack {
    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()

    }
  2. zentrope revised this gist Dec 3, 2021. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion Login.swift
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,18 @@ struct ContentView: View {
    // }

    func validateUsername() {
    status = username.count < 8 ? .unknown : namesInUse.contains(username.lowercased()) ? .valid : .invalid
    if username.count < 8 {
    status = .unknown
    return
    }

    if namesInUse.contains(username.lowercased()) {
    status = .invalid
    return
    }

    // username is good to go
    status = .valid
    }

    @ViewBuilder
  3. zentrope created this gist Dec 3, 2021.
    141 changes: 141 additions & 0 deletions Login.swift
    Original 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()
    }
    }