140 lines
3.4 KiB
Swift
140 lines
3.4 KiB
Swift
//
|
|
// PreferencesManager.swift
|
|
// FluxRead
|
|
//
|
|
// Created by RSA on 17/01/2026.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
final class PreferencesManager {
|
|
|
|
private let defaults = UserDefaults.standard
|
|
|
|
// private var cachePreferences: AppSettings?
|
|
|
|
// MARK: - Set Values
|
|
|
|
func set(_ value: Any?, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
}
|
|
|
|
func set(_ value: Bool, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
}
|
|
|
|
func set(_ value: Int, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
}
|
|
|
|
func set(_ value: Double, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
}
|
|
|
|
func set(_ value: String, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
}
|
|
|
|
func set(_ value: Date, forKey key: String) {
|
|
defaults.set(value, forKey: key)
|
|
defaults.synchronize()
|
|
}
|
|
|
|
func set<T: Encodable>(_ value: T, forKey key: String) throws {
|
|
let data = try JSONEncoder().encode(value)
|
|
defaults.set(data, forKey: key)
|
|
defaults.synchronize()
|
|
}
|
|
|
|
// MARK: - Get Values
|
|
|
|
func string(forKey key: String) -> String? {
|
|
return defaults.string(forKey: key)
|
|
}
|
|
|
|
func bool(forKey key: String) -> Bool {
|
|
return defaults.bool(forKey: key)
|
|
}
|
|
|
|
func int(forKey key: String) -> Int {
|
|
return defaults.integer(forKey: key)
|
|
}
|
|
|
|
func double(forKey key: String) -> Double {
|
|
return defaults.double(forKey: key)
|
|
}
|
|
|
|
func date(forKey key: String) -> Date? {
|
|
return defaults.object(forKey: key) as? Date
|
|
}
|
|
|
|
func data(forKey key: String) -> Data? {
|
|
return defaults.data(forKey: key)
|
|
}
|
|
|
|
func object<T: Decodable>(forKey key: String, as type: T.Type) throws -> T? {
|
|
guard let data = defaults.data(forKey: key) else { return nil }
|
|
return try JSONDecoder().decode(type, from: data)
|
|
}
|
|
|
|
// MARK: - Remove Values
|
|
|
|
func remove(forKey key: String) {
|
|
defaults.removeObject(forKey: key)
|
|
}
|
|
|
|
func removeAll() {
|
|
if let bundleID = Bundle.main.bundleIdentifier {
|
|
defaults.removePersistentDomain(forName: bundleID)
|
|
}
|
|
}
|
|
|
|
// MARK: - Check Existence
|
|
|
|
func exists(forKey key: String) -> Bool {
|
|
return defaults.object(forKey: key) != nil
|
|
}
|
|
/*
|
|
func getAppSettings() -> AppSettings {
|
|
do {
|
|
|
|
if let cacheSettings = self.cachePreferences {
|
|
|
|
return cacheSettings
|
|
}
|
|
|
|
if let appSettings = try self.object(forKey: "appSettings", as: AppSettings.self) {
|
|
self.cachePreferences = appSettings
|
|
return appSettings
|
|
}
|
|
} catch {
|
|
|
|
let defaultSettings = AppSettings()
|
|
|
|
self.saveAppSettings(defaultSettings)
|
|
return defaultSettings
|
|
|
|
print("❌ Failed to decode AppSettings: \(error.localizedDescription)")
|
|
}
|
|
|
|
// Return default settings if none exist or decoding fails
|
|
return AppSettings()
|
|
}
|
|
|
|
func saveAppSettings(_ settings: AppSettings) {
|
|
do {
|
|
try self.set(settings, forKey: "appSettings")
|
|
self.cachePreferences = settings
|
|
} catch {
|
|
print("❌ Failed to save AppSettings: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
// MARK: - Synchronize
|
|
*/
|
|
@discardableResult
|
|
func synchronize() -> Bool {
|
|
return defaults.synchronize()
|
|
}
|
|
}
|