Gemini/Gemini-iOS/Preferences.swift

68 lines
2.0 KiB
Swift

//
// Preferences.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/27/20.
//
import UIKit
class Preferences: Codable, ObservableObject {
static let shared: Preferences = load()
private static var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private static var archiveURL = Preferences.documentsDirectory.appendingPathComponent("Preferences").appendingPathExtension("plist")
static func save() {
let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection)
}
static func load() -> Preferences {
let decoder = PropertyListDecoder()
if let data = try? Data(contentsOf: archiveURL),
let prefs = try? decoder.decode(Preferences.self, from: data) {
return prefs
}
return Preferences()
}
private init() {}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
theme = try container.decode(UIUserInterfaceStyle.self, forKey: .theme)
useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
useReaderMode = try container.decode(Bool.self, forKey: .useReaderMode)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(theme, forKey: .theme)
try container.encode(useInAppSafari, forKey: .useInAppSafari)
try container.encode(useReaderMode, forKey: .useReaderMode)
}
@Published var theme = UIUserInterfaceStyle.unspecified
@Published var useInAppSafari = false
@Published var useReaderMode = false
enum CodingKeys: String, CodingKey {
case theme
case useInAppSafari
case useReaderMode
}
}
extension UIUserInterfaceStyle: Codable {}