diff --git a/Mastodon/Coordinator/SceneCoordinator.swift b/Mastodon/Coordinator/SceneCoordinator.swift index 2d9cb4352..590c1669c 100644 --- a/Mastodon/Coordinator/SceneCoordinator.swift +++ b/Mastodon/Coordinator/SceneCoordinator.swift @@ -183,7 +183,7 @@ extension SceneCoordinator { case followedTags(viewModel: FollowedTagsViewModel) // setting - case settings + case settings(setting: Setting) // report case report(viewModel: ReportViewModel) @@ -522,7 +522,7 @@ private extension SceneCoordinator { _viewController.preferredBarTintColor = SystemTheme.navigationBarBackgroundColor _viewController.preferredControlTintColor = Asset.Colors.Brand.blurple.color viewController = _viewController - + case .alertController(let alertController): if let popoverPresentationController = alertController.popoverPresentationController { assert( @@ -536,18 +536,20 @@ private extension SceneCoordinator { activityViewController.popoverPresentationController?.sourceView = sourceView activityViewController.popoverPresentationController?.barButtonItem = barButtonItem viewController = activityViewController - case .settings: + case .settings(let setting): guard let presentedOn = sender, let accountName = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: appContext.managedObjectContext)?.username else { return nil } - - let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn, accountName: accountName) + + let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn, + accountName: accountName, + setting: setting) settingsCoordinator.delegate = self settingsCoordinator.start() - + viewController = settingsCoordinator.navigationController childCoordinator = settingsCoordinator - + case .editStatus(let viewModel): let composeViewController = ComposeViewController(viewModel: viewModel) viewController = composeViewController diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift index f4c78b1bd..2ed6b26ad 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift @@ -372,7 +372,7 @@ extension HomeTimelineViewController { @objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) { guard let setting = context.settingService.currentSetting.value else { return } - _ = coordinator.present(scene: .settings, from: self, transition: .none) + _ = coordinator.present(scene: .settings(setting: setting), from: self, transition: .none) } @objc private func refreshControlValueChanged(_ sender: RefreshControl) { diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index f2ddb2c8b..a343983f8 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -518,7 +518,7 @@ extension ProfileViewController { @objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) { guard let setting = context.settingService.currentSetting.value else { return } - _ = coordinator.present(scene: .settings, from: self, transition: .none) + _ = coordinator.present(scene: .settings(setting: setting), from: self, transition: .none) } @objc private func shareBarButtonItemPressed(_ sender: UIBarButtonItem) { diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index 65fa836a5..c74d05a4e 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -652,11 +652,9 @@ extension MainTabBarController { } @objc private func openSettingsKeyCommandHandler(_ sender: UIKeyCommand) { + guard let setting = context.settingService.currentSetting.value else { return } - guard let authContext, - let setting = context.settingService.currentSetting.value else { return } - - _ = coordinator.present(scene: .settings, from: nil, transition: .none) + _ = coordinator.present(scene: .settings(setting: setting), from: self, transition: .none) } @objc private func composeNewPostKeyCommandHandler(_ sender: UIKeyCommand) { diff --git a/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift b/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift index 716a40562..ac6342f83 100644 --- a/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift +++ b/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift @@ -191,10 +191,9 @@ extension SidebarViewController: UICollectionViewDelegate { case .tab(let tab): delegate?.sidebarViewController(self, didSelectTab: tab) case .setting: - guard let authContext = viewModel.authContext else { return } guard let setting = context.settingService.currentSetting.value else { return } - _ = coordinator.present(scene: .settings, from: self, transition: .none) + _ = coordinator.present(scene: .settings(setting: setting), from: self, transition: .none) case .compose: assertionFailure() } diff --git a/Mastodon/Scene/Settings/General Settings/GeneralSettingsViewController.swift b/Mastodon/Scene/Settings/General Settings/GeneralSettingsViewController.swift index f0ecb817c..944911d06 100644 --- a/Mastodon/Scene/Settings/General Settings/GeneralSettingsViewController.swift +++ b/Mastodon/Scene/Settings/General Settings/GeneralSettingsViewController.swift @@ -1,6 +1,8 @@ // Copyright © 2023 Mastodon gGmbH. All rights reserved. import UIKit +import MastodonSDK +import CoreDataStack struct GeneralSettingsViewModel { var selectedAppearence: GeneralSetting.Appearance @@ -9,7 +11,7 @@ struct GeneralSettingsViewModel { } protocol GeneralSettingsViewControllerDelegate: AnyObject { - + func save(_ viewController: UIViewController, setting: Setting, viewModel: GeneralSettingsViewModel) } class GeneralSettingsViewController: UIViewController { @@ -18,11 +20,13 @@ class GeneralSettingsViewController: UIViewController { let tableView: UITableView var tableViewDataSource: GeneralSettingsDiffableTableViewDataSource? + private(set) var viewModel: GeneralSettingsViewModel + let setting: Setting let sections: [GeneralSettingsSection] - init() { + init(setting: Setting) { tableView = UITableView(frame: .zero, style: .insetGrouped) tableView.translatesAutoresizingMaskIntoConstraints = false tableView.register(GeneralSettingSelectionCell.self, forCellReuseIdentifier: GeneralSettingSelectionCell.reuseIdentifier) @@ -43,8 +47,20 @@ class GeneralSettingsViewController: UIViewController { ]) ] - //FIXME: Get Values from Setting - viewModel = GeneralSettingsViewModel(selectedAppearence: .dark, playAnimations: true, selectedOpenLinks: .browser) + let openLinksIn: GeneralSetting.OpenLinksIn + if setting.preferredUsingDefaultBrowser { + openLinksIn = .browser + } else { + openLinksIn = .mastodon + } + let playAnimations = (setting.preferredStaticAvatar == false && setting.preferredStaticEmoji == false) + viewModel = GeneralSettingsViewModel( + selectedAppearence: GeneralSetting.Appearance(rawValue: UserDefaults.shared.customUserInterfaceStyle.rawValue) ?? .system, + playAnimations: playAnimations, + selectedOpenLinks: openLinksIn + ) + + self.setting = setting super.init(nibName: nil, bundle: nil) @@ -111,7 +127,6 @@ extension GeneralSettingsViewController: UITableViewDelegate { switch section { case .appearance(let appearanceOption): viewModel.selectedAppearence = appearanceOption - UserDefaults.shared.customUserInterfaceStyle = appearanceOption.interfaceStyle case .design(_): break @@ -119,13 +134,12 @@ extension GeneralSettingsViewController: UITableViewDelegate { viewModel.selectedOpenLinks = openLinksInOption } - //TODO: @zeitschlag Store in Settings???? - if let snapshot = tableViewDataSource?.snapshot() { tableViewDataSource?.applySnapshotUsingReloadData(snapshot) } tableView.deselectRow(at: indexPath, animated: true) + delegate?.save(self, setting: setting, viewModel: viewModel) } } @@ -141,11 +155,9 @@ extension GeneralSettingsViewController: GeneralSettingToggleCellDelegate { } } - //TODO: @zeitschlag Store in Settings???? - if let snapshot = tableViewDataSource?.snapshot() { tableViewDataSource?.applySnapshotUsingReloadData(snapshot) } - + delegate?.save(self, setting: self.setting, viewModel: viewModel) } } diff --git a/Mastodon/Scene/Settings/SettingsCoordinator.swift b/Mastodon/Scene/Settings/SettingsCoordinator.swift index 3ef087b16..7fe9539d5 100644 --- a/Mastodon/Scene/Settings/SettingsCoordinator.swift +++ b/Mastodon/Scene/Settings/SettingsCoordinator.swift @@ -3,6 +3,7 @@ import UIKit import AuthenticationServices import MastodonCore +import CoreDataStack protocol SettingsCoordinatorDelegate: AnyObject { func logout(_ settingsCoordinator: SettingsCoordinator) @@ -17,12 +18,14 @@ class SettingsCoordinator: NSObject, Coordinator { let presentedOn: UIViewController weak var delegate: SettingsCoordinatorDelegate? - private let settingsViewController: SettingsViewController - init(presentedOn: UIViewController, accountName: String) { + let setting: Setting + + init(presentedOn: UIViewController, accountName: String, setting: Setting) { self.presentedOn = presentedOn navigationController = UINavigationController() + self.setting = setting settingsViewController = SettingsViewController(accountName: accountName) } @@ -44,7 +47,7 @@ extension SettingsCoordinator: SettingsViewControllerDelegate { func didSelect(_ viewController: UIViewController, entry: SettingsEntry) { switch entry { case .general: - let generalSettingsViewController = GeneralSettingsViewController() + let generalSettingsViewController = GeneralSettingsViewController(setting: setting) generalSettingsViewController.delegate = self navigationController.pushViewController(generalSettingsViewController, animated: true) @@ -101,5 +104,10 @@ extension SettingsCoordinator: ASWebAuthenticationPresentationContextProviding { //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) + } } diff --git a/MastodonSDK/Sources/CoreDataStack/Entity/App/Setting.swift b/MastodonSDK/Sources/CoreDataStack/Entity/App/Setting.swift index b25d01f92..3950bd2c6 100644 --- a/MastodonSDK/Sources/CoreDataStack/Entity/App/Setting.swift +++ b/MastodonSDK/Sources/CoreDataStack/Entity/App/Setting.swift @@ -14,6 +14,7 @@ public final class Setting: NSManagedObject { @NSManaged public var userID: String // @NSManaged public var appearanceRaw: String + @available(*, deprecated, message: "We need a new core data version, so we postpone this.") @NSManaged public var preferredTrueBlackDarkMode: Bool @NSManaged public var preferredStaticAvatar: Bool @NSManaged public var preferredStaticEmoji: Bool @@ -66,12 +67,6 @@ extension Setting { // didUpdate(at: Date()) // } - public func update(preferredTrueBlackDarkMode: Bool) { - guard preferredTrueBlackDarkMode != self.preferredTrueBlackDarkMode else { return } - self.preferredTrueBlackDarkMode = preferredTrueBlackDarkMode - didUpdate(at: Date()) - } - public func update(preferredStaticAvatar: Bool) { guard preferredStaticAvatar != self.preferredStaticAvatar else { return } self.preferredStaticAvatar = preferredStaticAvatar