Impressia/CoreData/ApplicationSettingsHandler....

198 lines
7.1 KiB
Swift
Raw Normal View History

2022-12-31 16:31:05 +01:00
//
// https://mczachurski.dev
2023-04-09 20:51:33 +02:00
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2022-12-31 16:31:05 +01:00
//
import Foundation
import CoreData
2023-04-07 16:59:18 +02:00
import EnvironmentKit
2022-12-31 16:31:05 +01:00
class ApplicationSettingsHandler {
2023-01-05 11:55:20 +01:00
public static let shared = ApplicationSettingsHandler()
private init() { }
func get(viewContext: NSManagedObjectContext? = nil) -> ApplicationSettings {
2022-12-31 16:31:05 +01:00
var settingsList: [ApplicationSettings] = []
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2022-12-31 16:31:05 +01:00
let fetchRequest = ApplicationSettings.fetchRequest()
do {
settingsList = try context.fetch(fetchRequest)
} catch {
2023-03-11 18:30:33 +01:00
CoreDataError.shared.handle(error, message: "Error during fetching application settings.")
2022-12-31 16:31:05 +01:00
}
if let settings = settingsList.first {
return settings
} else {
let settings = self.createApplicationSettingsEntity(viewContext: context)
2023-01-26 15:10:47 +01:00
settings.avatarShape = Int32(AvatarShape.circle.rawValue)
settings.theme = Int32(Theme.system.rawValue)
settings.tintColor = Int32(TintColor.accentColor2.rawValue)
CoreDataHandler.shared.save(viewContext: context)
2022-12-31 16:31:05 +01:00
return settings
}
}
2023-04-07 18:58:15 +02:00
func update(applicationState: ApplicationState) {
let defaultSettings = ApplicationSettingsHandler.shared.get()
if let tintColor = TintColor(rawValue: Int(defaultSettings.tintColor)) {
applicationState.tintColor = tintColor
}
if let theme = Theme(rawValue: Int(defaultSettings.theme)) {
applicationState.theme = theme
}
if let avatarShape = AvatarShape(rawValue: Int(defaultSettings.avatarShape)) {
applicationState.avatarShape = avatarShape
}
applicationState.activeIcon = defaultSettings.activeIcon
applicationState.showSensitive = defaultSettings.showSensitive
applicationState.showPhotoDescription = defaultSettings.showPhotoDescription
2023-04-10 21:53:15 +02:00
applicationState.showAvatarsOnTimeline = defaultSettings.showAvatarsOnTimeline
2023-04-14 16:50:47 +02:00
applicationState.showFavouritesOnTimeline = defaultSettings.showFavouritesOnTimeline
applicationState.showAltIconOnTimeline = defaultSettings.showAltIconOnTimeline
applicationState.warnAboutMissingAlt = defaultSettings.warnAboutMissingAlt
2023-04-07 18:58:15 +02:00
if let menuPosition = MenuPosition(rawValue: Int(defaultSettings.menuPosition)) {
applicationState.menuPosition = menuPosition
}
applicationState.hapticTabSelectionEnabled = defaultSettings.hapticTabSelectionEnabled
applicationState.hapticRefreshEnabled = defaultSettings.hapticRefreshEnabled
applicationState.hapticButtonPressEnabled = defaultSettings.hapticButtonPressEnabled
applicationState.hapticAnimationEnabled = defaultSettings.hapticAnimationEnabled
applicationState.hapticNotificationEnabled = defaultSettings.hapticNotificationEnabled
}
2023-03-29 17:06:41 +02:00
func set(accountId: String?) {
2023-03-15 14:27:59 +01:00
let defaultSettings = self.get()
2023-03-29 17:06:41 +02:00
defaultSettings.currentAccount = accountId
CoreDataHandler.shared.save()
}
2022-12-31 16:31:05 +01:00
2023-03-15 14:27:59 +01:00
func set(tintColor: TintColor) {
let defaultSettings = self.get()
2023-01-12 18:34:48 +01:00
defaultSettings.tintColor = Int32(tintColor.rawValue)
CoreDataHandler.shared.save()
}
2023-01-13 13:37:01 +01:00
2023-03-15 14:27:59 +01:00
func set(theme: Theme) {
let defaultSettings = self.get()
2023-01-13 13:37:01 +01:00
defaultSettings.theme = Int32(theme.rawValue)
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(avatarShape: AvatarShape) {
let defaultSettings = self.get()
2023-01-24 12:22:53 +01:00
defaultSettings.avatarShape = Int32(avatarShape.rawValue)
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(hapticTabSelectionEnabled: Bool) {
let defaultSettings = self.get()
defaultSettings.hapticTabSelectionEnabled = hapticTabSelectionEnabled
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(hapticRefreshEnabled: Bool) {
let defaultSettings = self.get()
defaultSettings.hapticRefreshEnabled = hapticRefreshEnabled
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(hapticAnimationEnabled: Bool) {
let defaultSettings = self.get()
defaultSettings.hapticAnimationEnabled = hapticAnimationEnabled
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(hapticNotificationEnabled: Bool) {
let defaultSettings = self.get()
defaultSettings.hapticNotificationEnabled = hapticNotificationEnabled
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(hapticButtonPressEnabled: Bool) {
let defaultSettings = self.get()
defaultSettings.hapticButtonPressEnabled = hapticButtonPressEnabled
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(showSensitive: Bool) {
let defaultSettings = self.get()
defaultSettings.showSensitive = showSensitive
2023-03-05 09:53:06 +01:00
CoreDataHandler.shared.save()
}
2023-03-15 14:27:59 +01:00
func set(showPhotoDescription: Bool) {
let defaultSettings = self.get()
defaultSettings.showPhotoDescription = showPhotoDescription
2023-03-08 17:43:05 +01:00
CoreDataHandler.shared.save()
}
2023-03-18 18:40:07 +01:00
func set(activeIcon: String) {
let defaultSettings = self.get()
defaultSettings.activeIcon = activeIcon
CoreDataHandler.shared.save()
}
2023-04-06 13:19:55 +02:00
func set(menuPosition: MenuPosition) {
let defaultSettings = self.get()
defaultSettings.menuPosition = Int32(menuPosition.rawValue)
CoreDataHandler.shared.save()
}
func set(showAvatarsOnTimeline: Bool) {
let defaultSettings = self.get()
defaultSettings.showAvatarsOnTimeline = showAvatarsOnTimeline
CoreDataHandler.shared.save()
}
2023-04-14 16:50:47 +02:00
func set(showFavouritesOnTimeline: Bool) {
let defaultSettings = self.get()
defaultSettings.showFavouritesOnTimeline = showFavouritesOnTimeline
CoreDataHandler.shared.save()
}
func set(showAltIconOnTimeline: Bool) {
let defaultSettings = self.get()
defaultSettings.showAltIconOnTimeline = showAltIconOnTimeline
CoreDataHandler.shared.save()
}
func set(warnAboutMissingAlt: Bool) {
let defaultSettings = self.get()
defaultSettings.warnAboutMissingAlt = warnAboutMissingAlt
CoreDataHandler.shared.save()
}
2023-04-21 16:58:52 +02:00
func set(customNavigationMenuItem1: Int) {
2023-04-18 20:34:59 +02:00
let defaultSettings = self.get()
2023-04-21 16:58:52 +02:00
defaultSettings.customNavigationMenuItem1 = Int32(customNavigationMenuItem1)
2023-04-18 20:34:59 +02:00
CoreDataHandler.shared.save()
}
2023-04-21 16:58:52 +02:00
func set(customNavigationMenuItem2: Int) {
2023-04-18 20:34:59 +02:00
let defaultSettings = self.get()
2023-04-21 16:58:52 +02:00
defaultSettings.customNavigationMenuItem2 = Int32(customNavigationMenuItem2)
2023-04-18 20:34:59 +02:00
CoreDataHandler.shared.save()
}
2023-04-21 16:58:52 +02:00
func set(customNavigationMenuItem3: Int) {
2023-04-18 20:34:59 +02:00
let defaultSettings = self.get()
2023-04-21 16:58:52 +02:00
defaultSettings.customNavigationMenuItem3 = Int32(customNavigationMenuItem3)
2023-04-18 20:34:59 +02:00
CoreDataHandler.shared.save()
}
private func createApplicationSettingsEntity(viewContext: NSManagedObjectContext? = nil) -> ApplicationSettings {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-03-11 18:30:33 +01:00
return ApplicationSettings(context: context)
}
2022-12-31 16:31:05 +01:00
}