NetNewsWire/Multiplatform/Shared/AppDefaults.swift

347 lines
9.7 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
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-03 08:24:35 +02:00
2020-07-02 03:57:36 +02:00
static let userInterfaceColorPalette = "userInterfaceColorPalette"
2020-07-03 08:24:35 +02:00
static let timelineSortDirection = "timelineSortDirection"
2020-07-02 03:57:36 +02:00
static let timelineGroupByFeed = "timelineGroupByFeed"
2020-07-03 08:24:35 +02:00
static let timelineIconDimensions = "timelineIconDimensions"
2020-07-03 08:01:38 +02:00
static let timelineNumberOfLines = "timelineNumberOfLines"
// Sidebar Defaults
static let sidebarConfirmDelete = "sidebarConfirmDelete"
2020-07-03 08:01:38 +02:00
// iOS Defaults
static let refreshClearsReadArticles = "refreshClearsReadArticles"
static let articleFullscreenAvailable = "articleFullscreenAvailable"
2020-07-02 03:57:36 +02:00
static let articleFullscreenEnabled = "articleFullscreenEnabled"
static let confirmMarkAllAsRead = "confirmMarkAllAsRead"
// macOS Defaults
static let articleTextSize = "articleTextSize"
2020-07-02 03:57:36 +02:00
static let openInBrowserInBackground = "openInBrowserInBackground"
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
// 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)
2020-07-17 16:33:38 +02:00
#if os(macOS)
self.objectWillChange.send()
#else
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.objectWillChange.send()
})
2020-07-17 16:33:38 +02:00
#endif
}
}
2020-07-14 08:57:55 +02:00
static var userInterfaceColorScheme: ColorScheme? {
switch AppDefaults.shared.userInterfaceColorPalette {
case .light:
return ColorScheme.light
case .dark:
return ColorScheme.dark
default:
return nil
}
}
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
2020-07-13 02:43:25 +02:00
@AppStorage(wrappedValue: false, Key.timelineGroupByFeed, store: store) var timelineGroupByFeed: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
@AppStorage(wrappedValue: 2.0, Key.timelineNumberOfLines, store: store) var timelineNumberOfLines: Double {
2020-07-02 03:57:36 +02:00
didSet {
objectWillChange.send()
}
}
2020-07-03 08:24:35 +02:00
@AppStorage(wrappedValue: 40.0, Key.timelineIconDimensions, store: store) var timelineIconDimensions: Double {
2020-07-02 03:57:36 +02:00
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`.
2020-07-13 02:43:25 +02:00
@AppStorage(wrappedValue: false, Key.timelineSortDirection, store: store) var timelineSortDirection: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
// MARK: Sidebar
@AppStorage(wrappedValue: true, Key.sidebarConfirmDelete, store: store) var sidebarConfirmDelete: Bool {
didSet {
objectWillChange.send()
}
}
2020-07-02 03:57:36 +02:00
// MARK: Refresh
@AppStorage(wrappedValue: false, Key.refreshClearsReadArticles, store: store) var refreshClearsReadArticles: Bool
// MARK: Articles
@AppStorage(wrappedValue: false, Key.articleFullscreenAvailable, store: store) var articleFullscreenAvailable: Bool
@AppStorage(wrappedValue: false, Key.articleFullscreenEnabled, store: store) var articleFullscreenEnabled: Bool
@AppStorage(wrappedValue: 3, Key.articleTextSize, store: store) var articleTextSizeTag: Int {
didSet {
objectWillChange.send()
}
}
var articleTextSize: ArticleTextSize {
ArticleTextSize(rawValue: articleTextSizeTag) ?? ArticleTextSize.large
}
2020-07-02 03:57:36 +02:00
// 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
@AppStorage(wrappedValue: false, Key.openInBrowserInBackground, store: store) var openInBrowserInBackground: Bool {
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-03 08:24:35 +02:00
static func registerDefaults() {
let defaults: [String : Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue,
Key.timelineGroupByFeed: false,
Key.refreshClearsReadArticles: false,
Key.timelineNumberOfLines: 2,
Key.timelineIconDimensions: 40,
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue,
Key.articleFullscreenAvailable: false,
Key.articleFullscreenEnabled: false,
Key.confirmMarkAllAsRead: true,
"NSScrollViewShouldScrollUnderTitlebar": false,
Key.refreshInterval: RefreshInterval.everyHour.rawValue]
AppDefaults.store.register(defaults: defaults)
}
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
}
}