-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPrefs.swift
183 lines (150 loc) · 6.55 KB
/
Prefs.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
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
public struct PrefsKeys {
public static let KeyLastRemoteTabSyncTime = "lastRemoteTabSyncTime"
public static let KeyLastSyncFinishTime = "lastSyncFinishTime"
public static let KeyDefaultHomePageURL = "KeyDefaultHomePageURL"
public static let KeyNoImageModeStatus = "NoImageModeStatus"
public static let KeyNightModeButtonIsInMenu = "NightModeButtonIsInMenuPrefKey"
public static let KeyNightModeStatus = "NightModeStatus"
public static let KeyMailToOption = "MailToOption"
public static let HasFocusInstalled = "HasFocusInstalled"
public static let HasPocketInstalled = "HasPocketInstalled"
public static let IntroSeen = "IntroViewControllerSeen"
//Activity Stream
public static let KeyTopSitesCacheIsValid = "topSitesCacheIsValid"
public static let KeyTopSitesCacheSize = "topSitesCacheSize"
public static let KeyNewTab = "NewTabPrefKey"
public static let ASPocketStoriesVisible = "ASPocketStoriesVisible"
public static let ASRecentHighlightsVisible = "ASRecentHighlightsVisible"
public static let ASBookmarkHighlightsVisible = "ASBookmarkHighlightsVisible"
public static let ASLastInvalidation = "ASLastInvalidation"
public static let KeyUseCustomSyncService = "useCustomSyncService"
public static let KeyCustomSyncToken = "customSyncTokenServer"
public static let KeyCustomSyncProfile = "customSyncProfileServer"
public static let KeyCustomSyncOauth = "customSyncOauthServer"
public static let KeyCustomSyncAuth = "customSyncAuthServer"
public static let KeyCustomSyncWeb = "customSyncWebServer"
public static let UseStageServer = "useStageSyncService"
}
public struct PrefsDefaults {
public static let ChineseHomePageURL = "http://mobile.firefoxchina.cn/"
public static let ChineseNewTabDefault = "HomePage"
}
public protocol Prefs {
func getBranchPrefix() -> String
func branch(_ branch: String) -> Prefs
func setTimestamp(_ value: Timestamp, forKey defaultName: String)
func setLong(_ value: UInt64, forKey defaultName: String)
func setLong(_ value: Int64, forKey defaultName: String)
func setInt(_ value: Int32, forKey defaultName: String)
func setString(_ value: String, forKey defaultName: String)
func setBool(_ value: Bool, forKey defaultName: String)
func setObject(_ value: Any?, forKey defaultName: String)
func stringForKey(_ defaultName: String) -> String?
func objectForKey<T: Any>(_ defaultName: String) -> T?
func boolForKey(_ defaultName: String) -> Bool?
func intForKey(_ defaultName: String) -> Int32?
func timestampForKey(_ defaultName: String) -> Timestamp?
func longForKey(_ defaultName: String) -> Int64?
func unsignedLongForKey(_ defaultName: String) -> UInt64?
func stringArrayForKey(_ defaultName: String) -> [String]?
func arrayForKey(_ defaultName: String) -> [Any]?
func dictionaryForKey(_ defaultName: String) -> [String: Any]?
func removeObjectForKey(_ defaultName: String)
func clearAll()
}
open class MockProfilePrefs: Prefs {
let prefix: String
open func getBranchPrefix() -> String {
return self.prefix
}
// Public for testing.
open var things: NSMutableDictionary = NSMutableDictionary()
public init(things: NSMutableDictionary, prefix: String) {
self.things = things
self.prefix = prefix
}
public init() {
self.prefix = ""
}
open func branch(_ branch: String) -> Prefs {
return MockProfilePrefs(things: self.things, prefix: self.prefix + branch + ".")
}
private func name(_ name: String) -> String {
return self.prefix + name
}
open func setTimestamp(_ value: Timestamp, forKey defaultName: String) {
self.setLong(value, forKey: defaultName)
}
open func setLong(_ value: UInt64, forKey defaultName: String) {
setObject(NSNumber(value: value as UInt64), forKey: defaultName)
}
open func setLong(_ value: Int64, forKey defaultName: String) {
setObject(NSNumber(value: value as Int64), forKey: defaultName)
}
open func setInt(_ value: Int32, forKey defaultName: String) {
things[name(defaultName)] = NSNumber(value: value as Int32)
}
open func setString(_ value: String, forKey defaultName: String) {
things[name(defaultName)] = value
}
open func setBool(_ value: Bool, forKey defaultName: String) {
things[name(defaultName)] = value
}
open func setObject(_ value: Any?, forKey defaultName: String) {
things[name(defaultName)] = value
}
open func stringForKey(_ defaultName: String) -> String? {
return things[name(defaultName)] as? String
}
open func boolForKey(_ defaultName: String) -> Bool? {
return things[name(defaultName)] as? Bool
}
open func objectForKey<T: Any>(_ defaultName: String) -> T? {
return things[name(defaultName)] as? T
}
open func timestampForKey(_ defaultName: String) -> Timestamp? {
return unsignedLongForKey(defaultName)
}
open func unsignedLongForKey(_ defaultName: String) -> UInt64? {
return things[name(defaultName)] as? UInt64
}
open func longForKey(_ defaultName: String) -> Int64? {
return things[name(defaultName)] as? Int64
}
open func intForKey(_ defaultName: String) -> Int32? {
return things[name(defaultName)] as? Int32
}
open func stringArrayForKey(_ defaultName: String) -> [String]? {
if let arr = self.arrayForKey(defaultName) {
if let arr = arr as? [String] {
return arr
}
}
return nil
}
open func arrayForKey(_ defaultName: String) -> [Any]? {
let r: Any? = things.object(forKey: name(defaultName)) as Any?
if r == nil {
return nil
}
if let arr = r as? [Any] {
return arr
}
return nil
}
open func dictionaryForKey(_ defaultName: String) -> [String: Any]? {
return things.object(forKey: name(defaultName)) as? [String: Any]
}
open func removeObjectForKey(_ defaultName: String) {
self.things.removeObject(forKey: name(defaultName))
}
open func clearAll() {
let dictionary = things as! [String: Any]
let keysToDelete: [String] = dictionary.keys.filter { $0.hasPrefix(self.prefix) }
things.removeObjects(forKeys: keysToDelete)
}
}