Impressia/CoreData/ApplicationSettingsHandler....

115 lines
3.7 KiB
Swift
Raw Normal View History

2022-12-31 16:31:05 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 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
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-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()
}
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
}