Skip to content
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
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!
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Copy link
Owner

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

self.showErrorAlert()
}
}

// MARK: Make Params
private extension LuzResetPasswordViewController {
func makeParams() -> [String: String] {
let email = emailTextfield.text!.trimmingCharacters(in: .whitespaces)
return [
"email" : email
]
func showSuccess() {
Copy link
Owner

Choose a reason for hiding this comment

The 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."
)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

salve salve @ppedroam !

Como a BadNetworkLayer.shared.resetPassword precisa de uma referencia pra viewModel fiquei com receio de criar a camada de service e passar uma referencia de viewController para a camada de serviço. Alguma sugestão?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

para solucionar isso deve se deixar de usar a BadNetworkLayer e começar usar os objetos da GoodNetworkLayer
image

}

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)
}
}