-
Notifications
You must be signed in to change notification settings - Fork 10
Refactor: ResetPasswordViewController - ViewModel #27
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import UIKit | ||
|
|
||
| enum LuzResetPasswordFactory { | ||
| static func make() -> UIViewController { | ||
| let viewModel = LuzResetPasswordViewModel() | ||
| let viewController = LuzResetPasswordViewController(viewModel: viewModel) | ||
| viewModel.delegate = viewController | ||
| return viewController | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import UIKit | ||
|
|
||
| final class LuzResetPasswordViewController: UIViewController { | ||
|
|
||
| @IBOutlet weak var emailTextfield: UITextField! | ||
| @IBOutlet weak var recoverPasswordButton: UIButton! | ||
| @IBOutlet weak var loginButton: UIButton! | ||
|
|
@@ -15,61 +14,52 @@ final class LuzResetPasswordViewController: UIViewController { | |
| var email = "" | ||
| var recoveryEmail = false | ||
|
|
||
| private let viewModel: LuzResetPasswordViewModel | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| configureUI() | ||
| viewModel.delegate = self | ||
| } | ||
|
|
||
| public override var preferredStatusBarStyle: UIStatusBarStyle { | ||
| return .lightContent | ||
| } | ||
|
|
||
| init(viewModel: LuzResetPasswordViewModel) { | ||
| self.viewModel = viewModel | ||
| super.init(nibName: nil, bundle: nil) | ||
| } | ||
|
|
||
| required init?(coder: NSCoder) { | ||
| fatalError("init(coder:) has not been implemented") | ||
| } | ||
|
|
||
| // MARK: - IBActions | ||
| @IBAction func close(_ sender: Any) { | ||
| dismiss(animated: true) | ||
| } | ||
|
|
||
| @IBAction func recoverPasswordDidTap(_ sender: Any) { | ||
| @IBAction func didTapResetPassword(_ sender: Any) { | ||
| if recoveryEmail { | ||
| dismiss(animated: true) | ||
| return | ||
| } | ||
|
|
||
| do { | ||
| try FormValidator.validateEmail(emailTextfield.text) | ||
| try ConnectivityValidator.checkInternetConnection() | ||
| } catch { | ||
| handleError(error) | ||
| return | ||
| } | ||
|
|
||
| self.view.endEditing(true) | ||
|
|
||
| BadNetworkLayer | ||
| .shared | ||
| .resetPassword( | ||
| self, | ||
| parameters: makeParams() | ||
| ) { success in | ||
| guard success else { | ||
| self.showErrorAlert() | ||
| return | ||
| } | ||
| self.handleSucess() | ||
| } | ||
| guard let email = emailTextfield.text else { return } | ||
| viewModel.recoverPassword(from: self, email: email) | ||
| } | ||
|
|
||
| @IBAction func helpDidTap(_ sender: Any) { | ||
| let vc = LuzContactUsViewController() | ||
| vc.modalPresentationStyle = .fullScreen | ||
| vc.modalTransitionStyle = .coverVertical | ||
| self.present(vc, animated: true, completion: nil) | ||
| @IBAction func didTapContactUS(_ sender: Any) { | ||
| let contactUsViewController = LuzContactUsViewController() | ||
| contactUsViewController.modalPresentationStyle = .fullScreen | ||
| contactUsViewController.modalTransitionStyle = .coverVertical | ||
| self.present(contactUsViewController, animated: true, completion: nil) | ||
| } | ||
|
|
||
| @IBAction func createAccountDidTap(_ sender: Any) { | ||
| let newVc = LuzCreateAccountViewController() | ||
| newVc.modalPresentationStyle = .fullScreen | ||
| present(newVc, animated: true) | ||
| @IBAction func didTapCreateAccount(_ sender: Any) { | ||
| let createAccountViewController = LuzCreateAccountViewController() | ||
| createAccountViewController.modalPresentationStyle = .fullScreen | ||
| present(createAccountViewController, animated: true) | ||
| } | ||
|
|
||
| @IBAction func emailBeginEditing(_ sender: Any) { | ||
|
|
@@ -171,30 +161,14 @@ private extension LuzResetPasswordViewController { | |
| alertController.addAction(action) | ||
| self.present(alertController, animated: true) | ||
| } | ||
| } | ||
|
|
||
| func handleError(_ error: Error) { | ||
| emailTextfield.setErrorColor() | ||
| textLabel.textColor = .red | ||
|
|
||
| switch error { | ||
| case ValidationError.emptyEmail: | ||
| return textLabel.text = "E-mail não pode estar vazio" | ||
| case ValidationError.invalidFormat: | ||
| return textLabel.text = "Verifique o email informado" | ||
| case ConnectivityError.noInternet: | ||
| return Globals.showNoInternetCOnnection(controller: self) | ||
| default: | ||
| textLabel.text = "Ocorreu um erro inesperado" | ||
| } | ||
| extension LuzResetPasswordViewController: LuzResetPasswordViewModelDelegate { | ||
| func showError(_ message: String) { | ||
| self.showErrorAlert() | ||
| } | ||
| } | ||
|
|
||
| // MARK: Make Params | ||
| private extension LuzResetPasswordViewController { | ||
| func makeParams() -> [String: String] { | ||
| let email = emailTextfield.text!.trimmingCharacters(in: .whitespaces) | ||
| return [ | ||
| "email" : email | ||
| ] | ||
| func showSuccess() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. showSucces what? melhorar nome |
||
| self.handleSucess() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import UIKit | ||
|
|
||
| protocol LuzResetPasswordViewModelDelegate: AnyObject { | ||
| func showError(_ message: String) | ||
| func showSuccess() | ||
| } | ||
|
|
||
| final class LuzResetPasswordViewModel { | ||
| weak var delegate: LuzResetPasswordViewModelDelegate? | ||
|
|
||
| func recoverPassword( | ||
| from viewController: UIViewController, | ||
| email: String | ||
| ) { | ||
| do { | ||
| try FormValidator.validateEmail(email) | ||
| try ConnectivityValidator.checkInternetConnection() | ||
|
|
||
| } catch { | ||
| handleError(error) | ||
| return | ||
| } | ||
|
|
||
| BadNetworkLayer.shared.resetPassword( | ||
| viewController, | ||
| parameters: ["email": email] | ||
| ) { success in | ||
| success ? self.delegate?.showSuccess() : self.delegate?.showError( | ||
| "Algo de errado aconteceu. Tente novamente mais tarde." | ||
| ) | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. salve salve @ppedroam ! Como a
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| private func handleError(_ error: Error) { | ||
| let errorMessage: String | ||
| switch error { | ||
| case ValidationError.emptyEmail: | ||
| errorMessage = "E-mail não pode estar vazio" | ||
| case ValidationError.invalidFormat: | ||
| errorMessage = "Verifique o email informado" | ||
| case ConnectivityError.noInternet: | ||
| errorMessage = "Sem conexão com a internet" | ||
| default: | ||
| errorMessage = "Ocorreu um erro inesperado" | ||
| } | ||
| delegate?.showError(errorMessage) | ||
| } | ||
| } | ||

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.
show error what? alerta? melhorar nome