NetNewsWire/Multiplatform/Shared/AppDefaults.swift

354 lines
9.6 KiB
Swift
Raw Normal View History

//
// AppDefaults.swift
// NetNewsWire
//
2020-07-02 03:57:36 +02:00
// Created by Stuart Breckenridge on 1/7/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import Foundation
2020-07-02 03:57:36 +02:00
import SwiftUI
2020-07-02 03:57:36 +02:00
enum UserInterfaceColorPalette: Int, CustomStringConvertible, CaseIterable {
case automatic = 0
case light = 1
case dark = 2
2020-07-02 03:57:36 +02:00
var description: String {
switch self {
case .automatic:
return NSLocalizedString("Automatic", comment: "Automatic")
case .light:
return NSLocalizedString("Light", comment: "Light")
case .dark:
return NSLocalizedString("Dark", comment: "Dark")
}
}
}
2020-07-02 03:57:36 +02:00
enum FontSize: Int {
case small = 0
case medium = 1
case large = 2
case veryLarge = 3
}
2020-07-02 03:57:36 +02:00
final class AppDefaults: ObservableObject {
#if os(macOS)
2020-07-02 03:57:36 +02:00
static let store: UserDefaults = UserDefaults.standard
#endif
#if os(iOS)
2020-07-02 03:57:36 +02:00
static let store: UserDefaults = {
let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as! String
let suiteName = "\(appIdentifierPrefix)group.\(Bundle.main.bundleIdentifier!)"
return UserDefaults.init(suiteName: suiteName)!
}()
#endif
2020-07-02 03:57:36 +02:00
public static let shared = AppDefaults()
private init() {}
struct Key {
2020-07-02 03:57:36 +02:00
// Shared Defaults
static let refreshInterval = "refreshInterval"
static let hideDockUnreadCount = "JustinMillerHideDockUnreadCount"
static let activeExtensionPointIDs = "activeExtensionPointIDs"
static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
static let firstRunDate = "firstRunDate"
static let lastRefresh = "lastRefresh"
static let addWebFeedAccountID = "addWebFeedAccountID"
static let addWebFeedFolderName = "addWebFeedFolderName"
static let addFolderAccountID = "addFolderAccountID"
2020-07-02 03:57:36 +02:00
static let timelineSortDirection = "timelineSortDirection"
// iOS Defaults
static let userInterfaceColorPalette = "userInterfaceColorPalette"
static let timelineGroupByFeed = "timelineGroupByFeed"
static let refreshClearsReadArticles = "refreshClearsReadArticles"
static let timelineNumberOfLines = "timelineNumberOfLines"
static let timelineIconSize = "timelineIconSize"
static let articleFullscreenAvailable = "articleFullscreenAvailable"
static let articleFullscreenEnabled = "articleFullscreenEnabled"
static let confirmMarkAllAsRead = "confirmMarkAllAsRead"
// macOS Defaults
static let windowState = "windowState"
static let sidebarFontSize = "sidebarFontSize"
static let timelineFontSize = "timelineFontSize"
static let detailFontSize = "detailFontSize"
static let openInBrowserInBackground = "openInBrowserInBackground"
static let importOPMLAccountID = "importOPMLAccountID"
static let exportOPMLAccountID = "exportOPMLAccountID"
static let defaultBrowserID = "defaultBrowserID"
static let checkForUpdatesAutomatically = "checkForUpdatesAutomatically"
static let downloadTestBuilds = "downloadTestBuild"
static let sendCrashLogs = "sendCrashLogs"
// Hidden macOS Defaults
static let showDebugMenu = "ShowDebugMenu"
static let timelineShowsSeparators = "CorreiaSeparators"
static let showTitleOnMainWindow = "KafasisTitleMode"
#if !MAC_APP_STORE
static let webInspectorEnabled = "WebInspectorEnabled"
static let webInspectorStartsAttached = "__WebInspectorPageGroupLevel1__.WebKit2InspectorStartsAttached"
#endif
}
2020-07-02 03:57:36 +02:00
private static let smallestFontSizeRawValue = FontSize.small.rawValue
private static let largestFontSizeRawValue = FontSize.veryLarge.rawValue
// MARK: Development Builds
let isDeveloperBuild: Bool = {
if let dev = Bundle.main.object(forInfoDictionaryKey: "DeveloperEntitlements") as? String, dev == "-dev" {
return true
}
return false
}()
2020-07-02 03:57:36 +02:00
// MARK: First Run Details
var firstRunDate: Date? {
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.setValue(newValue, forKey: Key.firstRunDate)
objectWillChange.send()
}
get {
2020-07-02 03:57:36 +02:00
AppDefaults.store.object(forKey: Key.firstRunDate) as? Date
}
}
2020-07-02 03:57:36 +02:00
// MARK: Refresh Interval
@AppStorage(wrappedValue: 4, Key.refreshInterval, store: store) var interval: Int {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
var refreshInterval: RefreshInterval {
RefreshInterval(rawValue: interval) ?? RefreshInterval.everyHour
}
// MARK: Dock Badge
@AppStorage(wrappedValue: false, Key.hideDockUnreadCount, store: store) var hideDockUnreadCount {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
// MARK: Color Palette
var userInterfaceColorPalette: UserInterfaceColorPalette {
get {
2020-07-02 03:57:36 +02:00
if let palette = UserInterfaceColorPalette(rawValue: AppDefaults.store.integer(forKey: Key.userInterfaceColorPalette)) {
return palette
}
return .automatic
}
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.set(newValue.rawValue, forKey: Key.userInterfaceColorPalette)
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
// MARK: Feeds & Folders
@AppStorage(Key.addWebFeedAccountID, store: store) var addWebFeedAccountID: String?
@AppStorage(Key.addWebFeedFolderName, store: store) var addWebFeedFolderName: String?
@AppStorage(Key.addFolderAccountID, store: store) var addFolderAccountID: String?
@AppStorage(wrappedValue: false, Key.confirmMarkAllAsRead, store: store) var confirmMarkAllAsRead: Bool
// MARK: Extension Points
var activeExtensionPointIDs: [[AnyHashable : AnyHashable]]? {
get {
2020-07-02 03:57:36 +02:00
return AppDefaults.store.object(forKey: Key.activeExtensionPointIDs) as? [[AnyHashable : AnyHashable]]
}
set {
UserDefaults.standard.set(newValue, forKey: Key.activeExtensionPointIDs)
2020-07-02 03:57:36 +02:00
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
// MARK: Image Cache
var lastImageCacheFlushDate: Date? {
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.setValue(newValue, forKey: Key.lastImageCacheFlushDate)
objectWillChange.send()
}
get {
2020-07-02 03:57:36 +02:00
AppDefaults.store.object(forKey: Key.lastImageCacheFlushDate) as? Date
}
}
2020-07-02 03:57:36 +02:00
// MARK: Timeline
@AppStorage(wrappedValue: false, Key.timelineGroupByFeed, store: store) var timelineGroupByFeed: Bool
@AppStorage(wrappedValue: 3, Key.timelineNumberOfLines, store: store) var timelineNumberOfLines: Int {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: 40.0, Key.timelineIconSize, store: store) var timelineIconSize: Double {
didSet {
objectWillChange.send()
}
2020-07-02 03:57:36 +02:00
}
/// Set to `true` to sort oldest to newest, `false` for newest to oldest. Default is `false`.
@AppStorage(wrappedValue: false, Key.timelineSortDirection, store: store) var timelineSortDirection: Bool
// MARK: Refresh
@AppStorage(wrappedValue: false, Key.refreshClearsReadArticles, store: store) var refreshClearsReadArticles: Bool
// MARK: Articles
@AppStorage(wrappedValue: false, Key.articleFullscreenAvailable, store: store) var articleFullscreenAvailable: Bool
// MARK: Refresh
var lastRefresh: Date? {
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.setValue(newValue, forKey: Key.lastRefresh)
objectWillChange.send()
}
get {
2020-07-02 03:57:36 +02:00
AppDefaults.store.object(forKey: Key.lastRefresh) as? Date
}
}
2020-07-02 03:57:36 +02:00
// MARK: Window State
var windowState: [AnyHashable : Any]? {
get {
2020-07-02 03:57:36 +02:00
return AppDefaults.store.object(forKey: Key.windowState) as? [AnyHashable : Any]
}
set {
2020-07-02 03:57:36 +02:00
UserDefaults.standard.set(newValue, forKey: Key.windowState)
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: false, Key.openInBrowserInBackground, store: store) var openInBrowserInBackground: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
var sidebarFontSize: FontSize {
get {
2020-07-02 03:57:36 +02:00
return fontSize(for: Key.sidebarFontSize)
}
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.set(newValue.rawValue, forKey: Key.sidebarFontSize)
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
var timelineFontSize: FontSize {
get {
2020-07-02 03:57:36 +02:00
return fontSize(for: Key.timelineFontSize)
}
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineFontSize)
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
var detailFontSize: FontSize {
get {
2020-07-02 03:57:36 +02:00
return fontSize(for: Key.detailFontSize)
}
set {
2020-07-02 03:57:36 +02:00
AppDefaults.store.set(newValue.rawValue, forKey: Key.detailFontSize)
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(Key.importOPMLAccountID, store: store) var importOPMLAccountID: String? {
didSet {
objectWillChange.send()
}
2020-07-02 03:57:36 +02:00
}
@AppStorage(Key.exportOPMLAccountID, store: store) var exportOPMLAccountID: String? {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(Key.defaultBrowserID, store: store) var defaultBrowserID: String? {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(Key.showTitleOnMainWindow, store: store) var showTitleOnMainWindow: Bool? {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: false, Key.showDebugMenu, store: store) var showDebugMenu: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: false, Key.timelineShowsSeparators, store: store) var timelineShowsSeparators: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
#if !MAC_APP_STORE
@AppStorage(wrappedValue: false, Key.webInspectorEnabled, store: store) var webInspectorEnabled: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: false, Key.webInspectorStartsAttached, store: store) var webInspectorStartsAttached: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
#endif
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: true, Key.checkForUpdatesAutomatically, store: store) var checkForUpdatesAutomatically: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: false, Key.downloadTestBuilds, store: store) var downloadTestBuilds: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: true, Key.sendCrashLogs, store: store) var sendCrashLogs: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
}
2020-07-02 03:57:36 +02:00
extension AppDefaults {
func isFirstRun() -> Bool {
if let _ = AppDefaults.store.object(forKey: Key.firstRunDate) as? Date {
return false
}
2020-07-02 03:57:36 +02:00
firstRunDate = Date()
return true
}
2020-07-02 03:57:36 +02:00
func fontSize(for key: String) -> FontSize {
// Punted till after 1.0.
return .medium
}
}