-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
KeyValueStoreType.swift
229 lines (196 loc) · 7.17 KB
/
KeyValueStoreType.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
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
import Foundation
public enum AppKeys: String {
// swiftformat:disable wrap
case closedFacebookConnectInActivity = "com.kickstarter.KeyValueStoreType.closedFacebookConnectInActivity"
case closedFindFriendsInActivity = "com.kickstarter.KeyValueStoreType.closedFindFriendsInActivity"
case deniedNotificationContexts = "com.kickstarter.KeyValueStoreType.deniedNotificationContexts"
case favoriteCategoryIds = "favorite_category_ids"
case hasCompletedCategoryPersonalizationFlow = "com.kickstarter.KeyValueStoreType.hasCompletedCategoryPersonalizationFlow"
case hasDismissedPersonalizationCard = "com.kickstarter.KeyValueStoreType.hasDismissedPersonalizationCard"
case hasSeenFavoriteCategoryAlert = "com.kickstarter.KeyValueStoreType.hasSeenFavoriteCategoryAlert"
case hasSeenSaveProjectAlert = "com.kickstarter.KeyValueStoreType.hasSeenSaveProjectAlert"
case lastSeenActivitySampleId = "com.kickstarter.KeyValueStoreType.lastSeenActivitySampleId"
case onboardingCategories = "com.kickstarter.KeyValueStoreType.onboardingCategories"
case remoteConfigFeatureFlags = "com.kickstarter.KeyValueStoreType.remoteConfigFeatureFlags"
case seenAppRating = "com.kickstarter.KeyValueStoreType.hasSeenAppRating"
case seenGamesNewsletter = "com.kickstarter.KeyValueStoreType.hasSeenGamesNewsletter"
// swiftformat:enable wrap
}
public protocol KeyValueStoreType: AnyObject {
func set(_ value: Bool, forKey defaultName: String)
func set(_ value: Int, forKey defaultName: String)
func set(_ value: Any?, forKey defaultName: String)
func bool(forKey defaultName: String) -> Bool
func data(forKey defaultName: String) -> Data?
func dictionary(forKey defaultName: String) -> [String: Any]?
func integer(forKey defaultName: String) -> Int
func object(forKey defaultName: String) -> Any?
func string(forKey defaultName: String) -> String?
func synchronize() -> Bool
func removeObject(forKey defaultName: String)
var deniedNotificationContexts: [String] { get set }
var favoriteCategoryIds: [Int] { get set }
var hasClosedFacebookConnectInActivity: Bool { get set }
var hasClosedFindFriendsInActivity: Bool { get set }
var hasCompletedCategoryPersonalizationFlow: Bool { get set }
var hasDismissedPersonalizationCard: Bool { get set }
var hasSeenAppRating: Bool { get set }
var hasSeenFavoriteCategoryAlert: Bool { get set }
var hasSeenGamesNewsletterPrompt: Bool { get set }
var hasSeenSaveProjectAlert: Bool { get set }
var lastSeenActivitySampleId: Int { get set }
var onboardingCategories: Data? { get set }
var remoteConfigFeatureFlags: [String: Bool] { get set }
}
extension KeyValueStoreType {
public var favoriteCategoryIds: [Int] {
get {
return self.object(forKey: AppKeys.favoriteCategoryIds.rawValue) as? [Int] ?? []
}
set {
self.set(newValue, forKey: AppKeys.favoriteCategoryIds.rawValue)
}
}
public var deniedNotificationContexts: [String] {
get {
return self.object(forKey: AppKeys.deniedNotificationContexts.rawValue) as? [String] ?? []
}
set {
self.set(newValue, forKey: AppKeys.deniedNotificationContexts.rawValue)
}
}
public var hasDismissedPersonalizationCard: Bool {
get {
return self.bool(forKey: AppKeys.hasDismissedPersonalizationCard.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.hasDismissedPersonalizationCard.rawValue)
}
}
public var hasCompletedCategoryPersonalizationFlow: Bool {
get {
return self.bool(forKey: AppKeys.hasCompletedCategoryPersonalizationFlow.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.hasCompletedCategoryPersonalizationFlow.rawValue)
}
}
public var hasClosedFacebookConnectInActivity: Bool {
get {
return self.object(forKey: AppKeys.closedFacebookConnectInActivity.rawValue) as? Bool ?? false
}
set {
self.set(newValue, forKey: AppKeys.closedFacebookConnectInActivity.rawValue)
}
}
public var hasClosedFindFriendsInActivity: Bool {
get {
return self.object(forKey: AppKeys.closedFindFriendsInActivity.rawValue) as? Bool ?? false
}
set {
self.set(newValue, forKey: AppKeys.closedFindFriendsInActivity.rawValue)
}
}
public var hasSeenAppRating: Bool {
get {
return self.bool(forKey: AppKeys.seenAppRating.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.seenAppRating.rawValue)
}
}
public var hasSeenFavoriteCategoryAlert: Bool {
get {
return self.bool(forKey: AppKeys.hasSeenFavoriteCategoryAlert.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.hasSeenFavoriteCategoryAlert.rawValue)
}
}
public var hasSeenGamesNewsletterPrompt: Bool {
get {
return self.bool(forKey: AppKeys.seenGamesNewsletter.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.seenGamesNewsletter.rawValue)
}
}
public var hasSeenSaveProjectAlert: Bool {
get {
return self.bool(forKey: AppKeys.hasSeenSaveProjectAlert.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.hasSeenSaveProjectAlert.rawValue)
}
}
public var lastSeenActivitySampleId: Int {
get {
return self.integer(forKey: AppKeys.lastSeenActivitySampleId.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.lastSeenActivitySampleId.rawValue)
}
}
public var onboardingCategories: Data? {
get {
return self.data(forKey: AppKeys.onboardingCategories.rawValue)
}
set {
self.set(newValue, forKey: AppKeys.onboardingCategories.rawValue)
}
}
public var remoteConfigFeatureFlags: [String: Bool] {
get {
return self
.object(forKey: AppKeys.remoteConfigFeatureFlags.rawValue) as? [String: Bool] ?? [:]
}
set {
self.set(newValue, forKey: AppKeys.remoteConfigFeatureFlags.rawValue)
}
}
}
extension UserDefaults: KeyValueStoreType {}
extension NSUbiquitousKeyValueStore: KeyValueStoreType {
public func integer(forKey defaultName: String) -> Int {
return Int(longLong(forKey: defaultName))
}
public func set(_ value: Int, forKey defaultName: String) {
return self.set(Int64(value), forKey: defaultName)
}
}
internal class MockKeyValueStore: KeyValueStoreType {
var store: [String: Any] = [:]
func set(_ value: Bool, forKey defaultName: String) {
self.store[defaultName] = value
}
func set(_ value: Int, forKey defaultName: String) {
self.store[defaultName] = value
}
func set(_ value: Any?, forKey key: String) {
self.store[key] = value
}
func bool(forKey defaultName: String) -> Bool {
return self.store[defaultName] as? Bool ?? false
}
func data(forKey defaultName: String) -> Data? {
return self.store[defaultName] as? Data
}
func dictionary(forKey key: String) -> [String: Any]? {
return self.object(forKey: key) as? [String: Any]
}
func integer(forKey defaultName: String) -> Int {
return self.store[defaultName] as? Int ?? 0
}
func object(forKey key: String) -> Any? {
return self.store[key]
}
func string(forKey defaultName: String) -> String? {
return self.store[defaultName] as? String
}
func removeObject(forKey defaultName: String) {
self.set(nil, forKey: defaultName)
}
func synchronize() -> Bool {
return true
}
}