forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockService.swift
More file actions
184 lines (145 loc) · 6.47 KB
/
MockService.swift
File metadata and controls
184 lines (145 loc) · 6.47 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
//
// MockService.swift
// MockKit
//
// Created by Darin Krauss on 5/17/19.
// Copyright © 2019 LoopKit Authors. All rights reserved.
//
import os.log
import Foundation
import LoopKit
public final class MockService: Service {
public static let pluginIdentifier = "MockService"
public static let localizedTitle = "Simulator"
public weak var stateDelegate: StatefulPluggableDelegate?
public weak var serviceDelegate: ServiceDelegate?
public var remoteData: Bool
public var logging: Bool
public var analytics: Bool
public let maxHistoryItems = 1000
private var lockedHistory = Locked<[String]>([])
public var history: [String] {
lockedHistory.value
}
private var dateFormatter = ISO8601DateFormatter()
public init() {
self.remoteData = true
self.logging = true
self.analytics = true
}
public init?(rawState: RawStateValue) {
self.remoteData = rawState["remoteData"] as? Bool ?? false
self.logging = rawState["logging"] as? Bool ?? false
self.analytics = rawState["analytics"] as? Bool ?? false
}
public var rawState: RawStateValue {
var rawValue: RawStateValue = [:]
rawValue["remoteData"] = remoteData
rawValue["logging"] = logging
rawValue["analytics"] = analytics
return rawValue
}
public let isOnboarded = true // No distinction between created and onboarded
public func completeCreate() {}
public func completeUpdate() {
stateDelegate?.pluginDidUpdateState(self)
}
public func completeDelete() {
stateDelegate?.pluginWantsDeletion(self)
}
public func clearHistory() {
lockedHistory.value = []
}
private func record(_ message: String) {
let timestamp = self.dateFormatter.string(from: Date())
lockedHistory.mutate { history in
history.append("\(timestamp): \(message)")
if history.count > self.maxHistoryItems {
history.removeFirst(history.count - self.maxHistoryItems)
}
}
}
}
extension MockService: AnalyticsService {
public func recordIdentify(_ property: String, array: [String]) {
record("[AnalyticsService] Identify: \(property) \(array)")
}
public func recordAnalyticsEvent(_ name: String, withProperties properties: [AnyHashable: Any]?, outOfSession: Bool) {
if analytics {
record("[AnalyticsService] \(name) \(String(describing: properties)) \(outOfSession)")
}
}
public func recordIdentify(_ property: String, value: String) {
record("[AnalyticsService] Identify: \(property) \(value)")
}
}
extension MockService: LoggingService {
public func log(_ message: StaticString, subsystem: String, category: String, type: OSLogType, _ args: [CVarArg]) {
if logging {
// Since this is only stored in memory, do not worry about public/private qualifiers
let messageWithoutQualifiers = message.description.replacingOccurrences(of: "%{public}", with: "%").replacingOccurrences(of: "%{private}", with: "%")
let messageWithArguments = String(format: messageWithoutQualifiers, arguments: args)
record("[LoggingService] \(messageWithArguments)")
}
}
}
extension MockService: RemoteDataService {
public func uploadTemporaryOverrideData(updated: [TemporaryScheduleOverride], deleted: [TemporaryScheduleOverride], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload temporary override data (updated: \(updated.count), deleted: \(deleted.count))")
}
completion(.success(false))
}
public func uploadAlertData(_ stored: [SyncAlertObject], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload alert data (stored: \(stored.count))")
}
completion(.success(false))
}
public func uploadCarbData(created: [SyncCarbObject], updated: [SyncCarbObject], deleted: [SyncCarbObject], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload carb data (created: \(created.count), updated: \(updated.count), deleted: \(deleted.count))")
}
completion(.success(false))
}
public func uploadDoseData(created: [DoseEntry], deleted: [DoseEntry], completion: @escaping (_ result: Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload dose data (created: \(created.count), deleted: \(deleted.count))")
}
completion(.success(false))
}
public func uploadDosingDecisionData(_ stored: [StoredDosingDecision], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
let warned = stored.filter { !$0.warnings.isEmpty }
let errored = stored.filter { !$0.errors.isEmpty }
record("[RemoteDataService] Upload dosing decision data (stored: \(stored.count), warned: \(warned.count), errored: \(errored.count))")
}
completion(.success(false))
}
public func uploadGlucoseData(_ stored: [StoredGlucoseSample], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload glucose data (stored: \(stored.count))")
}
completion(.success(false))
}
public func uploadPumpEventData(_ stored: [PersistedPumpEvent], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload pump event data (stored: \(stored.count))")
}
completion(.success(false))
}
public func uploadSettingsData(_ stored: [StoredSettings], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload settings data (stored: \(stored.count))")
}
completion(.success(false))
}
public func uploadCgmEventData(_ stored: [LoopKit.PersistedCgmEvent], completion: @escaping (Result<Bool, Error>) -> Void) {
if remoteData {
record("[RemoteDataService] Upload cgm event data (stored: \(stored.count))")
}
completion(.success(false))
}
public func remoteNotificationWasReceived(_ notification: [String: AnyObject]) async throws {
}
}