/
Preferences.swift
177 lines (146 loc) · 4.96 KB
/
Preferences.swift
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
//
// Preferences.swift
// WWDC
//
// Created by Guilherme Rambo on 13/05/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Foundation
import ConfCore
extension Notification.Name {
static let LocalVideoStoragePathPreferenceDidChange = Notification.Name("LocalVideoStoragePathPreferenceDidChange")
static let RefreshPeriodicallyPreferenceDidChange = Notification.Name("RefreshPeriodicallyPreferenceDidChange")
static let SkipBackAndForwardBy30SecondsPreferenceDidChange = Notification.Name("SkipBackAndForwardBy30SecondsPreferenceDidChange")
static let SyncUserDataPreferencesDidChange = Notification.Name("SyncUserDataPreferencesDidChange")
static let PreferredTranscriptLanguageDidChange = Notification.Name("PreferredTranscriptLanguageDidChange")
}
final class Preferences {
static let shared: Preferences = Preferences()
private let defaults = UserDefaults.standard
private static let defaultLocalVideoStoragePath = NSString.path(withComponents: [NSHomeDirectory(), "Library", "Application Support", "WWDC"])
init() {
defaults.register(defaults: [
"localVideoStoragePath": Self.defaultLocalVideoStoragePath,
"includeAppBannerInSharedClips": true
])
}
/// The URL for the folder where downloaded videos will be saved
var localVideoStorageURL: URL {
set { localVideoStoragePath = newValue.path }
get { URL(fileURLWithPath: localVideoStoragePath) }
}
private var localVideoStoragePath: String {
set {
defaults.set(newValue, forKey: #function)
}
get {
if let path = defaults.object(forKey: #function) as? String {
return path
} else {
return Self.defaultLocalVideoStoragePath
}
}
}
var activeTab: MainWindowTab {
get {
let rawValue = defaults.integer(forKey: #function)
return MainWindowTab(rawValue: rawValue) ?? .schedule
}
set {
defaults.set(newValue.rawValue, forKey: #function)
}
}
var selectedScheduleItemIdentifier: String? {
get {
return defaults.object(forKey: #function) as? String
}
set {
defaults.set(newValue, forKey: #function)
}
}
var selectedVideoItemIdentifier: String? {
get {
return defaults.object(forKey: #function) as? String
}
set {
defaults.set(newValue, forKey: #function)
}
}
var filtersState: String? {
get {
defaults.object(forKey: #function) as? String
}
set {
defaults.set(newValue, forKey: #function)
}
}
var userOptedOutOfCrashReporting: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
}
}
var searchInTranscripts: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
}
}
var searchInBookmarks: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
}
}
var refreshPeriodically: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
NotificationCenter.default.post(name: .RefreshPeriodicallyPreferenceDidChange, object: nil)
}
}
var skipIntro: Bool {
get { defaults.bool(forKey: #function) }
set { defaults.set(newValue, forKey: #function) }
}
var includeAppBannerInSharedClips: Bool {
get { defaults.bool(forKey: #function) }
set { defaults.set(newValue, forKey: #function) }
}
var skipBackAndForwardBy30Seconds: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
NotificationCenter.default.post(name: .SkipBackAndForwardBy30SecondsPreferenceDidChange, object: nil)
}
}
var syncUserData: Bool {
get {
return defaults.bool(forKey: #function)
}
set {
defaults.set(newValue, forKey: #function)
NotificationCenter.default.post(name: .SyncUserDataPreferencesDidChange, object: nil)
}
}
public static let fallbackTranscriptLanguageCode = "en"
var transcriptLanguageCode: String {
get { defaults.string(forKey: #function) ?? ConfigResponse.fallbackFeedLanguage }
set {
let notify = newValue != transcriptLanguageCode
defaults.set(newValue, forKey: #function)
guard notify else { return }
NotificationCenter.default.post(name: .PreferredTranscriptLanguageDidChange, object: newValue)
}
}
}