mastodon-ios/Mastodon/Coordinator/SceneCoordinator.swift

342 lines
14 KiB
Swift
Raw Normal View History

2021-01-27 07:50:13 +01:00
//
// SceneCoordinator.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-1-27.
import UIKit
import SafariServices
2021-02-20 06:56:24 +01:00
import CoreDataStack
2021-01-27 07:50:13 +01:00
final public class SceneCoordinator {
private weak var scene: UIScene!
private weak var sceneDelegate: SceneDelegate!
private weak var appContext: AppContext!
private(set) weak var tabBarController: MainTabBarController!
2021-01-27 07:50:13 +01:00
let id = UUID().uuidString
init(scene: UIScene, sceneDelegate: SceneDelegate, appContext: AppContext) {
self.scene = scene
self.sceneDelegate = sceneDelegate
self.appContext = appContext
scene.session.sceneCoordinator = self
}
}
extension SceneCoordinator {
enum Transition {
case show // push
case showDetail // replace
case modal(animated: Bool, completion: (() -> Void)? = nil)
case custom(transitioningDelegate: UIViewControllerTransitioningDelegate)
case customPush
case safariPresent(animated: Bool, completion: (() -> Void)? = nil)
case alertController(animated: Bool, completion: (() -> Void)? = nil)
case activityViewControllerPresent(animated: Bool, completion: (() -> Void)? = nil)
2021-01-27 07:50:13 +01:00
}
enum Scene {
2021-02-26 11:27:47 +01:00
// onboarding
2021-02-20 06:56:24 +01:00
case welcome
2021-02-26 11:27:47 +01:00
case mastodonPickServer(viewMode: MastodonPickServerViewModel)
2021-02-05 10:53:00 +01:00
case mastodonRegister(viewModel: MastodonRegisterViewModel)
2021-02-22 09:20:44 +01:00
case mastodonServerRules(viewModel: MastodonServerRulesViewModel)
2021-02-23 08:38:05 +01:00
case mastodonConfirmEmail(viewModel: MastodonConfirmEmailViewModel)
case mastodonResendEmail(viewModel: MastodonResendEmailViewModel)
case mastodonWebView(viewModel:WebViewModel)
2021-02-03 09:01:08 +01:00
2021-06-22 07:41:40 +02:00
#if ASDK
2021-06-21 14:27:43 +02:00
// ASDK
case asyncHome
2021-06-22 07:41:40 +02:00
#endif
// search
case searchDetail(viewModel: SearchDetailViewModel)
2021-03-11 08:41:27 +01:00
// compose
case compose(viewModel: ComposeViewModel)
2021-04-13 13:46:42 +02:00
// thread
case thread(viewModel: ThreadViewModel)
2021-04-01 04:12:57 +02:00
// Hashtag Timeline
case hashtagTimeline(viewModel: HashtagTimelineViewModel)
2021-04-01 08:39:15 +02:00
// profile
case profile(viewModel: ProfileViewModel)
2021-04-07 08:24:28 +02:00
case favorite(viewModel: FavoriteViewModel)
2021-04-01 08:39:15 +02:00
// setting
case settings(viewModel: SettingsViewModel)
// report
case report(viewModel: ReportViewModel)
2021-04-21 08:46:31 +02:00
// suggestion account
case suggestionAccount(viewModel: SuggestionAccountViewModel)
// media preview
case mediaPreview(viewModel: MediaPreviewViewModel)
2021-02-26 11:27:47 +01:00
// misc
case safari(url: URL)
case alertController(alertController: UIAlertController)
case activityViewController(activityViewController: UIActivityViewController, sourceView: UIView?, barButtonItem: UIBarButtonItem?)
2021-02-24 11:07:11 +01:00
#if DEBUG
case publicTimeline
#endif
2021-02-26 11:27:47 +01:00
var isOnboarding: Bool {
switch self {
case .welcome,
.mastodonPickServer,
.mastodonRegister,
.mastodonServerRules,
.mastodonConfirmEmail,
.mastodonResendEmail:
return true
default:
return false
}
}
2021-01-27 07:50:13 +01:00
}
}
extension SceneCoordinator {
func setup() {
2021-02-26 11:27:47 +01:00
let viewController = MainTabBarController(context: appContext, coordinator: self)
sceneDelegate.window?.rootViewController = viewController
2021-04-21 08:46:31 +02:00
tabBarController = viewController
2021-02-26 11:27:47 +01:00
}
func setupOnboardingIfNeeds(animated: Bool) {
// Check user authentication status and show onboarding if needs
2021-02-20 06:56:24 +01:00
do {
2021-02-26 11:27:47 +01:00
let request = MastodonAuthentication.sortedFetchRequest
if try appContext.managedObjectContext.count(for: request) == 0 {
2021-02-26 11:27:47 +01:00
DispatchQueue.main.async {
self.present(
scene: .welcome,
from: nil,
transition: .modal(animated: animated, completion: nil)
)
2021-02-20 06:56:24 +01:00
}
}
} catch {
2021-02-26 11:27:47 +01:00
assertionFailure(error.localizedDescription)
2021-02-20 06:56:24 +01:00
}
2021-01-27 07:50:13 +01:00
}
@discardableResult
func present(scene: Scene, from sender: UIViewController?, transition: Transition) -> UIViewController? {
guard let viewController = get(scene: scene) else {
return nil
}
guard var presentingViewController = sender ?? sceneDelegate.window?.rootViewController?.topMost else {
return nil
}
// adapt for child controller
if let navigationControllerVisibleViewController = presentingViewController.navigationController?.visibleViewController {
switch viewController {
case is ProfileViewController:
let title: String = {
let title = navigationControllerVisibleViewController.navigationItem.title ?? ""
return title.count > 10 ? "" : title
}()
let barButtonItem = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
barButtonItem.tintColor = .white
navigationControllerVisibleViewController.navigationItem.backBarButtonItem = barButtonItem
default:
navigationControllerVisibleViewController.navigationItem.backBarButtonItem = nil
}
}
2021-01-27 07:50:13 +01:00
if let mainTabBarController = presentingViewController as? MainTabBarController,
let navigationController = mainTabBarController.selectedViewController as? UINavigationController,
let topViewController = navigationController.topViewController {
presentingViewController = topViewController
}
switch transition {
case .show:
presentingViewController.show(viewController, sender: sender)
case .showDetail:
2021-04-01 08:39:15 +02:00
let navigationController = AdaptiveStatusBarStyleNavigationController(rootViewController: viewController)
2021-01-27 07:50:13 +01:00
presentingViewController.showDetailViewController(navigationController, sender: sender)
case .modal(let animated, let completion):
2021-02-26 11:27:47 +01:00
let modalNavigationController: UINavigationController = {
if scene.isOnboarding {
2021-04-01 08:39:15 +02:00
return AdaptiveStatusBarStyleNavigationController(rootViewController: viewController)
2021-02-26 11:27:47 +01:00
} else {
return UINavigationController(rootViewController: viewController)
}
}()
2021-04-01 08:39:15 +02:00
modalNavigationController.modalPresentationCapturesStatusBarAppearance = true
2021-01-27 07:50:13 +01:00
if let adaptivePresentationControllerDelegate = viewController as? UIAdaptivePresentationControllerDelegate {
modalNavigationController.presentationController?.delegate = adaptivePresentationControllerDelegate
}
presentingViewController.present(modalNavigationController, animated: animated, completion: completion)
case .custom(let transitioningDelegate):
viewController.modalPresentationStyle = .custom
viewController.transitioningDelegate = transitioningDelegate
sender?.present(viewController, animated: true, completion: nil)
case .customPush:
// set delegate in view controller
assert(sender?.navigationController?.delegate != nil)
sender?.navigationController?.pushViewController(viewController, animated: true)
case .safariPresent(let animated, let completion):
if UserDefaults.shared.preferredUsingDefaultBrowser, case let .safari(url) = scene {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
viewController.modalPresentationCapturesStatusBarAppearance = true
presentingViewController.present(viewController, animated: animated, completion: completion)
}
2021-01-27 07:50:13 +01:00
case .alertController(let animated, let completion):
2021-04-01 08:39:15 +02:00
viewController.modalPresentationCapturesStatusBarAppearance = true
2021-01-27 07:50:13 +01:00
presentingViewController.present(viewController, animated: animated, completion: completion)
case .activityViewControllerPresent(let animated, let completion):
2021-04-01 08:39:15 +02:00
viewController.modalPresentationCapturesStatusBarAppearance = true
2021-01-27 07:50:13 +01:00
presentingViewController.present(viewController, animated: animated, completion: completion)
}
return viewController
}
2021-04-21 08:46:31 +02:00
func switchToTabBar(tab: MainTabBarController.Tab) {
tabBarController.selectedIndex = tab.rawValue
}
2021-01-27 07:50:13 +01:00
}
private extension SceneCoordinator {
func get(scene: Scene) -> UIViewController? {
let viewController: UIViewController?
2021-02-02 08:38:54 +01:00
switch scene {
2021-02-20 06:56:24 +01:00
case .welcome:
let _viewController = WelcomeViewController()
viewController = _viewController
2021-02-26 11:27:47 +01:00
case .mastodonPickServer(let viewModel):
let _viewController = MastodonPickServerViewController()
2021-02-02 08:38:54 +01:00
_viewController.viewModel = viewModel
viewController = _viewController
2021-02-05 10:53:00 +01:00
case .mastodonRegister(let viewModel):
let _viewController = MastodonRegisterViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-02-22 09:20:44 +01:00
case .mastodonServerRules(let viewModel):
let _viewController = MastodonServerRulesViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-02-23 08:38:05 +01:00
case .mastodonConfirmEmail(let viewModel):
let _viewController = MastodonConfirmEmailViewController()
_viewController.viewModel = viewModel
viewController = _viewController
case .mastodonResendEmail(let viewModel):
let _viewController = MastodonResendEmailViewController()
_viewController.viewModel = viewModel
viewController = _viewController
case .mastodonWebView(let viewModel):
let _viewController = WebViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-06-22 07:41:40 +02:00
#if ASDK
2021-06-21 14:27:43 +02:00
case .asyncHome:
let _viewController = AsyncHomeTimelineViewController()
viewController = _viewController
2021-06-22 07:41:40 +02:00
#endif
case .searchDetail(let viewModel):
let _viewController = SearchDetailViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-03-11 08:41:27 +01:00
case .compose(let viewModel):
let _viewController = ComposeViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-04-13 13:46:42 +02:00
case .thread(let viewModel):
let _viewController = ThreadViewController()
_viewController.viewModel = viewModel
viewController = _viewController
case .hashtagTimeline(let viewModel):
let _viewController = HashtagTimelineViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-04-01 08:39:15 +02:00
case .profile(let viewModel):
let _viewController = ProfileViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-04-07 08:24:28 +02:00
case .favorite(let viewModel):
let _viewController = FavoriteViewController()
_viewController.viewModel = viewModel
viewController = _viewController
2021-04-21 08:46:31 +02:00
case .suggestionAccount(let viewModel):
let _viewController = SuggestionAccountViewController()
_viewController.viewModel = viewModel
viewController = _viewController
case .mediaPreview(let viewModel):
let _viewController = MediaPreviewViewController()
_viewController.viewModel = viewModel
2021-04-21 08:46:31 +02:00
viewController = _viewController
case .safari(let url):
guard let scheme = url.scheme?.lowercased(),
scheme == "http" || scheme == "https" else {
return nil
}
let _viewController = SFSafariViewController(url: url)
2021-07-05 10:07:17 +02:00
_viewController.preferredBarTintColor = ThemeService.shared.currentTheme.value.navigationBarBackgroundColor
_viewController.preferredControlTintColor = Asset.Colors.brandBlue.color
viewController = _viewController
2021-02-03 09:01:08 +01:00
case .alertController(let alertController):
if let popoverPresentationController = alertController.popoverPresentationController {
assert(
popoverPresentationController.sourceView != nil ||
popoverPresentationController.sourceRect != .zero ||
popoverPresentationController.barButtonItem != nil
)
}
viewController = alertController
case .activityViewController(let activityViewController, let sourceView, let barButtonItem):
activityViewController.popoverPresentationController?.sourceView = sourceView
activityViewController.popoverPresentationController?.barButtonItem = barButtonItem
viewController = activityViewController
case .settings(let viewModel):
2021-04-19 14:34:08 +02:00
let _viewController = SettingsViewController()
_viewController.viewModel = viewModel
2021-04-19 14:34:08 +02:00
viewController = _viewController
case .report(let viewModel):
2021-04-19 14:34:08 +02:00
let _viewController = ReportViewController()
_viewController.viewModel = viewModel
2021-04-19 14:34:08 +02:00
viewController = _viewController
2021-02-24 11:07:11 +01:00
#if DEBUG
case .publicTimeline:
let _viewController = PublicTimelineViewController()
_viewController.viewModel = PublicTimelineViewModel(context: appContext)
viewController = _viewController
#endif
2021-02-02 08:38:54 +01:00
}
2021-01-27 07:50:13 +01:00
setupDependency(for: viewController as? NeedsDependency)
return viewController
}
private func setupDependency(for needs: NeedsDependency?) {
needs?.context = appContext
needs?.coordinator = self
}
}