Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signup #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SwiftUIBasics/Views/Components/FormTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct FormTextField: View {
var name = ""
@Binding var value: String
var isSecure = false
var fieldType: UIKeyboardType = .default

var body: some View {
VStack {
Expand All @@ -20,6 +21,7 @@ struct FormTextField: View {
.padding(.horizontal)
} else {
TextField(name, text: $value)
.keyboardType(fieldType)
.font(.system(size: 20, weight: .semibold, design: .rounded))
.padding(.horizontal)
}
Expand Down
83 changes: 63 additions & 20 deletions SwiftUIBasics/Views/SignUpView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,89 @@ import Combine

class SignUpViewModel: ObservableObject {
// inputs
@Published var username: String = ""
@Published var email: String = ""
@Published var password: String = ""
@Published var passwordConfirm: String = ""

// outputs
@Published var isValidUsernameLength: Bool = false
@Published var isValidEmail: Bool = false

@Published var isValidPasswordLength: Bool = false
@Published var isValidPasswordLowerCase: Bool = false
@Published var isValidPasswordUpperCase: Bool = false
@Published var passwordHasSpecialSymbol: Bool = false
@Published var passwordHasNumber: Bool = false
@Published var isValidPassword: Bool = false

@Published var isValidPasswordMatch: Bool = false

@Published var isValid: Bool = false

private var cancelableSet: Set<AnyCancellable> = []

init() {
$username
$email
.receive(on: RunLoop.main)
.map { username in
return username.count >= 4
.map { email in
let emailPattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailPattern)
return emailPredicate.evaluate(with: email)
}
.assign(to: \.isValidUsernameLength, on: self)
.assign(to: \.isValidEmail, on: self)
.store(in: &cancelableSet)

$password
.receive(on: RunLoop.main)
.map { password in
return password.count >= 8
let symbolRegex = Regex(/\W+/)
return password.contains(symbolRegex)
}
.assign(to: \.isValidPasswordLength, on: self)
.assign(to: \.passwordHasSpecialSymbol, on: self)
.store(in: &cancelableSet)

$password
.receive(on: RunLoop.main)
.map { password in
let pattern = "[A-Z]"
if let _ = password.range(of: pattern, options: .regularExpression) {
return true
} else {
return false
}
let numberRegex = Regex(/[0-9]/)
return password.contains(numberRegex)
}
.assign(to: \.passwordHasNumber, on: self)
.store(in: &cancelableSet)

$password
.receive(on: RunLoop.main)
.map { password in
let lowercaseRegex = Regex(/[a-z]/)
return password.contains(lowercaseRegex)
}
.assign(to: \.isValidPasswordLowerCase, on: self)
.store(in: &cancelableSet)

$password
.receive(on: RunLoop.main)
.map { password in
let uppercaseRegex = Regex(/[A-Z]/)
return password.contains(uppercaseRegex)
}
.assign(to: \.isValidPasswordUpperCase, on: self)
.store(in: &cancelableSet)

$password
.receive(on: RunLoop.main)
.map { password in
return password.count >= 8
}
.assign(to: \.isValidPasswordLength, on: self)
.store(in: &cancelableSet)

Publishers.CombineLatest4($isValidPasswordLength, $isValidPasswordUpperCase, $isValidPasswordLowerCase, $passwordHasSpecialSymbol, $passwordHasNumber)
.receive(on: RunLoop.main)
.map { (length, uppercase, lowercase, symbol, number) in
return length && uppercase && lowercase && symbol && number
}
.assign(to: \.isValidPassword, on: self)
.store(in: &cancelableSet)

Publishers.CombineLatest($password, $passwordConfirm)
.receive(on: RunLoop.main)
.map { (password, passwordConfirm) in
Expand All @@ -61,9 +101,9 @@ class SignUpViewModel: ObservableObject {
.assign(to: \.isValidPasswordMatch, on: self)
.store(in: &cancelableSet)

Publishers.CombineLatest4($isValidUsernameLength, $isValidPasswordLength, $isValidPasswordUpperCase, $isValidPasswordMatch)
.map { (a, b, c, d) in
return a && b && c && d
Publishers.CombineLatest4($isValidEmail, $isValidPassword, $isValidPasswordMatch)
.map { (email, password, passwordMatch) in
return email && password && passwordMatch
}
.assign(to: \.isValid, on: self)
.store(in: &cancelableSet)
Expand All @@ -80,13 +120,16 @@ struct SignUpView: View {
.bold()
.foregroundStyle(.maryBlue)
.padding(.bottom, 30)
FormTextField(name: "Username", value: $vm.username)
RequirementText(text: "A minimum of 4 characters", isValid: vm.isValidUsernameLength)
FormTextField(name: "Email", value: $vm.email, fieldType: .emailAddress)
RequirementText(text: "A valid email format", isValid: vm.isValidEmail)
.padding()
FormTextField(name: "Password", value: $vm.password, isSecure: true)
VStack {
RequirementText(text: "A minimum of 8 characters", isValid: vm.isValidPasswordLength)
RequirementText(text: "One uppercase letter", isValid: vm.isValidPasswordUpperCase)
RequirementText(text: "At least one uppercase letter", isValid: vm.isValidPasswordUpperCase)
RequirementText(text: "At lest one lowercase letter", isValid: vm.isValidPasswordLowerCase)
RequirementText(text: "At least one symbol", isValid: vm.passwordHasSpecialSymbol)
RequirementText(text: "At least one digit", isValid: vm.passwordHasNumber)
}
.padding()
FormTextField(name: "Confirm Password", value: $vm.passwordConfirm, isSecure: true)
Expand Down