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
|
2023-03-29 09:35:01 +02:00
|
|
|
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() { }
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
func get(viewContext: NSManagedObjectContext? = nil) -> ApplicationSettings {
|
2022-12-31 16:31:05 +01:00
|
|
|
var settingsList: [ApplicationSettings] = []
|
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
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 {
|
2023-03-29 09:35:01 +02:00
|
|
|
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)
|
2023-03-29 09:35:01 +02:00
|
|
|
CoreDataHandler.shared.save(viewContext: context)
|
2022-12-31 16:31:05 +01:00
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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
|
2023-04-17 08:06:49 +02:00
|
|
|
applicationState.showAltIconOnTimeline = defaultSettings.showAltIconOnTimeline
|
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
|
2023-01-11 14:31:31 +01:00
|
|
|
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-04-01 12:10:59 +02: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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02: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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-18 18:40:07 +01:00
|
|
|
func set(activeIcon: String) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.activeIcon = activeIcon
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
func set(menuPosition: MenuPosition) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.menuPosition = Int32(menuPosition.rawValue)
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
2023-04-10 11:23:00 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:06:49 +02:00
|
|
|
func set(showAltIconOnTimeline: Bool) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.showAltIconOnTimeline = showAltIconOnTimeline
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
2023-04-18 20:34:59 +02:00
|
|
|
func set(customNavigationMenuItem1: Int32) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.customNavigationMenuItem1 = customNavigationMenuItem1
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func set(customNavigationMenuItem2: Int32) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.customNavigationMenuItem2 = customNavigationMenuItem2
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func set(customNavigationMenuItem3: Int32) {
|
|
|
|
let defaultSettings = self.get()
|
|
|
|
defaultSettings.customNavigationMenuItem3 = customNavigationMenuItem3
|
|
|
|
CoreDataHandler.shared.save()
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:35:01 +02:00
|
|
|
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
|
|
|
}
|