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-09-26 15:36:57 +02:00
|
|
|
import MastodonSDK
|
|
|
|
import Combine
|
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
|
2023-09-25 18:18:27 +02:00
|
|
|
let appContext: AppContext
|
2023-09-26 15:36:57 +02:00
|
|
|
let authContext: AuthContext
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2023-06-29 16:22:59 +02:00
|
|
|
|
2023-09-26 15:36:57 +02:00
|
|
|
init(presentedOn: UIViewController, accountName: String, setting: Setting, appContext: AppContext, authContext: AuthContext) {
|
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-09-25 18:18:27 +02:00
|
|
|
self.appContext = appContext
|
2023-09-26 15:36:57 +02:00
|
|
|
self.authContext = authContext
|
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 {
|
2023-09-25 18:18:27 +02:00
|
|
|
case .general:
|
|
|
|
let generalSettingsViewController = GeneralSettingsViewController(setting: setting)
|
|
|
|
generalSettingsViewController.delegate = self
|
|
|
|
|
|
|
|
navigationController.pushViewController(generalSettingsViewController, animated: true)
|
|
|
|
case .notifications:
|
|
|
|
|
|
|
|
let currentSetting = appContext.settingService.currentSetting.value
|
2023-09-26 16:10:08 +02:00
|
|
|
let notificationsEnabled = appContext.notificationService.isNotificationPermissionGranted.value
|
|
|
|
let notificationViewController = NotificationSettingsViewController(currentSetting: currentSetting, notificationsEnabled: notificationsEnabled)
|
2023-09-25 18:18:27 +02:00
|
|
|
notificationViewController.delegate = self
|
|
|
|
|
|
|
|
self.navigationController.pushViewController(notificationViewController, animated: true)
|
|
|
|
|
|
|
|
case .aboutMastodon:
|
|
|
|
let aboutViewController = AboutViewController()
|
|
|
|
aboutViewController.delegate = self
|
|
|
|
|
|
|
|
navigationController.pushViewController(aboutViewController, animated: true)
|
|
|
|
case .supportMastodon:
|
|
|
|
break
|
|
|
|
// present support-screen
|
|
|
|
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
|
2023-06-30 21:14:44 +02:00
|
|
|
|
2023-06-29 16:22:59 +02:00
|
|
|
setting.update(preferredStaticEmoji: viewModel.playAnimations == false)
|
|
|
|
setting.update(preferredStaticAvatar: viewModel.playAnimations == false)
|
2023-06-30 21:14:44 +02:00
|
|
|
UserDefaults.shared.preferredStaticEmoji = viewModel.playAnimations == false
|
|
|
|
UserDefaults.shared.preferredStaticAvatar = viewModel.playAnimations == false
|
|
|
|
|
2023-06-29 16:22:59 +02:00
|
|
|
setting.update(preferredUsingDefaultBrowser: viewModel.selectedOpenLinks == .browser)
|
|
|
|
}
|
2023-06-28 18:47:24 +02:00
|
|
|
}
|
2023-06-30 11:45:58 +02:00
|
|
|
|
|
|
|
//MARK: - NotificationSettingsViewControllerDelegate
|
|
|
|
extension SettingsCoordinator: NotificationSettingsViewControllerDelegate {
|
|
|
|
func showPolicyList(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel) {
|
|
|
|
let policyListViewController = PolicySelectionViewController(viewModel: viewModel)
|
|
|
|
policyListViewController.delegate = self
|
|
|
|
|
|
|
|
navigationController.pushViewController(policyListViewController, animated: true)
|
|
|
|
}
|
2023-09-26 15:36:57 +02:00
|
|
|
|
|
|
|
func viewWillDisappear(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel) {
|
|
|
|
|
|
|
|
guard viewModel.updated else { return }
|
|
|
|
|
|
|
|
let authenticationBox = authContext.mastodonAuthenticationBox
|
|
|
|
guard let subscription = setting.activeSubscription,
|
|
|
|
setting.domain == authenticationBox.domain,
|
|
|
|
setting.userID == authenticationBox.userID,
|
|
|
|
let legacyViewModel = appContext.notificationService.dequeueNotificationViewModel(mastodonAuthenticationBox: authenticationBox), let deviceToken = appContext.notificationService.deviceToken.value else { return }
|
|
|
|
|
|
|
|
let queryData = Mastodon.API.Subscriptions.QueryData(
|
|
|
|
policy: viewModel.selectedPolicy.subscriptionPolicy,
|
|
|
|
alerts: Mastodon.API.Subscriptions.QueryData.Alerts(
|
|
|
|
favourite: viewModel.notifyFavorites,
|
|
|
|
follow: viewModel.notifyNewFollowers,
|
|
|
|
reblog: viewModel.notifyBoosts,
|
|
|
|
mention: viewModel.notifyMentions,
|
|
|
|
poll: subscription.alert.poll
|
|
|
|
)
|
|
|
|
)
|
|
|
|
let query = legacyViewModel.createSubscribeQuery(
|
|
|
|
deviceToken: deviceToken,
|
|
|
|
queryData: queryData,
|
|
|
|
mastodonAuthenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
|
|
|
|
appContext.apiService.createSubscription(
|
|
|
|
subscriptionObjectID: subscription.objectID,
|
|
|
|
query: query,
|
|
|
|
mastodonAuthenticationBox: authenticationBox
|
|
|
|
).sink(receiveCompletion: { completion in
|
|
|
|
print(completion)
|
|
|
|
}, receiveValue: { output in
|
|
|
|
print(output)
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
2023-06-30 11:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//MARK: - PolicySelectionViewControllerDelegate
|
|
|
|
extension SettingsCoordinator: PolicySelectionViewControllerDelegate {
|
2023-09-21 22:09:57 +02:00
|
|
|
func newPolicySelected(_ viewController: PolicySelectionViewController, newPolicy: NotificationPolicy) {
|
2023-09-26 15:36:57 +02:00
|
|
|
self.setting.activeSubscription?.policyRaw = newPolicy.subscriptionPolicy.rawValue
|
|
|
|
try? self.appContext.managedObjectContext.save()
|
2023-09-21 22:09:57 +02:00
|
|
|
}
|
2023-06-30 11:45:58 +02:00
|
|
|
}
|