diff --git a/chat-iOS/App/Routes.swift b/chat-iOS/App/Routes.swift index 30f55d8..89bacad 100755 --- a/chat-iOS/App/Routes.swift +++ b/chat-iOS/App/Routes.swift @@ -13,6 +13,6 @@ struct Routes { let isAuthenticated = true if isAuthenticated { return MainTabBarViewBuilder.create() } - return LoginViewBuilder.create() + return UINavigationController(rootViewController: AuthTopViewBuilder.create()) } } diff --git a/chat-iOS/Views/AuthTop/AuthTopViewController.swift b/chat-iOS/Views/AuthTop/AuthTopViewController.swift index 7c592c5..bb3e292 100644 --- a/chat-iOS/Views/AuthTop/AuthTopViewController.swift +++ b/chat-iOS/Views/AuthTop/AuthTopViewController.swift @@ -9,4 +9,12 @@ import UIKit class AuthTopViewController: UIViewController { + @IBAction func signUpButtonTapped(_ sender: Any) { + } + + @IBAction func signInButtonTapped(_ sender: Any) { + let loginViewController = LoginViewBuilder.create() + navigationController?.pushViewController(loginViewController, animated: true) + } + } diff --git a/chat-iOS/Views/Login/LoginModel.swift b/chat-iOS/Views/Login/LoginModel.swift new file mode 100644 index 0000000..ea60712 --- /dev/null +++ b/chat-iOS/Views/Login/LoginModel.swift @@ -0,0 +1,59 @@ +// +// LoginViewModel.swift +// chat-iOS +// +// Created by 戸高新也 on 2020/06/18. +// + +import FirebaseFirestore +import FirebaseAuth + +protocol LoginModelProtocol { + var presenter: LoginModelOutput! { get set } + func signIn(withEmail email: String, password: String) +} + +protocol LoginModelOutput: class { + func successSignIn(withUser user: User) + func onError(error: Error?) +} + +final class LoginModel: LoginModelProtocol { + weak var presenter: LoginModelOutput! + + func signIn(withEmail email: String, password: String) { + Auth.auth().signIn(withEmail: email, password: password) { [weak self] (result, error) in + if let err = error { + self?.presenter.onError(error: err) + return + } + + guard let uid = result?.user.uid else { + self?.presenter.onError(error: nil) + return + } + + let userReference = Firestore.firestore() + .collection("message").document("v1") + .collection("users").document(uid) + + userReference.getDocument { (snapshot, error) in + if let err = error { + self?.presenter.onError(error: err) + return + } + + if let snapshot = snapshot { + do { + guard let user = try snapshot.data(as: User.self) else { + return + } + self?.presenter.successSignIn(withUser: user) + } catch let err { + self?.presenter.onError(error: err) + } + } + } + } + } +} diff --git a/chat-iOS/Views/Login/LoginViewBuilder.swift b/chat-iOS/Views/Login/LoginViewBuilder.swift index 74d8683..9b53546 100644 --- a/chat-iOS/Views/Login/LoginViewBuilder.swift +++ b/chat-iOS/Views/Login/LoginViewBuilder.swift @@ -12,7 +12,7 @@ struct LoginViewBuilder { guard let loginViewController = LoginViewController.loadFromStoryboard() as? LoginViewController else { fatalError("fatal: Failed to initialize the SampleViewController") } - let model = LoginViewModel() + let model = LoginModel() let presenter = LoginViewPresenter(model: model) loginViewController.inject(with: presenter) return loginViewController diff --git a/chat-iOS/Views/Login/LoginViewController.swift b/chat-iOS/Views/Login/LoginViewController.swift index 6a60f75..d9f871a 100644 --- a/chat-iOS/Views/Login/LoginViewController.swift +++ b/chat-iOS/Views/Login/LoginViewController.swift @@ -6,20 +6,67 @@ // import UIKit +import FirebaseAuth final class LoginViewController: UIViewController { private var presenter: LoginViewPresenterProtocol! + @IBOutlet weak var emailTextField: UITextField! + @IBOutlet weak var passwordTextField: UITextField! + + @IBAction func didTapSignInButton(_ sender: Any) { + guard let email = emailTextField.text, let password = passwordTextField.text else { + return + } + didTapSignInButton(email: email, password: password) + } + override func viewDidLoad() { super.viewDidLoad() + + setupViews() + } + + private func setupViews() { + emailTextField.delegate = self + passwordTextField.delegate = self + + view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismissKeyboard))) + } + + @objc private func handleDismissKeyboard() { + view.endEditing(true) } func inject(with presenter: LoginViewPresenterProtocol) { self.presenter = presenter self.presenter.view = self } + + func didTapSignInButton(email: String, password: String) { + presenter.didTapSignInButton(email: email, password: password) + } } extension LoginViewController: LoginViewPresenterOutput { + + func transitionToMainTabBar(withUser user: User) { + //TODO:- userを使ってMainTabBarへの遷移処理を書く + print(user.displayName) + } + + func showAlert(withMessage message: String) { + let alert = UIAlertController(title: "エラーが発生しました", message: message, preferredStyle: .alert) + let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: nil) + + alert.addAction(defaultAction) + present(alert, animated: false, completion: nil) + } +} +extension LoginViewController: UITextFieldDelegate { + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + textField.resignFirstResponder() + return true + } } diff --git a/chat-iOS/Views/Login/LoginViewModel.swift b/chat-iOS/Views/Login/LoginViewModel.swift deleted file mode 100644 index d9f1236..0000000 --- a/chat-iOS/Views/Login/LoginViewModel.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// LoginViewModel.swift -// chat-iOS -// -// Created by 戸高新也 on 2020/06/18. -// - - -protocol LoginViewModelProtocol { - var presenter: LoginViewModelOutput! { get set } -} - -protocol LoginViewModelOutput { - -} - -final class LoginViewModel: LoginViewModelProtocol { - var presenter: LoginViewModelOutput! -} diff --git a/chat-iOS/Views/Login/LoginViewPresenter.swift b/chat-iOS/Views/Login/LoginViewPresenter.swift index 721a40d..d1d5659 100644 --- a/chat-iOS/Views/Login/LoginViewPresenter.swift +++ b/chat-iOS/Views/Login/LoginViewPresenter.swift @@ -5,19 +5,42 @@ // Created by 戸高新也 on 2020/06/18. // +import FirebaseFirestore +import FirebaseAuth + protocol LoginViewPresenterProtocol { var view: LoginViewPresenterOutput! { get set } + func didTapSignInButton(email: String, password: String) } -protocol LoginViewPresenterOutput { - +protocol LoginViewPresenterOutput: class { + func transitionToMainTabBar(withUser user: User) + func showAlert(withMessage message: String) } -final class LoginViewPresenter: LoginViewPresenterProtocol, LoginViewModelOutput { - var view: LoginViewPresenterOutput! - private var model: LoginViewModelProtocol +final class LoginViewPresenter: LoginViewPresenterProtocol, LoginModelOutput { + weak var view: LoginViewPresenterOutput! + private var model: LoginModelProtocol - init(model: LoginViewModelProtocol) { + init(model: LoginModelProtocol) { self.model = model + self.model.presenter = self + } + + func didTapSignInButton(email: String, password: String) { + model.signIn(withEmail: email, password: password) + } + + func successSignIn(withUser user: User) { + DispatchQueue.main.async { [weak self] in + self?.view.transitionToMainTabBar(withUser: user) + } + } + + func onError(error: Error?) { + DispatchQueue.main.async { [weak self] in + let message = error?.localizedDescription ?? "ログインできませんでした" + self?.view.showAlert(withMessage: message) + } } } diff --git a/chat-iOS/Views/Storyboards/AuthTop.storyboard b/chat-iOS/Views/Storyboards/AuthTop.storyboard index 25695ff..8ff4acb 100644 --- a/chat-iOS/Views/Storyboards/AuthTop.storyboard +++ b/chat-iOS/Views/Storyboards/AuthTop.storyboard @@ -30,6 +30,9 @@ + + + diff --git a/chat-iOS/Views/Storyboards/Login.storyboard b/chat-iOS/Views/Storyboards/Login.storyboard index cc162a8..39957d2 100644 --- a/chat-iOS/Views/Storyboards/Login.storyboard +++ b/chat-iOS/Views/Storyboards/Login.storyboard @@ -36,7 +36,7 @@ - +