-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataType.swift
More file actions
249 lines (213 loc) · 6.32 KB
/
DataType.swift
File metadata and controls
249 lines (213 loc) · 6.32 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// This source file is part of the NeutroFeverGuard based on the Stanford Spezi Template Application project
//
// SPDX-FileCopyrightText: 2025 Stanford University
//
// SPDX-License-Identifier: MIT
//
import Foundation
import HealthKit
/*
Helper Function: check the date is not in future
*/
func isValidDate(_ date: Date) throws {
guard date <= Date() else {
throw DataError.invalidDate
}
}
enum LabTestType: String, CaseIterable, Codable {
case whiteBloodCell = "White Blood Cell Count"
case hemoglobin = "Hemoglobin"
case plateletCount = "Platelet Count"
case neutrophils = "% Neutrophils"
case lymphocytes = "% Lymphocytes"
case monocytes = "% Monocytes"
case eosinophils = "% Eosinophils"
case basophils = "% Basophils"
case blasts = "% Blasts"
}
enum TemperatureUnit: String {
case celsius = "Celsius"
case fahrenheit = "Fahrenheit"
var hkUnit: HKUnit {
switch self {
case .celsius:
return HKUnit.degreeCelsius()
case .fahrenheit:
return HKUnit.degreeFahrenheit()
}
}
}
/*
Heart Rate: date + time measured, and rate in BPM
*/
struct HeartRateEntry {
static let healthKitType = HKQuantityType(.heartRate)
static let unit = HKUnit.count().unitDivided(by: HKUnit.minute())
let date: Date
let bpm: Double
init(date: Date, bpm: Double) throws {
try isValidDate(date)
self.date = date
self.bpm = bpm
}
}
/*
Temperature: date + time measured, and temperature in either degrees celsius or
*/
struct TemperatureEntry {
static let healthKitType = HKQuantityType(.bodyTemperature)
let date: Date
let value: Double
let unit: TemperatureUnit
init(date: Date, value: Double, unit: TemperatureUnit) throws {
try isValidDate(date)
self.date = date
self.value = value
self.unit = unit
}
}
/*
Oxygen saturation: date + time measured, and oxygen saturation in %.
*/
struct OxygenSaturationEntry {
static let healthKitType = HKQuantityType(.oxygenSaturation)
static let unit = HKUnit.percent()
let date: Date
let percentage: Double
init(date: Date, percentage: Double) throws {
try isValidDate(date)
guard percentage >= 0 && percentage <= 100 else {
throw DataError.invalidPercentage
}
self.date = date
self.percentage = percentage
}
}
/*
Blood Pressure: date + time measured, and two pressures (systolic and diastolic) in mmHg.
*/
struct BloodPressureEntry {
static let systolicType = HKQuantityType(.bloodPressureSystolic)
static let diastolicType = HKQuantityType(.bloodPressureDiastolic)
static let unit = HKUnit.millimeterOfMercury()
let date: Date
let systolic: Double
let diastolic: Double
init(date: Date, systolic: Double, diastolic: Double) throws {
try isValidDate(date)
guard systolic >= 0, diastolic >= 0 else {
throw DataError.invalidBloodPressure
}
self.date = date
self.systolic = systolic
self.diastolic = diastolic
}
}
/*
Lab values:
- Date and time of lab measured
- Name of lab: white blood cell count, hemoglobin, platelet count, %neutrophils, %lymphocytes, %monocytes, %eosinophils, %basophils, %blasts
- Lab values: include the number associated with the lab name above
*/
struct LabEntry: Codable, Equatable {
var date: Date
var values: [LabTestType: Double]
init(date: Date, values: [LabTestType: Double]) throws {
try isValidDate(date)
self.date = date
self.values = values
}
}
/*
Medication administrations for chemotherapy:
- Start date and time of administration
- Optional end date and time of administration
- Medication name
- Medication dose
*/
enum DoseUnit: String, CaseIterable, Codable {
case mgUnit = "mg"
case mcgUnit = "mcg"
case gUnit = "g"
case mLUnit = "mL"
case percentUnit = "%"
}
struct MedicationEntry: Codable {
var date: Date
var name: String
var doseValue: Double
var doseUnit: DoseUnit
init(date: Date, name: String, doseValue: Double, doseUnit: DoseUnit) throws {
try isValidDate(date)
self.date = date
self.name = name
self.doseValue = doseValue
self.doseUnit = doseUnit
}
}
enum Symptom: String, CaseIterable, Codable {
case nausea = "Nausea"
case vomiting = "Vomiting"
case diarrhea = "Diarrhea"
case chills = "Chills"
case cough = "Cough"
case pain = "Pain"
}
struct SymptomEntry: Codable {
// periphery:ignore
var date: Date
// periphery:ignore
var symptoms: [Symptom: Int] // Maps symptoms to their severity (1-10)
init(date: Date, symptoms: [Symptom: Int]) throws {
try isValidDate(date)
// Validate that all severity ratings are between 1 and 10
for (_, severity) in symptoms {
guard severity >= 1 && severity <= 10 else {
throw DataError.invalidSeverity
}
}
self.date = date
self.symptoms = symptoms
}
}
struct MasccEntry: Codable {
var date: Date
// periphery:ignore
var symptoms: [MasccSymptom]
init(date: Date, symptoms: [MasccSymptom]) throws {
try MasccEntry.isValidDate(date)
self.date = date
self.symptoms = symptoms
}
static func isValidDate(_ date: Date) throws {
let calendar = Calendar.current
let currentDate = Date()
// Prevent future dates
guard date <= currentDate else {
throw DataError.invalidDate
}
}
}
enum MasccSymptom: String, Codable, CaseIterable {
case noHypotension = "No Hypotension"
case noCOPD = "No COPD"
case solidTumor = "Solid Tumor"
case noDehydration = "No Dehydration"
case mildSymptoms = "Mild Symptoms"
case moderateSymptoms = "Moderate Symptoms"
case severeSymptoms = "Severe Symptoms"
case ageUnder60 = "Age Under 60"
var score: Int {
switch self {
case .noHypotension: return 5
case .noCOPD: return 4
case .solidTumor: return 4
case .noDehydration: return 3
case .mildSymptoms: return 5
case .moderateSymptoms: return 3
case .severeSymptoms: return 0
case .ageUnder60: return 2
}
}
}