2023-06-22 14:29:47 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import UIKit
|
2023-06-27 19:04:41 +02:00
|
|
|
import AuthenticationServices
|
2023-06-28 09:02:14 +02:00
|
|
|
import MastodonCore
|
2023-06-29 16:22:59 +02:00
|
|
|
import CoreDataStack
|
2023-06-22 14:29:47 +02:00
|
|
|
|
2023-06-27 11:46:28 +02:00
|
|
|
protocol SettingsCoordinatorDelegate: AnyObject {
|
|
|
|
func logout(_ settingsCoordinator: SettingsCoordinator)
|
2023-06-27 19:04:41 +02:00
|
|
|
func openGithubURL(_ settingsCoordinator: SettingsCoordinator)
|
|
|
|
func openPrivacyURL(_ settingsCoordinator: SettingsCoordinator)
|
|
|
|
func openProfileSettingsURL(_ settingsCoordinator: SettingsCoordinator)
|
2023-06-27 11:46:28 +02:00
|
|
|
}
|
|
|
|
|
2023-06-27 19:04:41 +02:00
|
|
|
class SettingsCoordinator: NSObject, Coordinator {
|
2023-06-22 14:29:47 +02:00
|
|
|
|
|
|
|
let navigationController: UINavigationController
|
|
|
|
let presentedOn: UIViewController
|
|
|
|
|
2023-06-27 11:46:28 +02:00
|
|
|
weak var delegate: SettingsCoordinatorDelegate?
|
2023-06-23 16:51:42 +02:00
|
|
|
private let settingsViewController: SettingsViewController
|
2023-06-22 14:29:47 +02:00
|
|
|
|
2023-06-29 16:22:59 +02:00
|
|
|
let setting: Setting
|
|
|
|
|
|
|
|
init(presentedOn: UIViewController, accountName: String, setting: Setting) {
|
2023-06-22 14:29:47 +02:00
|
|
|
self.presentedOn = presentedOn
|
|
|
|
navigationController = UINavigationController()
|
2023-06-29 16:22:59 +02:00
|
|
|
self.setting = setting
|
2023-06-22 14:29:47 +02:00
|
|
|
|
2023-06-27 19:04:41 +02:00
|
|
|
settingsViewController = SettingsViewController(accountName: accountName)
|
2023-06-22 14:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
2023-06-23 16:51:42 +02:00
|
|
|
settingsViewController.delegate = self
|
2023-06-22 14:29:47 +02:00
|
|
|
|
2023-06-23 16:51:42 +02:00
|
|
|
navigationController.pushViewController(settingsViewController, animated: false)
|
2023-06-22 14:29:47 +02:00
|
|
|
presentedOn.present(navigationController, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-28 18:47:24 +02:00
|
|
|
//MARK: - SettingsViewControllerDelegate
|
2023-06-23 16:51:42 +02:00
|
|
|
extension SettingsCoordinator: SettingsViewControllerDelegate {
|
|
|
|
func done(_ viewController: UIViewController) {
|
|
|
|
viewController.dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func didSelect(_ viewController: UIViewController, entry: SettingsEntry) {
|
|
|
|
switch entry {
|
|
|
|
case .general:
|
2023-06-29 16:22:59 +02:00
|
|
|
let generalSettingsViewController = GeneralSettingsViewController(setting: setting)
|
2023-06-28 18:47:24 +02:00
|
|
|
generalSettingsViewController.delegate = self
|
|
|
|
|
|
|
|
navigationController.pushViewController(generalSettingsViewController, animated: true)
|
2023-06-23 16:51:42 +02:00
|
|
|
case .notifications:
|
|
|
|
break
|
|
|
|
// show notifications
|
|
|
|
case .aboutMastodon:
|
2023-06-27 19:04:41 +02:00
|
|
|
let aboutViewController = AboutViewController()
|
|
|
|
aboutViewController.delegate = self
|
|
|
|
|
|
|
|
navigationController.pushViewController(aboutViewController, animated: true)
|
2023-06-23 16:51:42 +02:00
|
|
|
case .supportMastodon:
|
|
|
|
break
|
|
|
|
// present support-screen
|
2023-06-27 11:46:28 +02:00
|
|
|
case .logout(_):
|
|
|
|
delegate?.logout(self)
|
2023-06-23 16:51:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 19:04:41 +02:00
|
|
|
}
|
|
|
|
|
2023-06-28 18:47:24 +02:00
|
|
|
//MARK: - AboutViewControllerDelegate
|
2023-06-27 19:04:41 +02:00
|
|
|
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-27 19:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 14:29:47 +02:00
|
|
|
|
2023-06-28 18:47:24 +02:00
|
|
|
//MARK: - ASWebAuthenticationPresentationContextProviding
|
2023-06-27 19:04:41 +02:00
|
|
|
extension SettingsCoordinator: ASWebAuthenticationPresentationContextProviding {
|
|
|
|
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
|
|
return navigationController.view.window!
|
|
|
|
}
|
2023-06-22 14:29:47 +02:00
|
|
|
}
|
2023-06-28 18:47:24 +02:00
|
|
|
|
|
|
|
//MARK: - GeneralSettingsViewControllerDelegate
|
|
|
|
extension SettingsCoordinator: GeneralSettingsViewControllerDelegate {
|
2023-06-29 16:22:59 +02:00
|
|
|
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
|
|
|
}
|