Impressia/Vernissage/CoreData/ApplicationSettingsHandler....

66 lines
2.2 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() { }
2022-12-31 16:31:05 +01:00
func getDefaultSettings() -> ApplicationSettings {
var settingsList: [ApplicationSettings] = []
let context = CoreDataHandler.shared.container.viewContext
let fetchRequest = ApplicationSettings.fetchRequest()
do {
settingsList = try context.fetch(fetchRequest)
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.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-02-23 08:09:02 +01:00
func setAccountAsDefault(accountData: AccountData?) {
let defaultSettings = self.getDefaultSettings()
2023-02-23 08:09:02 +01:00
defaultSettings.currentAccount = accountData?.id
CoreDataHandler.shared.save()
}
2022-12-31 16:31:05 +01:00
2023-01-12 18:34:48 +01:00
func setDefaultTintColor(tintColor: TintColor) {
let defaultSettings = self.getDefaultSettings()
defaultSettings.tintColor = Int32(tintColor.rawValue)
CoreDataHandler.shared.save()
}
2023-01-13 13:37:01 +01:00
func setDefaultTheme(theme: Theme) {
let defaultSettings = self.getDefaultSettings()
defaultSettings.theme = Int32(theme.rawValue)
CoreDataHandler.shared.save()
}
2023-01-12 18:34:48 +01:00
2023-01-24 12:22:53 +01:00
func setDefaultAvatarShape(avatarShape: AvatarShape) {
let defaultSettings = self.getDefaultSettings()
defaultSettings.avatarShape = Int32(avatarShape.rawValue)
CoreDataHandler.shared.save()
}
2022-12-31 16:31:05 +01:00
private func createApplicationSettingsEntity() -> ApplicationSettings {
let context = CoreDataHandler.shared.container.viewContext
return ApplicationSettings(context: context)
}
}