Skip to content
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

Feature/15 #22

Merged
merged 9 commits into from
Jul 13, 2020
2 changes: 1 addition & 1 deletion chat-iOS/App/Routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ struct Routes {
let isAuthenticated = true

if isAuthenticated { return MainTabBarViewBuilder.create() }
return LoginViewBuilder.create()
return UINavigationController(rootViewController: AuthTopViewBuilder.create())
}
}
8 changes: 8 additions & 0 deletions chat-iOS/Views/AuthTop/AuthTopViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
59 changes: 59 additions & 0 deletions chat-iOS/Views/Login/LoginModel.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion chat-iOS/Views/Login/LoginViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions chat-iOS/Views/Login/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
19 changes: 0 additions & 19 deletions chat-iOS/Views/Login/LoginViewModel.swift

This file was deleted.

35 changes: 29 additions & 6 deletions chat-iOS/Views/Login/LoginViewPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 6 additions & 0 deletions chat-iOS/Views/Storyboards/AuthTop.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<real key="value" value="8"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="signInButtonTapped:" destination="79b-Tc-bIU" eventType="touchUpInside" id="CW4-CB-rRZ"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="TYT-PT-4ds">
<rect key="frame" x="32" y="602" width="350" height="50"/>
Expand All @@ -46,6 +49,9 @@
<real key="value" value="8"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="signUpButtonTapped:" destination="79b-Tc-bIU" eventType="touchUpInside" id="EiI-eR-Xvn"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
Expand Down
9 changes: 8 additions & 1 deletion chat-iOS/Views/Storyboards/Login.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<constraint firstAttribute="height" constant="50" id="Lzk-bK-QU5"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<textInputTraits key="textInputTraits" secureTextEntry="YES"/>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="パスワード" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="o5O-uj-6LK">
<rect key="frame" x="40" y="179" width="87" height="21"/>
Expand All @@ -59,6 +59,9 @@
<real key="value" value="8"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="didTapSignInButton:" destination="PhT-f7-gGV" eventType="touchUpInside" id="xzO-t5-pL2"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
Expand All @@ -79,6 +82,10 @@
</constraints>
<viewLayoutGuide key="safeArea" id="LVU-Lx-83S"/>
</view>
<connections>
<outlet property="emailTextField" destination="CcZ-Od-rNk" id="nih-0M-Vxd"/>
<outlet property="passwordTextField" destination="dTe-Sc-UEj" id="TZJ-jy-x95"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Ti1-Tz-8LN" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down