-
Notifications
You must be signed in to change notification settings - Fork 25
Sign up view #76
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
Open
AlanSerralde
wants to merge
3
commits into
iOSLabUNAM:main
Choose a base branch
from
AlanSerralde:SignUpView
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sign up view #76
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 characters
Binary file added
BIN
+40 KB
...eproj/project.xcworkspace/xcuserdata/diplomado.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// AuthenticationViewModel.swift | ||
// SwiftUIBasics | ||
// | ||
// Created by Diplomado on 12/01/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
final class AuthenticationViewModel: ObservableObject { | ||
// MARK: - Password input | ||
@Published var password = "" | ||
@Published var confirmPassword = "" | ||
|
||
// MARK: - Password requirements | ||
@Published var hasEightChar = false | ||
@Published var hasSpacialChar = false | ||
@Published var hasOneDigit = false | ||
@Published var hasOneUpperCaseChar = false | ||
@Published var confirmationMatch = false | ||
@Published var areAllFieldsValid = false | ||
|
||
init() { | ||
validateSignUpFields() | ||
} | ||
|
||
private func validateSignUpFields() { | ||
/// Check password has minimum 8 characters | ||
$password | ||
.map { password in | ||
password.count >= 8 | ||
} | ||
.assign(to: &$hasEightChar) | ||
/// Check password has minimum 1 special character | ||
$password | ||
.map { password in | ||
password.rangeOfCharacter(from: CharacterSet(charactersIn: "!@#$%^&*()_+-=[]{}|:\"';<>,.?/~`")) != nil | ||
} | ||
.assign(to: &$hasSpacialChar) | ||
/// Check password has minimum 1 digit | ||
$password | ||
.map { password in | ||
password.contains { $0.isNumber } | ||
} | ||
.assign(to: &$hasOneDigit) | ||
/// Check password has minimum 1 uppercase letter | ||
$password | ||
.map { password in | ||
password.contains { $0.isUppercase } | ||
} | ||
.assign(to: &$hasOneUpperCaseChar) | ||
/// Check confirmation match password | ||
Publishers.CombineLatest($password, $confirmPassword) | ||
.map { [weak self] _, _ in | ||
guard let self else { return false} | ||
return self.password == self.confirmPassword | ||
} | ||
.assign(to: &$confirmationMatch) | ||
/// Check all fields match | ||
Publishers.CombineLatest($password, $confirmPassword) | ||
.map { [weak self] _, _ in | ||
guard let self else { return false} | ||
return self.hasEightChar && self.hasSpacialChar && self.hasOneDigit && self.hasOneUpperCaseChar && self.confirmationMatch | ||
} | ||
.assign(to: &$areAllFieldsValid) | ||
} | ||
} |
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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// SignUpViewModel.swift | ||
// SwiftUIBasics | ||
// | ||
// Created by Diplomado on 12/01/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
|
||
final class SignUpViewModel: ObservableObject { | ||
|
||
// Input values from view | ||
@Published var userName = "" | ||
@Published var userEmail = "" | ||
@Published var userPassword = "" | ||
@Published var userRepeatedPassword = "" | ||
|
||
// Output subscribers | ||
@Published var formIsValid = false | ||
|
||
private var publishers = Set<AnyCancellable>() | ||
|
||
init() { | ||
isSignupFormValidPublisher | ||
.receive(on: RunLoop.main) | ||
.assign(to: \.formIsValid, on: self) | ||
.store(in: &publishers) | ||
} | ||
|
||
} | ||
|
||
private extension SignUpViewModel { | ||
|
||
var isUserNameValidPublisher: AnyPublisher<Bool, Never> { | ||
$userName | ||
.map { name in | ||
return name.count >= 3 | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
var isUserEmailValidPublisher: AnyPublisher<Bool, Never> { | ||
$userPassword | ||
.map { password in | ||
return password.count >= 8 | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
var isPasswordValidPublisher: AnyPublisher<Bool, Never> { | ||
$userPassword | ||
.map { password in | ||
return password.count >= 8 | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
var passwordMatchesPublisher: AnyPublisher<Bool, Never> { | ||
Publishers.CombineLatest($userPassword, $userRepeatedPassword) | ||
.map { password, repeated in | ||
return password == repeated | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
var isSignupFormValidPublisher: AnyPublisher<Bool, Never> { | ||
Publishers.CombineLatest4( | ||
isUserNameValidPublisher, | ||
isUserEmailValidPublisher, | ||
isPasswordValidPublisher, | ||
passwordMatchesPublisher) | ||
.map { isNameValid, isEmailValid, isPasswordValid, passwordMatches in | ||
return isNameValid && isEmailValid && isPasswordValid && passwordMatches | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
} |
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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// TestSignUp.swift | ||
// SwiftUIBasics | ||
// | ||
// Created by Diplomado on 12/01/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct UserFormTextField: View { | ||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
struct SignUpPasswordScreen: View { | ||
@StateObject private var authVM = AuthenticationViewModel() | ||
|
||
var body: some View { | ||
VStack(spacing: 0) { | ||
VStack { | ||
VStack(alignment: .leading, spacing: 10) { | ||
Text("Password") | ||
.font(.title) | ||
.bold() | ||
Text("Password must have more than 8 characters, contain some special character, one digit, one uppercase letter") | ||
.font(.caption) | ||
} | ||
Group { | ||
UserFormTextField(text: $authVM.password, type: .password) | ||
VStack(alignment: .leading) { | ||
RequirementsPickerView(type: .eightChar, toggleState: $authVM.hasEightChar) | ||
RequirementsPickerView(type: .spacialChar, toggleState: $authVM.hasSpacialChar) | ||
RequirementsPickerView(type: .oneDigit, toggleState: $authVM.hasOneDigit) | ||
RequirementsPickerView(type: .upperCaseChar, toggleState: $authVM.hasOneUpperCaseChar) | ||
} | ||
UserFormTextField(text: $authVM.confirmPassword, type: .repeatPassword) | ||
RequirementsPickerView(type: .confirmation, toggleState: $authVM.confirmationMatch) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
SignUpPasswordScreen() | ||
} |
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 characters
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 characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File not needed on PR