-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathAppDefaults.swift
More file actions
613 lines (547 loc) · 19.4 KB
/
AppDefaults.swift
File metadata and controls
613 lines (547 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//
// AppDefaults.swift
// NetNewsWire
//
// Created by Brent Simmons on 9/22/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import UIKit
import os.log
import Account
import Articles
enum UserInterfaceColorPalette: Int, CustomStringConvertible, CaseIterable {
case automatic = 0
case light = 1
case dark = 2
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")
}
}
}
extension Notification.Name {
public static let userInterfaceColorPaletteDidUpdate = Notification.Name("UserInterfaceColorPaletteDidUpdateNotification")
public static let timelineIconSizeDidChange = Notification.Name("TimelineIconSizeDidChangeNotification")
public static let timelineNumberOfLinesDidChange = Notification.Name("TimelineNumberOfLinesDidChangeNotification")
}
final class AppDefaults: Sendable {
static let shared = AppDefaults()
static let defaultThemeName = "Default"
fileprivate static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "AppDefaults")
private init() {}
nonisolated(unsafe) 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)!
}()
struct Key {
static let userInterfaceColorPalette = "userInterfaceColorPalette"
static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
static let firstRunDate = "firstRunDate"
static let timelineGroupByFeed = "timelineGroupByFeed"
static let refreshClearsReadArticles = "refreshClearsReadArticles"
static let timelineNumberOfLines = "timelineNumberOfLines"
static let timelineIconDimension = "timelineIconSize"
static let timelineSortDirection = "timelineSortDirection"
static let articleFullscreenAvailable = "articleFullscreenAvailable"
static let articleFullscreenEnabled = "articleFullscreenEnabled"
static let markAsReadOnOpen = "markAsReadOnOpen"
static let confirmMarkAllAsRead = "confirmMarkAllAsRead"
static let lastRefresh = "lastRefresh"
static let addFeedAccountID = "addFeedAccountID"
static let addFeedFolderName = "addFeedFolderName"
static let addFolderAccountID = "addFolderAccountID"
static let useSystemBrowser = "useSystemBrowser"
static let currentThemeName = "currentThemeName"
static let articleContentJavascriptEnabled = "articleContentJavascriptEnabled"
static let hideReadFeeds = "hideReadFeeds"
static let isShowingExtractedArticle = "isShowingExtractedArticle"
static let articleWindowScrollY = "articleWindowScrollY"
static let expandedContainers = "expandedContainers"
static let smartFeedsHidingReadArticles = "smartFeedsHidingReadArticles"
static let feedsHidingReadArticles = "feedsHidingReadArticles"
static let foldersShowingReadArticles = "foldersShowingReadArticles"
static let selectedSidebarItem = "selectedSidebarItem"
static let selectedArticle = "selectedArticle"
static let didMigrateLegacyStateRestorationInfo = "didMigrateLegacyStateRestorationInfo"
static let splitViewPreferredDisplayMode = "splitViewPreferredDisplayMode"
}
let isDeveloperBuild: Bool = {
if let dev = Bundle.main.object(forInfoDictionaryKey: "DeveloperEntitlements") as? String, dev == "-dev" {
return true
}
return false
}()
let isFirstRun: Bool = {
if AppDefaults.store.object(forKey: Key.firstRunDate) is Date {
return false
}
firstRunDate = Date()
return true
}()
static var userInterfaceColorPalette: UserInterfaceColorPalette {
get {
if let result = UserInterfaceColorPalette(rawValue: int(for: Key.userInterfaceColorPalette)) {
return result
}
return .automatic
}
set {
setInt(for: Key.userInterfaceColorPalette, newValue.rawValue)
NotificationCenter.default.post(name: .userInterfaceColorPaletteDidUpdate, object: self)
}
}
var addFeedAccountID: String? {
get {
return AppDefaults.string(for: Key.addFeedAccountID)
}
set {
AppDefaults.setString(for: Key.addFeedAccountID, newValue)
}
}
var addFeedFolderName: String? {
get {
return AppDefaults.string(for: Key.addFeedFolderName)
}
set {
AppDefaults.setString(for: Key.addFeedFolderName, newValue)
}
}
var addFolderAccountID: String? {
get {
return AppDefaults.string(for: Key.addFolderAccountID)
}
set {
AppDefaults.setString(for: Key.addFolderAccountID, newValue)
}
}
var useSystemBrowser: Bool {
get {
return UserDefaults.standard.bool(forKey: Key.useSystemBrowser)
}
set {
UserDefaults.standard.setValue(newValue, forKey: Key.useSystemBrowser)
}
}
var lastImageCacheFlushDate: Date? {
get {
return AppDefaults.date(for: Key.lastImageCacheFlushDate)
}
set {
AppDefaults.setDate(for: Key.lastImageCacheFlushDate, newValue)
}
}
var timelineGroupByFeed: Bool {
get {
return AppDefaults.bool(for: Key.timelineGroupByFeed)
}
set {
AppDefaults.setBool(for: Key.timelineGroupByFeed, newValue)
}
}
var refreshClearsReadArticles: Bool {
get {
return AppDefaults.bool(for: Key.refreshClearsReadArticles)
}
set {
AppDefaults.setBool(for: Key.refreshClearsReadArticles, newValue)
}
}
var timelineSortDirection: ComparisonResult {
get {
return AppDefaults.sortDirection(for: Key.timelineSortDirection)
}
set {
AppDefaults.setSortDirection(for: Key.timelineSortDirection, newValue)
}
}
var articleFullscreenAvailable: Bool {
get {
return AppDefaults.bool(for: Key.articleFullscreenAvailable)
}
set {
AppDefaults.setBool(for: Key.articleFullscreenAvailable, newValue)
}
}
var articleFullscreenEnabled: Bool {
get {
return articleFullscreenAvailable && AppDefaults.bool(for: Key.articleFullscreenEnabled)
}
set {
AppDefaults.setBool(for: Key.articleFullscreenEnabled, newValue)
}
}
var logicalArticleFullscreenEnabled: Bool {
articleFullscreenAvailable && articleFullscreenEnabled
}
var markAsReadOnOpen: Bool {
get {
return AppDefaults.bool(for: Key.markAsReadOnOpen)
}
set {
AppDefaults.setBool(for: Key.markAsReadOnOpen, newValue)
}
}
var confirmMarkAllAsRead: Bool {
get {
return AppDefaults.bool(for: Key.confirmMarkAllAsRead)
}
set {
AppDefaults.setBool(for: Key.confirmMarkAllAsRead, newValue)
}
}
var isArticleContentJavascriptEnabled: Bool {
get {
return AppDefaults.bool(for: Key.articleContentJavascriptEnabled)
}
set {
AppDefaults.setBool(for: Key.articleContentJavascriptEnabled, newValue)
}
}
var splitViewPreferredDisplayMode: Int {
get {
return AppDefaults.int(for: Key.splitViewPreferredDisplayMode)
}
set {
AppDefaults.setInt(for: Key.splitViewPreferredDisplayMode, newValue)
}
}
var lastRefresh: Date? {
get {
return AppDefaults.date(for: Key.lastRefresh)
}
set {
AppDefaults.setDate(for: Key.lastRefresh, newValue)
}
}
var timelineNumberOfLines: Int {
get {
return AppDefaults.int(for: Key.timelineNumberOfLines)
}
set {
AppDefaults.setInt(for: Key.timelineNumberOfLines, newValue)
NotificationCenter.default.post(name: .timelineNumberOfLinesDidChange, object: nil)
}
}
var timelineIconSize: IconSize {
get {
let rawValue = AppDefaults.store.integer(forKey: Key.timelineIconDimension)
return IconSize(rawValue: rawValue) ?? IconSize.medium
}
set {
AppDefaults.store.set(newValue.rawValue, forKey: Key.timelineIconDimension)
NotificationCenter.default.post(name: .timelineIconSizeDidChange, object: nil)
}
}
var currentThemeName: String? {
get {
return AppDefaults.string(for: Key.currentThemeName)
}
set {
AppDefaults.setString(for: Key.currentThemeName, newValue)
}
}
var hideReadFeeds: Bool {
get {
UserDefaults.standard.bool(forKey: Key.hideReadFeeds)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.hideReadFeeds)
}
}
var isShowingExtractedArticle: Bool {
get {
UserDefaults.standard.bool(forKey: Key.isShowingExtractedArticle)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.isShowingExtractedArticle)
}
}
var articleWindowScrollY: Int {
get {
UserDefaults.standard.integer(forKey: Key.articleWindowScrollY)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.articleWindowScrollY)
}
}
var expandedContainers: Set<ContainerIdentifier> {
get {
guard let rawIdentifiers = UserDefaults.standard.array(forKey: Key.expandedContainers) as? [[String: String]] else {
return Set<ContainerIdentifier>()
}
let containerIdentifiers = rawIdentifiers.compactMap { ContainerIdentifier(userInfo: $0) }
return Set(containerIdentifiers)
}
set {
Self.logger.debug("AppDefaults: set expandedContainers: \(newValue)")
let containerIdentifierUserInfos = newValue.compactMap { $0.userInfo }
UserDefaults.standard.set(containerIdentifierUserInfos, forKey: Key.expandedContainers)
}
}
var smartFeedsHidingReadArticles: Set<String> {
get {
let smartFeedIDs = UserDefaults.standard.array(forKey: Key.smartFeedsHidingReadArticles) as? [String] ?? []
return Set(smartFeedIDs)
}
set {
let array = Array(newValue)
UserDefaults.standard.set(array, forKey: Key.smartFeedsHidingReadArticles)
}
}
var feedsHidingReadArticles: [String: Set<String>] { // Account id: Set<feed.feedID>
get {
guard let d = UserDefaults.standard.dictionary(forKey: Key.feedsHidingReadArticles) as? [String: [String]] else {
return [String: Set<String>]()
}
return d.mapValues { Set($0) }
}
set {
let d = newValue.mapValues { Array($0) }
UserDefaults.standard.set(d, forKey: Key.feedsHidingReadArticles)
}
}
var foldersShowingReadArticles: [String: Set<String>] { // Account id: Set<folder.nameForDisplay>
get {
guard let d = UserDefaults.standard.dictionary(forKey: Key.foldersShowingReadArticles) as? [String: [String]] else {
return [String: Set<String>]()
}
return d.mapValues { Set($0) }
}
set {
let d = newValue.mapValues { Array($0) }
UserDefaults.standard.set(d, forKey: Key.foldersShowingReadArticles)
}
}
var selectedSidebarItem: SidebarItemIdentifier? {
get {
guard let userInfo = UserDefaults.standard.dictionary(forKey: Key.selectedSidebarItem) as? [String: String] else {
return nil
}
return SidebarItemIdentifier(userInfo: userInfo)
}
set {
guard let newValue else {
UserDefaults.standard.removeObject(forKey: Key.selectedSidebarItem)
return
}
UserDefaults.standard.set(newValue.userInfo, forKey: Key.selectedSidebarItem)
}
}
var selectedArticle: ArticleSpecifier? {
get {
guard let d = UserDefaults.standard.dictionary(forKey: Key.selectedArticle) as? [String: String] else {
return nil
}
return ArticleSpecifier(dictionary: d)
}
set {
guard let newValue else {
UserDefaults.standard.removeObject(forKey: Key.selectedArticle)
return
}
UserDefaults.standard.set(newValue.dictionary, forKey: Key.selectedArticle)
}
}
var didMigrateLegacyStateRestorationInfo: Bool {
get {
UserDefaults.standard.bool(forKey: Key.didMigrateLegacyStateRestorationInfo)
}
set {
UserDefaults.standard.set(newValue, forKey: Key.didMigrateLegacyStateRestorationInfo)
}
}
@MainActor static func registerDefaults() {
let defaults: [String: Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue,
Key.timelineGroupByFeed: false,
Key.refreshClearsReadArticles: false,
Key.timelineNumberOfLines: 2,
Key.timelineIconDimension: IconSize.medium.rawValue,
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue,
Key.articleFullscreenAvailable: false,
Key.articleFullscreenEnabled: false,
Key.markAsReadOnOpen: true,
Key.confirmMarkAllAsRead: true,
Key.articleContentJavascriptEnabled: true,
Key.currentThemeName: Self.defaultThemeName,
Key.splitViewPreferredDisplayMode: UISplitViewController.DisplayMode.oneBesideSecondary.rawValue]
AppDefaults.store.register(defaults: defaults)
}
}
private extension AppDefaults {
static var firstRunDate: Date? {
get {
return date(for: Key.firstRunDate)
}
set {
setDate(for: Key.firstRunDate, newValue)
}
}
static func string(for key: String) -> String? {
return UserDefaults.standard.string(forKey: key)
}
static func setString(for key: String, _ value: String?) {
UserDefaults.standard.set(value, forKey: key)
}
static func bool(for key: String) -> Bool {
return AppDefaults.store.bool(forKey: key)
}
static func setBool(for key: String, _ flag: Bool) {
AppDefaults.store.set(flag, forKey: key)
}
static func int(for key: String) -> Int {
return AppDefaults.store.integer(forKey: key)
}
static func setInt(for key: String, _ x: Int) {
AppDefaults.store.set(x, forKey: key)
}
static func date(for key: String) -> Date? {
return AppDefaults.store.object(forKey: key) as? Date
}
static func setDate(for key: String, _ date: Date?) {
AppDefaults.store.set(date, forKey: key)
}
static func sortDirection(for key: String) -> ComparisonResult {
let rawInt = int(for: key)
if rawInt == ComparisonResult.orderedAscending.rawValue {
return .orderedAscending
}
return .orderedDescending
}
static func setSortDirection(for key: String, _ value: ComparisonResult) {
if value == .orderedAscending {
setInt(for: key, ComparisonResult.orderedAscending.rawValue)
} else {
setInt(for: key, ComparisonResult.orderedDescending.rawValue)
}
}
}
struct StateRestorationInfo {
let hideReadFeeds: Bool
let expandedContainers: Set<ContainerIdentifier>
let selectedSidebarItem: SidebarItemIdentifier?
let smartFeedsHidingReadArticles: Set<String>
let feedsHidingReadArticles: [String: Set<String>]
let foldersShowingReadArticles: [String: Set<String>]
let selectedArticle: ArticleSpecifier?
let articleWindowScrollY: Int
let isShowingExtractedArticle: Bool
init(hideReadFeeds: Bool,
expandedContainers: Set<ContainerIdentifier>,
selectedSidebarItem: SidebarItemIdentifier?,
smartFeedsHidingReadArticles: Set<String>,
feedsHidingReadArticles: [String: Set<String>],
foldersShowingReadArticles: [String: Set<String>],
selectedArticle: ArticleSpecifier?,
articleWindowScrollY: Int,
isShowingExtractedArticle: Bool) {
self.hideReadFeeds = hideReadFeeds
self.expandedContainers = expandedContainers
self.selectedSidebarItem = selectedSidebarItem
self.smartFeedsHidingReadArticles = smartFeedsHidingReadArticles
self.feedsHidingReadArticles = feedsHidingReadArticles
self.foldersShowingReadArticles = foldersShowingReadArticles
self.selectedArticle = selectedArticle
self.articleWindowScrollY = articleWindowScrollY
self.isShowingExtractedArticle = isShowingExtractedArticle
AppDefaults.logger.debug("AppDefaults: StateRestorationInfo:\nexpandedContainers: \(expandedContainers)\nselectedSidebarItem: \(selectedSidebarItem?.userInfo ?? [String: String]())\nsmartFeedsHidingReadArticles: \(smartFeedsHidingReadArticles)\nfeedsHidingReadArticles: \(feedsHidingReadArticles)\nfoldersShowingReadArticles: \(foldersShowingReadArticles)\nselectedArticle: \(selectedArticle?.dictionary ?? [String: String]())\narticleWindowScrollY: \(articleWindowScrollY)\nisShowingExtractedArticle: \(isShowingExtractedArticle ? "true" : "false")")
}
init() {
self.init(hideReadFeeds: AppDefaults.shared.hideReadFeeds,
expandedContainers: AppDefaults.shared.expandedContainers,
selectedSidebarItem: AppDefaults.shared.selectedSidebarItem,
smartFeedsHidingReadArticles: AppDefaults.shared.smartFeedsHidingReadArticles,
feedsHidingReadArticles: AppDefaults.shared.feedsHidingReadArticles,
foldersShowingReadArticles: AppDefaults.shared.foldersShowingReadArticles,
selectedArticle: AppDefaults.shared.selectedArticle,
articleWindowScrollY: AppDefaults.shared.articleWindowScrollY,
isShowingExtractedArticle: AppDefaults.shared.isShowingExtractedArticle)
}
// TODO: Delete for NetNewsWire 7.1.
init(legacyState: NSUserActivity?) {
if AppDefaults.shared.didMigrateLegacyStateRestorationInfo {
self.init()
return
}
AppDefaults.shared.didMigrateLegacyStateRestorationInfo = true
// Extract legacy window state if available
guard let windowState = legacyState?.userInfo?[UserInfoKey.windowState] as? [AnyHashable: Any] else {
self.init()
return
}
let hideReadFeeds: Bool
if let legacyValue = windowState[UserInfoKey.readFeedsFilterState] as? Bool {
hideReadFeeds = legacyValue
} else {
hideReadFeeds = AppDefaults.shared.hideReadFeeds
}
let expandedContainers: Set<ContainerIdentifier>
if let legacyState = windowState[UserInfoKey.containerExpandedWindowState] as? [[AnyHashable: AnyHashable]] {
let convertedState = legacyState.compactMap { dict -> [String: String]? in
var stringDict = [String: String]()
for (key, value) in dict {
if let keyString = key as? String, let valueString = value as? String {
stringDict[keyString] = valueString
}
}
return stringDict.isEmpty ? nil : stringDict
}
let containerIdentifiers = convertedState.compactMap { ContainerIdentifier(userInfo: $0) }
expandedContainers = Set(containerIdentifiers)
} else {
expandedContainers = AppDefaults.shared.expandedContainers
}
let sidebarItemsHidingReadArticles: Set<SidebarItemIdentifier>
if let legacyState = windowState[UserInfoKey.readArticlesFilterState] as? [[AnyHashable: AnyHashable]: Bool] {
let enabledFeeds = legacyState.filter { $0.value == true }
let convertedState = enabledFeeds.keys.compactMap { key -> [String: String]? in
var stringDict = [String: String]()
for (k, v) in key {
if let keyString = k as? String, let valueString = v as? String {
stringDict[keyString] = valueString
}
}
return stringDict.isEmpty ? nil : stringDict
}
let sidebarItemIdentifiers = convertedState.compactMap { SidebarItemIdentifier(userInfo: $0) }
sidebarItemsHidingReadArticles = Set(sidebarItemIdentifiers)
} else {
sidebarItemsHidingReadArticles = Set<SidebarItemIdentifier>()
}
var smartFeedsHidingReadArticles = Set<String>()
var feedsHidingReadArticles = [String: Set<String>]()
for sidebarItem in sidebarItemsHidingReadArticles {
switch sidebarItem {
case .smartFeed(let id):
smartFeedsHidingReadArticles.insert(id)
case .feed(let accountID, let feedID):
var feedIDs = feedsHidingReadArticles[accountID] ?? Set<String>()
feedIDs.insert(feedID)
feedsHidingReadArticles[accountID] = feedIDs
default:
continue
}
}
let selectedSidebarItem: SidebarItemIdentifier?
if let legacyState = windowState[UserInfoKey.feedIdentifier] as? [String: String],
let sidebarItemIdentifier = SidebarItemIdentifier(userInfo: legacyState) {
selectedSidebarItem = sidebarItemIdentifier
} else {
selectedSidebarItem = AppDefaults.shared.selectedSidebarItem
}
self.init(hideReadFeeds: hideReadFeeds,
expandedContainers: expandedContainers,
selectedSidebarItem: selectedSidebarItem,
smartFeedsHidingReadArticles: smartFeedsHidingReadArticles,
feedsHidingReadArticles: feedsHidingReadArticles,
foldersShowingReadArticles: AppDefaults.shared.foldersShowingReadArticles,
selectedArticle: AppDefaults.shared.selectedArticle,
articleWindowScrollY: AppDefaults.shared.articleWindowScrollY,
isShowingExtractedArticle: AppDefaults.shared.isShowingExtractedArticle)
}
}