Files
NetNewsWire/Shared/AppDefaults.swift
T

163 lines
4.6 KiB
Swift
Raw Normal View History

//
// AppDefaults.swift
// NetNewsWire
//
// Created by Brent Simmons on 10/22/26.
//
import Foundation
2026-10-22 20:55:50 -07:00
/// Settings common to Mac and iOS. Platform-specific settings are in extensions in AppDefaults+Mac.swift and AppDefaults+iOS.swift.
enum AppDefaults {
static let defaultThemeName = "Default"
struct Key {
static let firstRunDate = "firstRunDate"
static let timelineSortDirection = "timelineSortDirection"
static let timelineGroupByFeed = "timelineGroupByFeed"
static let addFeedAccountID = "addFeedAccountID"
static let addFeedFolderName = "addFeedFolderName"
static let addFolderAccountID = "addFolderAccountID"
static let currentThemeName = "currentThemeName"
static let articleContentJavascriptEnabled = "articleContentJavascriptEnabled"
static let unreadCountDisplay = "unreadCountDisplay"
2026-10-26 10:11:55 -07:00
static let timelineThumbnailsDisplay = "timelineThumbnailsDisplay"
2026-10-26 11:36:44 -07:00
static let timelineFeedIconsDisplay = "timelineFeedIconsDisplay"
}
/// Registered by both platforms. Each platform’s registerDefaults adds its own.
2026-10-22 20:55:50 -07:00
static var commonRegisteredDefaults: [String: Any] {
[
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue,
Key.timelineGroupByFeed: false,
Key.currentThemeName: defaultThemeName,
2026-10-26 10:11:55 -07:00
Key.articleContentJavascriptEnabled: true,
2026-10-26 11:36:44 -07:00
Key.timelineThumbnailsDisplay: TimelineThumbnailsDisplay.small.rawValue,
Key.timelineFeedIconsDisplay: TimelineFeedIconsDisplay.automatic.rawValue
2026-10-22 20:55:50 -07:00
]
}
static let isDeveloperBuild: Bool = {
if let dev = Bundle.main.object(forInfoDictionaryKey: "DeveloperEntitlements") as? String, dev == "-dev" {
return true
}
return false
}()
static let isFirstRun: Bool = {
if UserDefaults.standard.object(forKey: Key.firstRunDate) is Date {
return false
}
firstRunDate = Date()
return true
}()
static var addFeedAccountID: String? {
get {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.string(forKey: Key.addFeedAccountID)
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue, forKey: Key.addFeedAccountID)
}
}
static var addFeedFolderName: String? {
get {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.string(forKey: Key.addFeedFolderName)
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue, forKey: Key.addFeedFolderName)
}
}
static var addFolderAccountID: String? {
get {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.string(forKey: Key.addFolderAccountID)
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue, forKey: Key.addFolderAccountID)
}
}
static var currentThemeName: String? {
get {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.string(forKey: Key.currentThemeName)
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue, forKey: Key.currentThemeName)
}
}
static var isArticleContentJavascriptEnabled: Bool {
get {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.bool(forKey: Key.articleContentJavascriptEnabled)
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue, forKey: Key.articleContentJavascriptEnabled)
}
}
static var unreadCountDisplay: UnreadCountDisplay {
get {
2026-10-22 14:11:46 -07:00
UnreadCountDisplay(rawValue: UserDefaults.standard.integer(forKey: Key.unreadCountDisplay)) ?? .count
}
set {
2026-10-22 14:11:46 -07:00
UserDefaults.standard.set(newValue.rawValue, forKey: Key.unreadCountDisplay)
NotificationCenter.default.post(name: .unreadCountDisplaySettingDidChange, object: nil)
}
}
2026-10-26 10:11:55 -07:00
static var timelineThumbnailsDisplay: TimelineThumbnailsDisplay {
get {
TimelineThumbnailsDisplay(rawValue: UserDefaults.standard.integer(forKey: Key.timelineThumbnailsDisplay)) ?? .small
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: Key.timelineThumbnailsDisplay)
NotificationCenter.default.post(name: .timelineThumbnailsDisplayDidChange, object: nil)
}
}
2026-10-26 11:36:44 -07:00
static var timelineFeedIconsDisplay: TimelineFeedIconsDisplay {
get {
TimelineFeedIconsDisplay(rawValue: UserDefaults.standard.integer(forKey: Key.timelineFeedIconsDisplay)) ?? .automatic
}
set {
UserDefaults.standard.set(newValue.rawValue, forKey: Key.timelineFeedIconsDisplay)
NotificationCenter.default.post(name: .timelineFeedIconsDisplayDidChange, object: nil)
}
}
}
// MARK: - Helpers
extension AppDefaults {
static func date(for key: String) -> Date? {
2026-10-22 14:02:53 -07:00
UserDefaults.standard.object(forKey: key) as? Date
}
static func setDate(for key: String, _ date: Date?) {
UserDefaults.standard.set(date, forKey: key)
}
static func sortDirection(for key: String) -> ComparisonResult {
2026-10-22 14:11:46 -07:00
let rawInt = UserDefaults.standard.integer(forKey: key)
if rawInt == ComparisonResult.orderedAscending.rawValue {
return .orderedAscending
}
return .orderedDescending
}
}
private extension AppDefaults {
static var firstRunDate: Date? {
get {
2026-10-22 14:02:53 -07:00
date(for: Key.firstRunDate)
}
set {
setDate(for: Key.firstRunDate, newValue)
}
}
}