frenzy-ios/Reader/Preferences.swift

70 lines
1.9 KiB
Swift
Raw Normal View History

2022-01-16 16:58:28 +00:00
//
// Preferences.swift
// Reader
//
// Created by Shadowfacts on 1/16/22.
//
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)
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)
}
@Published var appearance = Appearance.unspecified {
2022-01-16 16:58:28 +00:00
didSet {
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
}
}
enum Appearance: Int, Codable {
case unspecified = 0
case light = 1
case dark = 2
2022-01-16 16:58:28 +00:00
}
extension Notification.Name {
static let appearanceChanged = Notification.Name("appearanceChanged")
2022-01-16 16:58:28 +00:00
}