2022-01-16 16:58:28 +00:00
|
|
|
//
|
|
|
|
// Preferences.swift
|
|
|
|
// Reader
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/16/22.
|
|
|
|
//
|
|
|
|
|
2022-01-16 17:32:45 +00:00
|
|
|
import Foundation
|
2022-01-16 16:58:28 +00:00
|
|
|
|
|
|
|
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)
|
2022-01-16 17:32:45 +00:00
|
|
|
self.appearance = try container.decode(Appearance.self, forKey: .appearance)
|
2022-01-16 16:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
try container.encode(appearance, forKey: .appearance)
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:32:45 +00:00
|
|
|
@Published var appearance = Appearance.unspecified {
|
2022-01-16 16:58:28 +00:00
|
|
|
didSet {
|
2022-01-16 17:32:45 +00:00
|
|
|
NotificationCenter.default.post(name: .appearanceChanged, object: nil, userInfo: [
|
|
|
|
"appearance": appearance.rawValue
|
|
|
|
])
|
2022-01-16 16:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case appearance
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:32:45 +00:00
|
|
|
enum Appearance: Int, Codable {
|
|
|
|
case unspecified = 0
|
|
|
|
case light = 1
|
|
|
|
case dark = 2
|
2022-01-16 16:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Notification.Name {
|
2022-01-16 17:32:45 +00:00
|
|
|
static let appearanceChanged = Notification.Name("appearanceChanged")
|
2022-01-16 16:58:28 +00:00
|
|
|
}
|