mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-11 13:59:13 +00:00
123 lines
4.2 KiB
Swift
123 lines
4.2 KiB
Swift
//
|
|
// AppCoordinator.swift
|
|
// ElementX
|
|
//
|
|
// Created by Stefan Ceriu on 11.02.2022.
|
|
//
|
|
|
|
import UIKit
|
|
import Kingfisher
|
|
|
|
class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator {
|
|
private let window: UIWindow
|
|
|
|
private let mainNavigationController: UINavigationController
|
|
private let splashViewController: UIViewController
|
|
|
|
private let navigationRouter: NavigationRouter
|
|
|
|
private let keychainController: KeychainControllerProtocol
|
|
private let authenticationCoordinator: AuthenticationCoordinator!
|
|
|
|
private var loadingActivity: Activity?
|
|
private var errorActivity: Activity?
|
|
|
|
var childCoordinators: [Coordinator] = []
|
|
|
|
init() {
|
|
splashViewController = SplashViewController()
|
|
mainNavigationController = UINavigationController(rootViewController: splashViewController)
|
|
mainNavigationController.navigationBar.isHidden = true
|
|
window = UIWindow(frame: UIScreen.main.bounds)
|
|
window.rootViewController = mainNavigationController
|
|
|
|
navigationRouter = NavigationRouter(navigationController: mainNavigationController)
|
|
|
|
guard let bundleIdentifier = Bundle.main.bundleIdentifier else {
|
|
fatalError("Should have a valid bundle identifier at this point")
|
|
}
|
|
|
|
keychainController = KeychainController(identifier: bundleIdentifier)
|
|
authenticationCoordinator = AuthenticationCoordinator(keychainController: keychainController,
|
|
navigationRouter: navigationRouter)
|
|
authenticationCoordinator.delegate = self
|
|
}
|
|
|
|
func start() {
|
|
window.makeKeyAndVisible()
|
|
authenticationCoordinator.start()
|
|
}
|
|
|
|
// MARK: - AuthenticationCoordinatorDelegate
|
|
|
|
func authenticationCoordinatorDidStartLoading(_ authenticationCoordinator: AuthenticationCoordinator) {
|
|
showLoadingIndicator()
|
|
}
|
|
|
|
func authenticationCoordinator(_ authenticationCoordinator: AuthenticationCoordinator, didFailWithError error: AuthenticationCoordinatorError) {
|
|
hideLoadingIndicator()
|
|
showLoginErrorToast()
|
|
}
|
|
|
|
func authenticationCoordinatorDidSetupUserSession(_ authenticationCoordinator: AuthenticationCoordinator) {
|
|
presentHomeScreen()
|
|
}
|
|
|
|
func authenticationCoordinatorDidTearDownUserSession(_ authenticationCoordinator: AuthenticationCoordinator) {
|
|
mainNavigationController.setViewControllers([splashViewController], animated: false)
|
|
authenticationCoordinator.start()
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func presentHomeScreen() {
|
|
|
|
hideLoadingIndicator()
|
|
|
|
guard let userSession = authenticationCoordinator.userSession else {
|
|
fatalError("User session should be already setup at this point")
|
|
}
|
|
|
|
let parameters = HomeScreenCoordinatorParameters(userSession: userSession)
|
|
let coordinator = HomeScreenCoordinator(parameters: parameters, imageCache: ImageCache.default)
|
|
|
|
coordinator.completion = { [weak self] result in
|
|
switch result {
|
|
case .logout:
|
|
self?.authenticationCoordinator.logout()
|
|
}
|
|
}
|
|
|
|
add(childCoordinator: coordinator)
|
|
navigationRouter.setRootModule(coordinator)
|
|
}
|
|
|
|
private func showLoadingIndicator() {
|
|
let presenter = FullscreenLoadingActivityPresenter(label: "Loading",
|
|
on: mainNavigationController)
|
|
|
|
let request = ActivityRequest(
|
|
presenter: presenter,
|
|
dismissal: .manual
|
|
)
|
|
|
|
loadingActivity = ActivityCenter.shared.add(request)
|
|
}
|
|
|
|
private func hideLoadingIndicator() {
|
|
loadingActivity = nil
|
|
}
|
|
|
|
private func showLoginErrorToast() {
|
|
let presenter = ToastActivityPresenter(viewState: .init(style: .success, label: "Failed logging in"),
|
|
navigationController: mainNavigationController)
|
|
|
|
let request = ActivityRequest(
|
|
presenter: presenter,
|
|
dismissal: .timeout(3.0)
|
|
)
|
|
|
|
errorActivity = ActivityCenter.shared.add(request)
|
|
}
|
|
}
|