2022-02-14 18:05:21 +02:00
|
|
|
//
|
|
|
|
// AppCoordinator.swift
|
|
|
|
// ElementX
|
|
|
|
//
|
|
|
|
// Created by Stefan Ceriu on 11.02.2022.
|
2022-05-31 13:05:30 +03:00
|
|
|
// Copyright © 2022 Element. All rights reserved.
|
2022-02-14 18:05:21 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-05-26 16:04:48 +03:00
|
|
|
import Kingfisher
|
2022-02-14 18:05:21 +02:00
|
|
|
|
|
|
|
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!
|
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
private var userSession: UserSession!
|
|
|
|
|
2022-04-01 16:05:26 +03:00
|
|
|
private let memberDetailProviderManager: MemberDetailProviderManager
|
2022-05-26 16:04:48 +03:00
|
|
|
|
2022-05-11 16:27:30 +03:00
|
|
|
private var indicatorPresenter: UserIndicatorTypePresenterProtocol
|
|
|
|
private var loadingIndicator: UserIndicator?
|
|
|
|
private var errorIndicator: UserIndicator?
|
2022-02-22 11:24:46 +02:00
|
|
|
|
2022-02-14 18:05:21 +02:00
|
|
|
var childCoordinators: [Coordinator] = []
|
|
|
|
|
|
|
|
init() {
|
|
|
|
splashViewController = SplashViewController()
|
|
|
|
mainNavigationController = UINavigationController(rootViewController: splashViewController)
|
|
|
|
window = UIWindow(frame: UIScreen.main.bounds)
|
|
|
|
window.rootViewController = mainNavigationController
|
|
|
|
|
|
|
|
navigationRouter = NavigationRouter(navigationController: mainNavigationController)
|
|
|
|
|
2022-04-01 16:05:26 +03:00
|
|
|
memberDetailProviderManager = MemberDetailProviderManager()
|
|
|
|
|
2022-05-11 16:27:30 +03:00
|
|
|
indicatorPresenter = UserIndicatorTypePresenter(presentingViewController: mainNavigationController)
|
|
|
|
|
2022-02-14 18:05:21 +02:00
|
|
|
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
|
2022-04-05 10:01:16 +03:00
|
|
|
|
|
|
|
let loggerConfiguration = MXLogConfiguration()
|
|
|
|
loggerConfiguration.logLevel = .verbose
|
|
|
|
MXLog.configure(loggerConfiguration)
|
|
|
|
|
|
|
|
// Benchmark.trackingEnabled = true
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
|
|
|
window.makeKeyAndVisible()
|
|
|
|
authenticationCoordinator.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - AuthenticationCoordinatorDelegate
|
|
|
|
|
2022-02-22 09:18:46 +02:00
|
|
|
func authenticationCoordinatorDidStartLoading(_ authenticationCoordinator: AuthenticationCoordinator) {
|
2022-02-22 11:24:46 +02:00
|
|
|
showLoadingIndicator()
|
2022-02-22 09:18:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 18:05:21 +02:00
|
|
|
func authenticationCoordinator(_ authenticationCoordinator: AuthenticationCoordinator, didFailWithError error: AuthenticationCoordinatorError) {
|
2022-02-22 11:24:46 +02:00
|
|
|
hideLoadingIndicator()
|
2022-02-22 13:53:34 +02:00
|
|
|
showLoginErrorToast()
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
func authenticationCoordinatorDidSetupClientProxy(_ authenticationCoordinator: AuthenticationCoordinator) {
|
|
|
|
guard let clientProxy = authenticationCoordinator.clientProxy else {
|
|
|
|
fatalError("User session should be setup at this point")
|
|
|
|
}
|
|
|
|
|
|
|
|
userSession = .init(clientProxy: clientProxy,
|
|
|
|
mediaProvider: MediaProvider(clientProxy: clientProxy, imageCache: ImageCache.default))
|
|
|
|
|
2022-02-14 18:05:21 +02:00
|
|
|
presentHomeScreen()
|
|
|
|
}
|
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
func authenticationCoordinatorDidTearDownClientProxy(_ authenticationCoordinator: AuthenticationCoordinator) {
|
2022-03-01 14:05:35 +02:00
|
|
|
if let presentedCoordinator = childCoordinators.first {
|
|
|
|
remove(childCoordinator: presentedCoordinator)
|
|
|
|
}
|
2022-05-26 16:04:48 +03:00
|
|
|
|
2022-02-22 09:18:46 +02:00
|
|
|
mainNavigationController.setViewControllers([splashViewController], animated: false)
|
|
|
|
authenticationCoordinator.start()
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func presentHomeScreen() {
|
2022-02-22 11:24:46 +02:00
|
|
|
|
|
|
|
hideLoadingIndicator()
|
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
guard let userSession = userSession else {
|
2022-02-14 18:05:21 +02:00
|
|
|
fatalError("User session should be already setup at this point")
|
|
|
|
}
|
|
|
|
|
2022-04-01 14:07:33 +03:00
|
|
|
let parameters = HomeScreenCoordinatorParameters(userSession: userSession,
|
2022-04-01 16:05:26 +03:00
|
|
|
attributedStringBuilder: AttributedStringBuilder(),
|
|
|
|
memberDetailProviderManager: memberDetailProviderManager)
|
2022-03-17 18:09:29 +02:00
|
|
|
let coordinator = HomeScreenCoordinator(parameters: parameters)
|
2022-02-14 18:05:21 +02:00
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
coordinator.callback = { [weak self] action in
|
|
|
|
switch action {
|
2022-02-22 09:18:46 +02:00
|
|
|
case .logout:
|
|
|
|
self?.authenticationCoordinator.logout()
|
2022-03-08 14:24:33 +02:00
|
|
|
case .selectRoom(let roomIdentifier):
|
|
|
|
self?.presentRoomWithIdentifier(roomIdentifier)
|
2022-02-22 09:18:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:05:21 +02:00
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
navigationRouter.setRootModule(coordinator)
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:24:33 +02:00
|
|
|
private func presentRoomWithIdentifier(_ roomIdentifier: String) {
|
2022-05-26 16:04:48 +03:00
|
|
|
guard let userSession = userSession else {
|
2022-03-08 14:24:33 +02:00
|
|
|
fatalError("User session should be already setup at this point")
|
|
|
|
}
|
|
|
|
|
2022-05-26 16:04:48 +03:00
|
|
|
guard let roomProxy = userSession.clientProxy.rooms.first(where: { $0.id == roomIdentifier }) else {
|
2022-03-31 15:37:52 +03:00
|
|
|
MXLog.error("Invalid room identifier: \(roomIdentifier)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:05:26 +03:00
|
|
|
let memberDetailProvider = memberDetailProviderManager.memberDetailProviderForRoomProxy(roomProxy)
|
2022-03-31 15:37:52 +03:00
|
|
|
|
|
|
|
let timelineItemFactory = RoomTimelineItemFactory(mediaProvider: userSession.mediaProvider,
|
2022-04-01 16:05:26 +03:00
|
|
|
memberDetailProvider: memberDetailProvider,
|
2022-03-31 15:37:52 +03:00
|
|
|
attributedStringBuilder: AttributedStringBuilder())
|
2022-03-08 14:24:33 +02:00
|
|
|
|
2022-03-31 15:37:52 +03:00
|
|
|
let timelineController = RoomTimelineController(timelineProvider: RoomTimelineProvider(roomProxy: roomProxy),
|
|
|
|
timelineItemFactory: timelineItemFactory,
|
|
|
|
mediaProvider: userSession.mediaProvider,
|
2022-04-01 16:05:26 +03:00
|
|
|
memberDetailProvider: memberDetailProvider)
|
2022-03-31 15:37:52 +03:00
|
|
|
|
|
|
|
let parameters = RoomScreenCoordinatorParameters(timelineController: timelineController,
|
2022-05-16 13:28:35 +03:00
|
|
|
roomName: roomProxy.displayName ?? roomProxy.name)
|
2022-03-31 15:37:52 +03:00
|
|
|
let coordinator = RoomScreenCoordinator(parameters: parameters)
|
|
|
|
|
|
|
|
self.add(childCoordinator: coordinator)
|
|
|
|
self.navigationRouter.push(coordinator) { [weak self] in
|
2022-03-08 14:24:33 +02:00
|
|
|
guard let self = self else { return }
|
|
|
|
|
2022-03-31 15:37:52 +03:00
|
|
|
self.remove(childCoordinator: coordinator)
|
2022-03-08 14:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:24:46 +02:00
|
|
|
private func showLoadingIndicator() {
|
2022-05-11 16:27:30 +03:00
|
|
|
loadingIndicator = indicatorPresenter.present(.loading(label: "Loading", isInteractionBlocking: true))
|
2022-02-22 11:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func hideLoadingIndicator() {
|
2022-05-11 16:27:30 +03:00
|
|
|
loadingIndicator = nil
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
2022-02-22 13:53:34 +02:00
|
|
|
|
|
|
|
private func showLoginErrorToast() {
|
2022-05-11 16:27:30 +03:00
|
|
|
errorIndicator = indicatorPresenter.present(.success(label: "Failed logging in"))
|
2022-02-22 13:53:34 +02:00
|
|
|
}
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|