Gemini/Gemini-iOS/Preferences.swift

100 lines
3.2 KiB
Swift
Raw Normal View History

2020-09-28 02:26:44 +00:00
//
// Preferences.swift
// Gemini-iOS
//
// Created by Shadowfacts on 9/27/20.
//
2020-09-30 03:44:44 +00:00
import UIKit
2020-09-28 02:26:44 +00:00
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)
2021-06-16 03:21:22 +00:00
if let stored = try container.decodeIfPresent(URL.self, forKey: .homepage) {
homepage = stored
}
2020-09-30 03:44:44 +00:00
theme = try container.decode(UIUserInterfaceStyle.self, forKey: .theme)
2021-06-16 02:42:16 +00:00
if let stored = try container.decodeIfPresent(Bool.self, forKey: .showLinkIcons) {
showLinkIcons = stored
}
2020-09-30 03:44:44 +00:00
2020-09-28 02:26:44 +00:00
useInAppSafari = try container.decode(Bool.self, forKey: .useInAppSafari)
useReaderMode = try container.decode(Bool.self, forKey: .useReaderMode)
2021-10-02 15:12:20 +00:00
if let stored = try container.decodeIfPresent(Bool.self, forKey: .hideToolbarsWhenScrolling) {
hideToolbarsWhenScrolling = stored
}
2021-10-02 15:18:38 +00:00
if let stored = try container.decodeIfPresent(Bool.self, forKey: .pullToRefreshEnabled) {
pullToRefreshEnabled = stored
}
2020-09-28 02:26:44 +00:00
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
2021-06-16 03:21:22 +00:00
try container.encode(homepage, forKey: .homepage)
2020-09-30 03:44:44 +00:00
try container.encode(theme, forKey: .theme)
2021-06-16 02:42:16 +00:00
try container.encode(showLinkIcons, forKey: .showLinkIcons)
2020-09-30 03:44:44 +00:00
2020-09-28 02:26:44 +00:00
try container.encode(useInAppSafari, forKey: .useInAppSafari)
try container.encode(useReaderMode, forKey: .useReaderMode)
2021-10-02 15:12:20 +00:00
try container.encode(hideToolbarsWhenScrolling, forKey: .hideToolbarsWhenScrolling)
2021-10-02 15:18:38 +00:00
try container.encode(pullToRefreshEnabled, forKey: .pullToRefreshEnabled)
2020-09-28 02:26:44 +00:00
}
2021-06-16 03:21:22 +00:00
@Published var homepage = AppDelegate.defaultHomepage
2020-09-30 03:44:44 +00:00
@Published var theme = UIUserInterfaceStyle.unspecified
2021-06-16 02:42:16 +00:00
@Published var showLinkIcons = true
2020-09-30 03:44:44 +00:00
2020-09-28 02:26:44 +00:00
@Published var useInAppSafari = false
@Published var useReaderMode = false
2021-10-02 15:12:20 +00:00
@Published var hideToolbarsWhenScrolling = true
2021-10-02 15:18:38 +00:00
@Published var pullToRefreshEnabled = true
2021-10-02 15:12:20 +00:00
2020-09-28 02:26:44 +00:00
enum CodingKeys: String, CodingKey {
2021-06-16 03:21:22 +00:00
case homepage
2020-09-30 03:44:44 +00:00
case theme
2021-06-16 02:42:16 +00:00
case showLinkIcons
2020-09-30 03:44:44 +00:00
2020-09-28 02:26:44 +00:00
case useInAppSafari
case useReaderMode
2021-10-02 15:12:20 +00:00
case hideToolbarsWhenScrolling
2021-10-02 15:18:38 +00:00
case pullToRefreshEnabled
2020-09-28 02:26:44 +00:00
}
}
2020-09-30 03:44:44 +00:00
extension UIUserInterfaceStyle: Codable {}