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)
// 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

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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