-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyValueStoreType.swift
164 lines (137 loc) 路 4.97 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
//
// KeyValueStoreType.swift
// Facestar
//
// Created by JohnP on 2/22/17.
// Copyright 漏 2017 JohnP. All rights reserved.
//
import Foundation
public enum AppKeys: String {
case closedFacebookConnectInActivity = "com.facestar.KeyValueStoreType.closedFacebookConnectInActivity"
case closedFindFriendsInActivity = "com.facestar.KeyValueStoreType.closedFindFriendsInActivity"
case favoriteCategoryIds = "favorite_category_ids"
case hasSeenFavoriteCategoryAlert = "com.facestar.KeyValueStoreType.hasSeenFavoriteCategoryAlert"
case lastSeenActivitySampleId = "com.facestar.KeyValueStoreType.lastSeenActivitySampleId"
case seenAppRating = "com.facestar.KeyValueStoreType.hasSeenAppRating"
case seenGamesNewsletter = "com.facestar.KeyValueStoreType.hasSeenGamesNewsletter"
}
public protocol KeyValueStoreType: class {
func setBool(_ bool: Bool, forKey key: String)
func setInteger(_ int: Int, forKey key: String)
func set(_ value: Any?, forKey defaultName: String)
func boolForKey(_ key: String) -> Bool
func dictionary(forKey defaultName: String) -> [String : Any]?
func integerForKey(_ key: String) -> Int
func object(forKey defaultName: String) -> Any?
func stringForKey(_ key: String) -> String?
func synchronize() -> Bool
func removeObjectForKey(_ key: String)
var favoriteCategoryIds: [Int] { get set }
var hasClosedFacebookConnectInActivity: Bool { get set }
var hasClosedFindFriendsInActivity: Bool { get set }
var hasSeenAppRating: Bool { get set }
var hasSeenFavoriteCategoryAlert: Bool { get set }
var hasSeenGamesNewsletterPrompt: Bool { get set }
var lastSeenActivitySampleId: Int { 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 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.boolForKey(AppKeys.seenAppRating.rawValue)
}
set {
self.setBool(newValue, forKey: AppKeys.seenAppRating.rawValue)
}
}
public var hasSeenFavoriteCategoryAlert: Bool {
get {
return self.boolForKey(AppKeys.hasSeenFavoriteCategoryAlert.rawValue)
}
set {
self.setBool(newValue, forKey: AppKeys.hasSeenFavoriteCategoryAlert.rawValue)
}
}
public var hasSeenGamesNewsletterPrompt: Bool {
get {
return self.boolForKey(AppKeys.seenGamesNewsletter.rawValue)
}
set {
self.setBool(newValue, forKey: AppKeys.seenGamesNewsletter.rawValue)
}
}
public var lastSeenActivitySampleId: Int {
get {
return self.integerForKey(AppKeys.lastSeenActivitySampleId.rawValue)
}
set {
self.setInteger(newValue, forKey: AppKeys.lastSeenActivitySampleId.rawValue)
}
}
}
extension UserDefaults: KeyValueStoreType {
}
extension NSUbiquitousKeyValueStore: KeyValueStoreType {
public func integerForKey(_ key: String) -> Int {
return Int(longLong(forKey: key))
}
public func setInteger(_ int: Int, forKey key: String) {
return set(Int64(int), forKey: key)
}
}
internal class MockKeyValueStore: KeyValueStoreType {
var store: [String:Any] = [:]
func setBool(_ bool: Bool, forKey key: String) {
self.store[key] = bool
}
func setInteger(_ int: Int, forKey key: String) {
self.store[key] = int
}
func set(_ value: Any?, forKey key: String) {
self.store[key] = value
}
func boolForKey(_ key: String) -> Bool {
return self.store[key] as? Bool ?? false
}
func integerForKey(_ key: String) -> Int {
return self.store[key] as? Int ?? 0
}
func object(forKey key: String) -> Any? {
return self.store[key]
}
func stringForKey(_ key: String) -> String? {
return self.object(forKey: key) as? String
}
func dictionary(forKey key: String) -> [String:Any]? {
return self.object(forKey: key) as? [String:Any]
}
func removeObjectForKey(_ key: String) {
self.set(nil, forKey: key)
}
func synchronize() -> Bool {
return true
}
}