Persist general settings (IOS-14, IOS-166)

This commit is contained in:
Nathan Mattes 2023-06-29 16:22:59 +02:00
parent c14418c440
commit 97e6d750ee
8 changed files with 49 additions and 35 deletions

View File

@ -183,7 +183,7 @@ extension SceneCoordinator {
case followedTags(viewModel: FollowedTagsViewModel) case followedTags(viewModel: FollowedTagsViewModel)
// setting // setting
case settings case settings(setting: Setting)
// report // report
case report(viewModel: ReportViewModel) case report(viewModel: ReportViewModel)
@ -536,12 +536,14 @@ private extension SceneCoordinator {
activityViewController.popoverPresentationController?.sourceView = sourceView activityViewController.popoverPresentationController?.sourceView = sourceView
activityViewController.popoverPresentationController?.barButtonItem = barButtonItem activityViewController.popoverPresentationController?.barButtonItem = barButtonItem
viewController = activityViewController viewController = activityViewController
case .settings: case .settings(let setting):
guard let presentedOn = sender, guard let presentedOn = sender,
let accountName = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: appContext.managedObjectContext)?.username let accountName = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: appContext.managedObjectContext)?.username
else { return nil } else { return nil }
let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn, accountName: accountName) let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn,
accountName: accountName,
setting: setting)
settingsCoordinator.delegate = self settingsCoordinator.delegate = self
settingsCoordinator.start() settingsCoordinator.start()

View File

@ -372,7 +372,7 @@ extension HomeTimelineViewController {
@objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) { @objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) {
guard let setting = context.settingService.currentSetting.value 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)
} }
@objc private func refreshControlValueChanged(_ sender: RefreshControl) { @objc private func refreshControlValueChanged(_ sender: RefreshControl) {

View File

@ -518,7 +518,7 @@ extension ProfileViewController {
@objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) { @objc private func settingBarButtonItemPressed(_ sender: UIBarButtonItem) {
guard let setting = context.settingService.currentSetting.value 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)
} }
@objc private func shareBarButtonItemPressed(_ sender: UIBarButtonItem) { @objc private func shareBarButtonItemPressed(_ sender: UIBarButtonItem) {

View File

@ -652,11 +652,9 @@ extension MainTabBarController {
} }
@objc private func openSettingsKeyCommandHandler(_ sender: UIKeyCommand) { @objc private func openSettingsKeyCommandHandler(_ sender: UIKeyCommand) {
guard let setting = context.settingService.currentSetting.value else { return }
guard let authContext, _ = coordinator.present(scene: .settings(setting: setting), from: self, transition: .none)
let setting = context.settingService.currentSetting.value else { return }
_ = coordinator.present(scene: .settings, from: nil, transition: .none)
} }
@objc private func composeNewPostKeyCommandHandler(_ sender: UIKeyCommand) { @objc private func composeNewPostKeyCommandHandler(_ sender: UIKeyCommand) {

View File

@ -191,10 +191,9 @@ extension SidebarViewController: UICollectionViewDelegate {
case .tab(let tab): case .tab(let tab):
delegate?.sidebarViewController(self, didSelectTab: tab) delegate?.sidebarViewController(self, didSelectTab: tab)
case .setting: case .setting:
guard let authContext = viewModel.authContext else { return }
guard let setting = context.settingService.currentSetting.value 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: case .compose:
assertionFailure() assertionFailure()
} }

View File

@ -1,6 +1,8 @@
// Copyright © 2023 Mastodon gGmbH. All rights reserved. // Copyright © 2023 Mastodon gGmbH. All rights reserved.
import UIKit import UIKit
import MastodonSDK
import CoreDataStack
struct GeneralSettingsViewModel { struct GeneralSettingsViewModel {
var selectedAppearence: GeneralSetting.Appearance var selectedAppearence: GeneralSetting.Appearance
@ -9,7 +11,7 @@ struct GeneralSettingsViewModel {
} }
protocol GeneralSettingsViewControllerDelegate: AnyObject { protocol GeneralSettingsViewControllerDelegate: AnyObject {
func save(_ viewController: UIViewController, setting: Setting, viewModel: GeneralSettingsViewModel)
} }
class GeneralSettingsViewController: UIViewController { class GeneralSettingsViewController: UIViewController {
@ -18,11 +20,13 @@ class GeneralSettingsViewController: UIViewController {
let tableView: UITableView let tableView: UITableView
var tableViewDataSource: GeneralSettingsDiffableTableViewDataSource? var tableViewDataSource: GeneralSettingsDiffableTableViewDataSource?
private(set) var viewModel: GeneralSettingsViewModel private(set) var viewModel: GeneralSettingsViewModel
let setting: Setting
let sections: [GeneralSettingsSection] let sections: [GeneralSettingsSection]
init() { init(setting: Setting) {
tableView = UITableView(frame: .zero, style: .insetGrouped) tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(GeneralSettingSelectionCell.self, forCellReuseIdentifier: GeneralSettingSelectionCell.reuseIdentifier) tableView.register(GeneralSettingSelectionCell.self, forCellReuseIdentifier: GeneralSettingSelectionCell.reuseIdentifier)
@ -43,8 +47,20 @@ class GeneralSettingsViewController: UIViewController {
]) ])
] ]
//FIXME: Get Values from Setting let openLinksIn: GeneralSetting.OpenLinksIn
viewModel = GeneralSettingsViewModel(selectedAppearence: .dark, playAnimations: true, selectedOpenLinks: .browser) 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) super.init(nibName: nil, bundle: nil)
@ -111,7 +127,6 @@ extension GeneralSettingsViewController: UITableViewDelegate {
switch section { switch section {
case .appearance(let appearanceOption): case .appearance(let appearanceOption):
viewModel.selectedAppearence = appearanceOption viewModel.selectedAppearence = appearanceOption
UserDefaults.shared.customUserInterfaceStyle = appearanceOption.interfaceStyle
case .design(_): case .design(_):
break break
@ -119,13 +134,12 @@ extension GeneralSettingsViewController: UITableViewDelegate {
viewModel.selectedOpenLinks = openLinksInOption viewModel.selectedOpenLinks = openLinksInOption
} }
//TODO: @zeitschlag Store in Settings????
if let snapshot = tableViewDataSource?.snapshot() { if let snapshot = tableViewDataSource?.snapshot() {
tableViewDataSource?.applySnapshotUsingReloadData(snapshot) tableViewDataSource?.applySnapshotUsingReloadData(snapshot)
} }
tableView.deselectRow(at: indexPath, animated: true) 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() { if let snapshot = tableViewDataSource?.snapshot() {
tableViewDataSource?.applySnapshotUsingReloadData(snapshot) tableViewDataSource?.applySnapshotUsingReloadData(snapshot)
} }
delegate?.save(self, setting: self.setting, viewModel: viewModel)
} }
} }

View File

@ -3,6 +3,7 @@
import UIKit import UIKit
import AuthenticationServices import AuthenticationServices
import MastodonCore import MastodonCore
import CoreDataStack
protocol SettingsCoordinatorDelegate: AnyObject { protocol SettingsCoordinatorDelegate: AnyObject {
func logout(_ settingsCoordinator: SettingsCoordinator) func logout(_ settingsCoordinator: SettingsCoordinator)
@ -17,12 +18,14 @@ class SettingsCoordinator: NSObject, Coordinator {
let presentedOn: UIViewController let presentedOn: UIViewController
weak var delegate: SettingsCoordinatorDelegate? weak var delegate: SettingsCoordinatorDelegate?
private let settingsViewController: SettingsViewController private let settingsViewController: SettingsViewController
init(presentedOn: UIViewController, accountName: String) { let setting: Setting
init(presentedOn: UIViewController, accountName: String, setting: Setting) {
self.presentedOn = presentedOn self.presentedOn = presentedOn
navigationController = UINavigationController() navigationController = UINavigationController()
self.setting = setting
settingsViewController = SettingsViewController(accountName: accountName) settingsViewController = SettingsViewController(accountName: accountName)
} }
@ -44,7 +47,7 @@ extension SettingsCoordinator: SettingsViewControllerDelegate {
func didSelect(_ viewController: UIViewController, entry: SettingsEntry) { func didSelect(_ viewController: UIViewController, entry: SettingsEntry) {
switch entry { switch entry {
case .general: case .general:
let generalSettingsViewController = GeneralSettingsViewController() let generalSettingsViewController = GeneralSettingsViewController(setting: setting)
generalSettingsViewController.delegate = self generalSettingsViewController.delegate = self
navigationController.pushViewController(generalSettingsViewController, animated: true) navigationController.pushViewController(generalSettingsViewController, animated: true)
@ -101,5 +104,10 @@ extension SettingsCoordinator: ASWebAuthenticationPresentationContextProviding {
//MARK: - GeneralSettingsViewControllerDelegate //MARK: - GeneralSettingsViewControllerDelegate
extension SettingsCoordinator: 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)
}
} }

View File

@ -14,6 +14,7 @@ public final class Setting: NSManagedObject {
@NSManaged public var userID: String @NSManaged public var userID: String
// @NSManaged public var appearanceRaw: 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 preferredTrueBlackDarkMode: Bool
@NSManaged public var preferredStaticAvatar: Bool @NSManaged public var preferredStaticAvatar: Bool
@NSManaged public var preferredStaticEmoji: Bool @NSManaged public var preferredStaticEmoji: Bool
@ -66,12 +67,6 @@ extension Setting {
// didUpdate(at: Date()) // 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) { public func update(preferredStaticAvatar: Bool) {
guard preferredStaticAvatar != self.preferredStaticAvatar else { return } guard preferredStaticAvatar != self.preferredStaticAvatar else { return }
self.preferredStaticAvatar = preferredStaticAvatar self.preferredStaticAvatar = preferredStaticAvatar