70 lines
1.9 KiB
Swift
70 lines
1.9 KiB
Swift
//
|
|
// Preferences.swift
|
|
// Reader
|
|
//
|
|
// Created by Shadowfacts on 1/16/22.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class Preferences: Codable, ObservableObject {
|
|
|
|
private static let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
|
private static let archiveURL = Preferences.documentsDirectory.appendingPathComponent("preferences").appendingPathExtension("plist")
|
|
private static let decoder = PropertyListDecoder()
|
|
private static let encoder = PropertyListEncoder()
|
|
|
|
static var shared = load()
|
|
|
|
private static func load() -> Preferences {
|
|
if let data = try? Data(contentsOf: archiveURL),
|
|
let prefs = try? decoder.decode(Preferences.self, from: data) {
|
|
return prefs
|
|
} else {
|
|
return Preferences()
|
|
}
|
|
}
|
|
|
|
static func save() {
|
|
if let data = try? encoder.encode(shared) {
|
|
try? data.write(to: archiveURL, options: .noFileProtection)
|
|
}
|
|
}
|
|
|
|
init() {
|
|
}
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
self.appearance = try container.decode(Appearance.self, forKey: .appearance)
|
|
}
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
try container.encode(appearance, forKey: .appearance)
|
|
}
|
|
|
|
@Published var appearance = Appearance.unspecified {
|
|
didSet {
|
|
NotificationCenter.default.post(name: .appearanceChanged, object: nil, userInfo: [
|
|
"appearance": appearance.rawValue
|
|
])
|
|
}
|
|
}
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case appearance
|
|
}
|
|
|
|
}
|
|
|
|
enum Appearance: Int, Codable {
|
|
case unspecified = 0
|
|
case light = 1
|
|
case dark = 2
|
|
}
|
|
|
|
extension Notification.Name {
|
|
static let appearanceChanged = Notification.Name("appearanceChanged")
|
|
}
|