Skip to content

Commit

Permalink
refactor(auth, core, lib): rethink design of front error handling ♻️ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil authored Jun 29, 2022
1 parent 9aba6ef commit 4a4dd34
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "production"
buildConfiguration = "development"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES"
Expand Down
7 changes: 6 additions & 1 deletion waosSwift/lib/helpers/Extensions/UITextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import FontAwesome
*/

extension UITextField {
// MARK: Constants

struct Metric {
static let error = UIColor(named: config["theme"]["themes"]["waos"]["error"].string ?? "")
}

/**
* @desc set left font awesome icon on
* @param {String} icon code,
Expand All @@ -28,5 +34,4 @@ extension UITextField {
leftView = outerView
leftViewMode = .always
}

}
39 changes: 16 additions & 23 deletions waosSwift/modules/auth/controllers/AuthForgotController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ final class AuthForgotController: CoreController, View, Stepper {

let inputEmail = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-envelope")
$0.placeholder = L10n.authMail + "..."
$0.autocapitalizationType = .none
$0.textContentType = .username
$0.icon="fa-envelope"
//$0.text = "[email protected]"
}
let buttonReset = CoreUIButton().then {
Expand Down Expand Up @@ -89,6 +89,13 @@ final class AuthForgotController: CoreController, View, Stepper {

override func setupConstraints() {
self.width = self.view.frame.width
// errors
labelErrors.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
make.centerX.equalTo(self.view)
make.bottom.equalTo(self.inputEmail.snp.top).offset(-10)
}
// inputs
labelSuccess.snp.makeConstraints { (make) -> Void in
make.left.equalTo(25)
Expand Down Expand Up @@ -118,15 +125,6 @@ final class AuthForgotController: CoreController, View, Stepper {
buttonReset.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(-70).keyboard(true, in: self.view)
}
labelErrors.snp.makeConstraints { (make) -> Void in
make.left.equalTo(25)
make.right.equalTo(-25)
make.centerX.equalTo(self.view)
make.centerY.equalTo(self.view).offset(120).keyboard(false, in: self.view)
}
labelErrors.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(20).keyboard(true, in: self.view)
}
// background
self.backgroundImage.snp.makeConstraints { make in
make.top.equalTo(self.view)
Expand Down Expand Up @@ -233,24 +231,19 @@ private extension AuthForgotController {
})
.disposed(by: self.disposeBag)
// validation errors
reactor.state
.map { $0.errors }
.filter { $0.count > 0 }
.distinctUntilChanged { $0.count == $1.count }
.subscribe(onNext: { errors in
self.error.configureContent(title: "Schema", body: errors.map { "\($0.description)." }.joined(separator: "\n"))
self.error.button?.isHidden = true
SwiftMessages.show(config: self.popupConfig, view: self.error)
})
.disposed(by: self.disposeBag)
reactor.state
.map { $0.errors.count }
.distinctUntilChanged()
.subscribe(onNext: { _ in
.subscribe(onNext: { count in
if(count > 0) {
self.labelErrors.text = reactor.currentState.errors.first?.description
} else {
self.labelErrors.text = ""
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.email)" }) != nil {
self.inputEmail.layer.borderWidth = 1.0
self.inputEmail.error()
} else {
self.inputEmail.layer.borderWidth = 0
self.inputEmail.valid()
}
})
.disposed(by: self.disposeBag)
Expand Down
47 changes: 19 additions & 28 deletions waosSwift/modules/auth/controllers/AuthSigninController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ final class AuthSignInController: CoreController, View, Stepper {

let inputEmail = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-envelope")
$0.placeholder = L10n.authMail + "..."
$0.autocapitalizationType = .none
$0.textContentType = .username
$0.icon="fa-envelope"
//$0.text = "[email protected]"
}
let inputPassword = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-key")
$0.placeholder = L10n.authPassword + "..."
$0.autocapitalizationType = .none
$0.returnKeyType = .done
$0.isSecureTextEntry = true
$0.textContentType = .password
$0.icon="fa-key"
//$0.text = "TestWaos@2019"
}
let buttonSignin = CoreUIButton().then {
Expand All @@ -48,9 +48,9 @@ final class AuthSignInController: CoreController, View, Stepper {
$0.setTitle(L10n.authSignUpTitle, for: .normal)
}
let labelErrors = CoreUILabel().then {
$0.numberOfLines = 4
$0.numberOfLines = 2
$0.textAlignment = .center
$0.textColor = UIColor.red
$0.textColor = Metric.onPrimary
}
let buttonForgot = UIButton().then {
$0.setTitle(L10n.authForgot, for: .normal)
Expand Down Expand Up @@ -108,6 +108,13 @@ final class AuthSignInController: CoreController, View, Stepper {

override func setupConstraints() {
self.width = self.view.frame.width
// errors
labelErrors.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
make.centerX.equalTo(self.view)
make.bottom.equalTo(self.inputEmail.snp.top).offset(-10)
}
// inputs
inputEmail.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
Expand Down Expand Up @@ -146,12 +153,6 @@ final class AuthSignInController: CoreController, View, Stepper {
buttonSignin.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(-40).keyboard(true, in: self.view)
}
labelErrors.snp.makeConstraints { (make) -> Void in
make.left.equalTo(25)
make.right.equalTo(-25)
make.centerX.equalTo(self.view)
make.centerY.equalTo(self.view).offset(120).keyboard(false, in: self.view)
}
buttonSignInApple.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
Expand All @@ -162,10 +163,6 @@ final class AuthSignInController: CoreController, View, Stepper {
make.right.equalTo(self.view.snp.left).keyboard(true, in: self.view)
make.centerY.equalTo(self.view).offset(65).keyboard(true, in: self.view)
}
// errors
labelErrors.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(20).keyboard(true, in: self.view)
}
// forgot
buttonForgot.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
Expand Down Expand Up @@ -288,25 +285,19 @@ private extension AuthSignInController {
.bind(to: self.rx.isAnimating)
.disposed(by: disposeBag)
// validation errors
reactor.state
.map { $0.errors }
.filter { $0.count > 0 }
.distinctUntilChanged { $0.count == $1.count }
.subscribe(onNext: { errors in
self.error.configureContent(title: "Schema", body: errors.map { "\($0.description)." }.joined(separator: "\n"))
self.error.button?.isHidden = true
SwiftMessages.hideAll()
SwiftMessages.show(config: self.popupConfig, view: self.error)
})
.disposed(by: self.disposeBag)
reactor.state
.map { $0.errors.count }
.distinctUntilChanged()
.subscribe(onNext: { _ in
.subscribe(onNext: { count in
if(count > 0) {
self.labelErrors.text = reactor.currentState.errors.first?.description
} else {
self.labelErrors.text = ""
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.email)" }) != nil {
self.inputEmail.layer.borderWidth = 1.0
self.inputEmail.error()
} else {
self.inputEmail.layer.borderWidth = 0
self.inputEmail.valid()
}
})
.disposed(by: self.disposeBag)
Expand Down
66 changes: 28 additions & 38 deletions waosSwift/modules/auth/controllers/AuthSignupController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ final class AuthSignUpController: CoreController, View, Stepper {

let inputFirstName = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-user")
$0.placeholder = L10n.authFirstname + "..."
$0.icon="fa-user"
}
let inputLastName = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-user")
$0.placeholder = L10n.authLastname + "..."
$0.icon="fa-user"
}
let inputEmail = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-envelope")
$0.placeholder = L10n.authMail + "..."
$0.autocapitalizationType = .none
$0.textContentType = .username
$0.icon="fa-envelope"
}
let inputPassword = CoreUITextField().then {
$0.autocorrectionType = .no
$0.setFontAwesomeIcon("fa-key")
$0.placeholder = L10n.authPassword + "..."
$0.autocapitalizationType = .none
$0.returnKeyType = .done
$0.isSecureTextEntry = true
$0.textContentType = .password
$0.icon="fa-key"
}
let progressPassword = UIProgressView().then {
$0.setProgress(0, animated: true)
Expand All @@ -57,9 +57,9 @@ final class AuthSignUpController: CoreController, View, Stepper {
$0.setTitleColor(Metric.secondary, for: .normal)
}
let labelErrors = CoreUILabel().then {
$0.numberOfLines = 5
$0.numberOfLines = 2
$0.textAlignment = .center
$0.textColor = UIColor.red
$0.textColor = Metric.onPrimary
}

// background
Expand Down Expand Up @@ -110,6 +110,13 @@ final class AuthSignUpController: CoreController, View, Stepper {

override func setupConstraints() {
self.width = self.view.frame.width
// errors
labelErrors.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
make.centerX.equalTo(self.view)
make.bottom.equalTo(self.inputFirstName.snp.top).offset(-10)
}
// inputs
inputFirstName.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
Expand Down Expand Up @@ -138,7 +145,6 @@ final class AuthSignUpController: CoreController, View, Stepper {
inputEmail.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(-100).keyboard(true, in: self.view)
}

inputPassword.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
Expand All @@ -148,7 +154,6 @@ final class AuthSignUpController: CoreController, View, Stepper {
inputPassword.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(-40).keyboard(true, in: self.view)
}

progressPassword.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(5)
Expand All @@ -158,7 +163,7 @@ final class AuthSignUpController: CoreController, View, Stepper {
progressPassword.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(-5).keyboard(true, in: self.view)
}

// buttons
buttonSignup.snp.makeConstraints { (make) -> Void in
make.width.equalTo(300)
make.height.equalTo(50)
Expand All @@ -168,15 +173,6 @@ final class AuthSignUpController: CoreController, View, Stepper {
buttonSignup.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(40).keyboard(true, in: self.view)
}
labelErrors.snp.makeConstraints { (make) -> Void in
make.left.equalTo(25)
make.right.equalTo(-25)
make.centerX.equalTo(self.view)
make.centerY.equalTo(self.view).offset(160).keyboard(false, in: self.view)
}
labelErrors.snp.prepareConstraints { (make) -> Void in
make.centerY.equalTo(self.view).offset(60).keyboard(true, in: self.view)
}
// background
self.backgroundImage.snp.makeConstraints { make in
make.top.equalTo(self.view)
Expand Down Expand Up @@ -332,40 +328,34 @@ private extension AuthSignUpController {
})
.disposed(by: self.disposeBag)
// validation errors
reactor.state
.map { $0.errors }
.filter { $0.count > 0 }
.distinctUntilChanged { $0.count == $1.count }
.subscribe(onNext: { errors in
self.error.configureContent(title: "Schema", body: errors.map { "\($0.description)." }.joined(separator: "\n"))
self.error.button?.isHidden = true
SwiftMessages.hideAll()
SwiftMessages.show(config: self.popupConfig, view: self.error)
})
.disposed(by: self.disposeBag)
reactor.state
.map { $0.errors.count }
.distinctUntilChanged()
.subscribe(onNext: { _ in
.subscribe(onNext: { count in
if(count > 0) {
self.labelErrors.text = reactor.currentState.errors.first?.description
} else {
self.labelErrors.text = ""
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.firstname)" }) != nil {
self.inputFirstName.layer.borderWidth = 1.0
self.inputFirstName.error()
} else {
self.inputFirstName.layer.borderWidth = 0
self.inputFirstName.valid()
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.lastname)" }) != nil {
self.inputLastName.layer.borderWidth = 1.0
self.inputLastName.error()
} else {
self.inputLastName.layer.borderWidth = 0
self.inputLastName.valid()
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.email)" }) != nil {
self.inputEmail.layer.borderWidth = 1.0
self.inputEmail.error()
} else {
self.inputEmail.layer.borderWidth = 0
self.inputEmail.valid()
}
if reactor.currentState.errors.firstIndex(where: { $0.title == "\(User.Validators.password)" }) != nil {
self.inputPassword.layer.borderWidth = 1.0
self.inputPassword.error()
} else {
self.inputPassword.layer.borderWidth = 0
self.inputPassword.valid()
}
})
.disposed(by: self.disposeBag)
Expand Down
Loading

0 comments on commit 4a4dd34

Please sign in to comment.