diff --git a/chat-iOS/App/AppDelegate.swift b/chat-iOS/App/AppDelegate.swift index 37bc5a9..14ea106 100755 --- a/chat-iOS/App/AppDelegate.swift +++ b/chat-iOS/App/AppDelegate.swift @@ -18,7 +18,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { if #available(iOS 13, *) { } else { window = UIWindow(frame: UIScreen.main.bounds) - window?.rootViewController = Routes.decideRootViewController() + window?.rootViewController = RootViewController() window?.makeKeyAndVisible() } return true diff --git a/chat-iOS/App/RootViewController.swift b/chat-iOS/App/RootViewController.swift new file mode 100644 index 0000000..fe6b89b --- /dev/null +++ b/chat-iOS/App/RootViewController.swift @@ -0,0 +1,72 @@ +// +// RootViewController.swift +// chat-iOS +// +// Created by 戸高新也 on 2020/07/13. +// + +import UIKit +import FirebaseAuth + +class RootViewController: UIViewController { + + static var instance: RootViewController { + return UIApplication.shared.keyWindow!.rootViewController as! RootViewController + } + + enum ViewType { + case login + case main + } + + private var viewType: ViewType? { + didSet { + guard let type = viewType, oldValue != type else { return } + switch type { + case .login: + currentViewController = UINavigationController(rootViewController: AuthTopViewBuilder.create()) + case .main: + currentViewController = MainTabBarViewBuilder.create() + } + } + } + + private(set) var currentViewController: UIViewController? { + didSet { + guard let currentViewController = currentViewController else { return } + + addChild(currentViewController) + view.addSubview(currentViewController.view) + currentViewController.didMove(toParent: self) + currentViewController.view.frame = view.bounds + + guard let oldViewController = oldValue else { return } + + view.sendSubviewToBack(currentViewController.view) + UIView.transition(from: oldViewController.view, to: currentViewController.view, duration: 0.35, options: .transitionCrossDissolve) { _ in + oldViewController.willMove(toParent: nil) + oldViewController.view.removeFromSuperview() + oldViewController.removeFromParent() + } + } + } + + override func loadView() { + super.loadView() + view.backgroundColor = .white + } + + override func viewDidLoad() { + super.viewDidLoad() + + if Auth.auth().currentUser?.uid == nil { + self.viewType = .login + } else { + self.viewType = .main + } + } + + func transition(to type: ViewType) { + self.viewType = type + } +} diff --git a/chat-iOS/App/SceneDelegate.swift b/chat-iOS/App/SceneDelegate.swift index 7b1137f..0018899 100755 --- a/chat-iOS/App/SceneDelegate.swift +++ b/chat-iOS/App/SceneDelegate.swift @@ -16,7 +16,7 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate { func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let scene = (scene as? UIWindowScene) else { return } window = UIWindow(windowScene: scene) - window?.rootViewController = Routes.decideRootViewController() + window?.rootViewController = RootViewController() window?.makeKeyAndVisible() } } diff --git a/chat-iOS/Views/Login/LoginViewController.swift b/chat-iOS/Views/Login/LoginViewController.swift index d9f871a..1d270ce 100644 --- a/chat-iOS/Views/Login/LoginViewController.swift +++ b/chat-iOS/Views/Login/LoginViewController.swift @@ -52,7 +52,7 @@ extension LoginViewController: LoginViewPresenterOutput { func transitionToMainTabBar(withUser user: User) { //TODO:- userを使ってMainTabBarへの遷移処理を書く - print(user.displayName) + RootViewController.instance.transition(to: .main) } func showAlert(withMessage message: String) {