forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSUserDefaults.swift
More file actions
200 lines (178 loc) · 6.49 KB
/
NSUserDefaults.swift
File metadata and controls
200 lines (178 loc) · 6.49 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
//
// NSUserDefaults.swift
// Naterade
//
// Created by Nathan Racklyeft on 8/30/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//
import Foundation
import LoopKit
import HealthKit
extension UserDefaults {
private enum Key: String {
case overrideHistory = "com.loopkit.overrideHistory"
case lastBedtimeQuery = "com.loopkit.Loop.lastBedtimeQuery"
case bedtime = "com.loopkit.Loop.bedtime"
case lastProfileExpirationAlertDate = "com.loopkit.Loop.lastProfileExpirationAlertDate"
case allowDebugFeatures = "com.loopkit.Loop.allowDebugFeatures"
case allowExperimentalFeatures = "com.loopkit.Loop.allowExperimentalFeatures"
case allowSimulators = "com.loopkit.Loop.allowSimulators"
case LastMissedMealNotification = "com.loopkit.Loop.lastMissedMealNotification"
case userRequestedLoopReset = "com.loopkit.Loop.userRequestedLoopReset"
case liveActivity = "com.loopkit.Loop.liveActivity"
}
public static let appGroup = UserDefaults(suiteName: Bundle.main.appGroupSuiteName)
public var legacyBasalRateSchedule: BasalRateSchedule? {
get {
if let rawValue = dictionary(forKey: "com.loudnate.Naterade.BasalRateSchedule") {
return BasalRateSchedule(rawValue: rawValue)
} else {
return nil
}
}
}
public var legacyCarbRatioSchedule: CarbRatioSchedule? {
get {
if let rawValue = dictionary(forKey: "com.loudnate.Naterade.CarbRatioSchedule") {
return CarbRatioSchedule(rawValue: rawValue)
} else {
return nil
}
}
}
public var legacyDefaultRapidActingModel: ExponentialInsulinModelPreset? {
get {
if let rawValue = string(forKey: "com.loopkit.Loop.defaultRapidActingModel") {
return ExponentialInsulinModelPreset(rawValue: rawValue)
}
return nil
}
}
public var legacyLoopSettings: LoopSettings? {
get {
if let rawValue = dictionary(forKey: "com.loopkit.Loop.loopSettings") {
return LoopSettings(rawValue: rawValue)
} else {
return nil
}
}
}
public var legacyInsulinSensitivitySchedule: InsulinSensitivitySchedule? {
get {
if let rawValue = dictionary(forKey: "com.loudnate.Naterade.InsulinSensitivitySchedule") {
return InsulinSensitivitySchedule(rawValue: rawValue)
} else {
return nil
}
}
}
public var overrideHistory: TemporaryScheduleOverrideHistory? {
get {
if let rawValue = object(forKey: Key.overrideHistory.rawValue) as? TemporaryScheduleOverrideHistory.RawValue {
return TemporaryScheduleOverrideHistory(rawValue: rawValue)
} else {
return nil
}
}
set {
set(newValue?.rawValue, forKey: Key.overrideHistory.rawValue)
}
}
public var lastBedtimeQuery: Date? {
get {
return object(forKey: Key.lastBedtimeQuery.rawValue) as? Date
}
set {
set(newValue, forKey: Key.lastBedtimeQuery.rawValue)
}
}
public var bedtime: Date? {
get {
return object(forKey: Key.bedtime.rawValue) as? Date
}
set {
set(newValue, forKey: Key.bedtime.rawValue)
}
}
public var lastProfileExpirationAlertDate: Date? {
get {
return object(forKey: Key.lastProfileExpirationAlertDate.rawValue) as? Date
}
set {
set(newValue, forKey: Key.lastProfileExpirationAlertDate.rawValue)
}
}
public var lastMissedMealNotification: MissedMealNotification? {
get {
let decoder = JSONDecoder()
guard let data = object(forKey: Key.LastMissedMealNotification.rawValue) as? Data else {
return nil
}
return try? decoder.decode(MissedMealNotification.self, from: data)
}
set {
do {
if let newValue = newValue {
let encoder = JSONEncoder()
let data = try encoder.encode(newValue)
set(data, forKey: Key.LastMissedMealNotification.rawValue)
} else {
set(nil, forKey: Key.LastMissedMealNotification.rawValue)
}
} catch {
assertionFailure("Unable to encode MissedMealNotification")
}
}
}
public var allowDebugFeatures: Bool {
get {
bool(forKey: Key.allowDebugFeatures.rawValue)
}
set {
set(newValue, forKey: Key.allowDebugFeatures.rawValue)
}
}
public var allowExperimentalFeatures: Bool {
return bool(forKey: Key.allowExperimentalFeatures.rawValue)
}
public var allowSimulators: Bool {
return bool(forKey: Key.allowSimulators.rawValue)
}
public var userRequestedLoopReset: Bool {
get {
bool(forKey: Key.userRequestedLoopReset.rawValue)
}
set {
setValue(newValue, forKey: Key.userRequestedLoopReset.rawValue)
}
}
public var liveActivity: LiveActivitySettings? {
get {
let decoder = JSONDecoder()
guard let data = object(forKey: Key.liveActivity.rawValue) as? Data else {
return nil
}
return try? decoder.decode(LiveActivitySettings.self, from: data)
}
set {
do {
if let newValue = newValue {
let encoder = JSONEncoder()
let data = try encoder.encode(newValue)
set(data, forKey: Key.liveActivity.rawValue)
} else {
set(nil, forKey: Key.liveActivity.rawValue)
}
} catch {
assertionFailure("Unable to encode MissedMealNotification")
}
}
}
public func removeLegacyLoopSettings() {
removeObject(forKey: "com.loudnate.Naterade.BasalRateSchedule")
removeObject(forKey: "com.loudnate.Naterade.CarbRatioSchedule")
removeObject(forKey: "com.loudnate.Naterade.InsulinSensitivitySchedule")
removeObject(forKey: "com.loopkit.Loop.defaultRapidActingModel")
removeObject(forKey: "com.loopkit.Loop.loopSettings")
}
}