Impressia/CoreData/ApplicationSettingsHandler....

108 lines
3.5 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.
// Licensed under the MIT License.
//
import Foundation
class ApplicationSettingsHandler {
2023-01-05 11:55:20 +01:00
public static let shared = ApplicationSettingsHandler()
private init() { }
2023-03-15 14:27:59 +01:00
func get() -> ApplicationSettings {
2022-12-31 16:31:05 +01:00
var settingsList: [ApplicationSettings] = []
let context = CoreDataHandler.shared.container.viewContext
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()
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)
2022-12-31 16:31:05 +01:00
CoreDataHandler.shared.save()
return settings
}
}
2023-03-15 14:27:59 +01:00
func set(accountData: AccountData?) {
let defaultSettings = self.get()
2023-02-23 08:09:02 +01:00
defaultSettings.currentAccount = accountData?.id
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-01-12 18:34:48 +01:00
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-08 17:43:05 +01:00
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-11 18:30:33 +01:00
private func createApplicationSettingsEntity() -> ApplicationSettings {
let context = CoreDataHandler.shared.container.viewContext
return ApplicationSettings(context: context)
}
2022-12-31 16:31:05 +01:00
}