Skip to content

Instantly share code, notes, and snippets.

@o-nnerb
Created April 29, 2023 18:09
Show Gist options
  • Save o-nnerb/0718f923b85ea435735becc6d06c5121 to your computer and use it in GitHub Desktop.
Save o-nnerb/0718f923b85ea435735becc6d06c5121 to your computer and use it in GitHub Desktop.
import Foundation
protocol LoginPresenterInput {
func emailDidChange(_ email: String)
func passwordDidChange(_ password: String)
func submit()
}
protocol LoginPresenterOutput: AnyObject {
func submit(isEnabled: Bool)
func loading(_ isLoading: Bool)
}
class LoginPresenter: BasePresenter<LoginInteractorInput, LoginCoordinating> {
// MARK: - Properties
var email: String = ""
var password: String = ""
var isEmailValid: Bool = false
var isPasswordValid: Bool = false
var isLoading: Bool = false
private(set) weak var view: LoginPresenterOutput?
var isValid: Bool {
isEmailValid && isPasswordValid
}
func attach(_ view: LoginPresenterOutput) {
self.view = view
}
}
extension LoginPresenter: LoginPresenterInput {
func emailDidChange(_ email: String) {
interactor.validate(email: email)
}
func passwordDidChange(_ password: String) {
interactor.validate(password: password)
}
func submit() {
guard isValid && !isLoading else {
return
}
isLoading = true
view?.loading(isLoading)
interactor.login(email, password)
}
}
extension LoginPresenter: LoginInteractorOutput {
func email(isValid: Bool) {
isEmailValid = isValid
view?.submit(isEnabled: isValid)
}
func password(isValid: Bool) {
isPasswordValid = isValid
view?.submit(isEnabled: isValid)
}
func login(_ result: Result<Void, Error>) {
isLoading = false
view?.loading(isLoading)
switch result {
case .success:
coordinator.openHomeScene()
case .failure(let error):
coordinator.openErrorScene(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment