mastodon-ios/Mastodon/Scene/Settings/SettingsCoordinator.swift

114 lines
4.3 KiB
Swift
Raw Normal View History

// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import UIKit
import AuthenticationServices
2023-06-28 09:02:14 +02:00
import MastodonCore
import CoreDataStack
2023-06-27 11:46:28 +02:00
protocol SettingsCoordinatorDelegate: AnyObject {
func logout(_ settingsCoordinator: SettingsCoordinator)
func openGithubURL(_ settingsCoordinator: SettingsCoordinator)
func openPrivacyURL(_ settingsCoordinator: SettingsCoordinator)
func openProfileSettingsURL(_ settingsCoordinator: SettingsCoordinator)
2023-06-27 11:46:28 +02:00
}
class SettingsCoordinator: NSObject, Coordinator {
let navigationController: UINavigationController
let presentedOn: UIViewController
2023-06-27 11:46:28 +02:00
weak var delegate: SettingsCoordinatorDelegate?
private let settingsViewController: SettingsViewController
let setting: Setting
init(presentedOn: UIViewController, accountName: String, setting: Setting) {
self.presentedOn = presentedOn
navigationController = UINavigationController()
self.setting = setting
settingsViewController = SettingsViewController(accountName: accountName)
}
func start() {
settingsViewController.delegate = self
navigationController.pushViewController(settingsViewController, animated: false)
presentedOn.present(navigationController, animated: true)
}
}
2023-06-28 18:47:24 +02:00
//MARK: - SettingsViewControllerDelegate
extension SettingsCoordinator: SettingsViewControllerDelegate {
func done(_ viewController: UIViewController) {
viewController.dismiss(animated: true)
}
func didSelect(_ viewController: UIViewController, entry: SettingsEntry) {
switch entry {
case .general:
let generalSettingsViewController = GeneralSettingsViewController(setting: setting)
2023-06-28 18:47:24 +02:00
generalSettingsViewController.delegate = self
navigationController.pushViewController(generalSettingsViewController, animated: true)
case .notifications:
break
// show notifications
case .aboutMastodon:
let aboutViewController = AboutViewController()
aboutViewController.delegate = self
navigationController.pushViewController(aboutViewController, animated: true)
case .supportMastodon:
break
// present support-screen
2023-06-27 11:46:28 +02:00
case .logout(_):
delegate?.logout(self)
}
}
}
2023-06-28 18:47:24 +02:00
//MARK: - AboutViewControllerDelegate
extension SettingsCoordinator: AboutViewControllerDelegate {
func didSelect(_ viewController: AboutViewController, entry: AboutSettingsEntry) {
switch entry {
case .evenMoreSettings:
delegate?.openProfileSettingsURL(self)
case .contributeToMastodon:
delegate?.openGithubURL(self)
case .privacyPolicy:
delegate?.openPrivacyURL(self)
case .clearMediaCache(_):
//FIXME: maybe we should inject an AppContext/AuthContext here instead of delegating everything to SceneCoordinator?
2023-06-28 09:02:14 +02:00
AppContext.shared.purgeCache()
viewController.update(with:
[AboutSettingsSection(entries: [
.evenMoreSettings,
.contributeToMastodon,
.privacyPolicy
]),
AboutSettingsSection(entries: [
.clearMediaCache(AppContext.shared.currentDiskUsage())
])]
)
}
}
}
2023-06-28 18:47:24 +02:00
//MARK: - ASWebAuthenticationPresentationContextProviding
extension SettingsCoordinator: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return navigationController.view.window!
}
}
2023-06-28 18:47:24 +02:00
//MARK: - GeneralSettingsViewControllerDelegate
extension SettingsCoordinator: GeneralSettingsViewControllerDelegate {
func save(_ viewController: UIViewController, setting: Setting, viewModel: GeneralSettingsViewModel) {
UserDefaults.shared.customUserInterfaceStyle = viewModel.selectedAppearence.interfaceStyle
setting.update(preferredStaticEmoji: viewModel.playAnimations == false)
setting.update(preferredStaticAvatar: viewModel.playAnimations == false)
setting.update(preferredUsingDefaultBrowser: viewModel.selectedOpenLinks == .browser)
}
2023-06-28 18:47:24 +02:00
}