Skip to content

Commit 57e6350

Browse files
authored
Merge pull request #304 from loopandlearn/openaps-predictions
Trio predictions
2 parents b36e5cc + 610cb13 commit 57e6350

File tree

7 files changed

+458
-239
lines changed

7 files changed

+458
-239
lines changed

LoopFollow.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
DD493AE52ACF2383009A6922 /* Treatments.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE42ACF2383009A6922 /* Treatments.swift */; };
2222
DD493AE72ACF23CF009A6922 /* DeviceStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE62ACF23CF009A6922 /* DeviceStatus.swift */; };
2323
DD493AE92ACF2445009A6922 /* BGData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE82ACF2445009A6922 /* BGData.swift */; };
24+
DD608A082C1F584900F91132 /* DeviceStatusLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */; };
25+
DD6A935E2BFA6FA2003FFB8E /* DeviceStatusOpenAPS.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6A935D2BFA6FA2003FFB8E /* DeviceStatusOpenAPS.swift */; };
2426
DD7E19842ACDA50C00DBD158 /* Overrides.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD7E19832ACDA50C00DBD158 /* Overrides.swift */; };
2527
DD7E19862ACDA59700DBD158 /* BGCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD7E19852ACDA59700DBD158 /* BGCheck.swift */; };
2628
DD7E19882ACDA5DA00DBD158 /* Notes.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD7E19872ACDA5DA00DBD158 /* Notes.swift */; };
@@ -211,6 +213,8 @@
211213
DD493AE42ACF2383009A6922 /* Treatments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Treatments.swift; sourceTree = "<group>"; };
212214
DD493AE62ACF23CF009A6922 /* DeviceStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatus.swift; sourceTree = "<group>"; };
213215
DD493AE82ACF2445009A6922 /* BGData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGData.swift; sourceTree = "<group>"; };
216+
DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatusLoop.swift; sourceTree = "<group>"; };
217+
DD6A935D2BFA6FA2003FFB8E /* DeviceStatusOpenAPS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatusOpenAPS.swift; sourceTree = "<group>"; };
214218
DD7E19832ACDA50C00DBD158 /* Overrides.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Overrides.swift; sourceTree = "<group>"; };
215219
DD7E19852ACDA59700DBD158 /* BGCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGCheck.swift; sourceTree = "<group>"; };
216220
DD7E19872ACDA5DA00DBD158 /* Notes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notes.swift; sourceTree = "<group>"; };
@@ -450,6 +454,8 @@
450454
DD493AE42ACF2383009A6922 /* Treatments.swift */,
451455
DD493AE62ACF23CF009A6922 /* DeviceStatus.swift */,
452456
DD493AE82ACF2445009A6922 /* BGData.swift */,
457+
DD6A935D2BFA6FA2003FFB8E /* DeviceStatusOpenAPS.swift */,
458+
DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */,
453459
);
454460
path = Nightscout;
455461
sourceTree = "<group>";
@@ -1013,6 +1019,7 @@
10131019
DDF9676E2AD08C6E00C5EB95 /* SiteChange.swift in Sources */,
10141020
DDCF979C24C14EFB002C9752 /* AdvancedSettingsViewController.swift in Sources */,
10151021
FC97881C2485969B00A7906C /* MainViewController.swift in Sources */,
1022+
DD6A935E2BFA6FA2003FFB8E /* DeviceStatusOpenAPS.swift in Sources */,
10161023
DD493AD52ACF2109009A6922 /* ResumePump.swift in Sources */,
10171024
DDCF979624C1443C002C9752 /* GeneralSettingsViewController.swift in Sources */,
10181025
FCC0FAC224922A22003E610E /* DictionaryKeyPath.swift in Sources */,
@@ -1021,6 +1028,7 @@
10211028
DD493AD92ACF2171009A6922 /* Carbs.swift in Sources */,
10221029
DD493AE92ACF2445009A6922 /* BGData.swift in Sources */,
10231030
FCC6886B24898FD800A0279D /* ObservationToken.swift in Sources */,
1031+
DD608A082C1F584900F91132 /* DeviceStatusLoop.swift in Sources */,
10241032
DD98F54424BCEFEE0007425A /* ShareClientExtension.swift in Sources */,
10251033
DD91E4DD2BDEC3F8002D9E97 /* GlucoseConversion.swift in Sources */,
10261034
FCC6886D2489909D00A0279D /* AnyConvertible.swift in Sources */,

LoopFollow/Application/Base.lproj/Main.storyboard

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,6 @@
529529
<view key="view" contentMode="scaleToFill" id="ljg-dW-5uR">
530530
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
531531
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
532-
<subviews>
533-
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6gS-Ff-qPd">
534-
<rect key="frame" x="0.0" y="44" width="414" height="769"/>
535-
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
536-
<viewLayoutGuide key="contentLayoutGuide" id="1wh-Gg-Auv"/>
537-
<viewLayoutGuide key="frameLayoutGuide" id="lqA-iJ-wdZ"/>
538-
</scrollView>
539-
</subviews>
540532
<viewLayoutGuide key="safeArea" id="aVb-ap-Pwt"/>
541533
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
542534
</view>

LoopFollow/Controllers/Graphs.swift

Lines changed: 229 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,102 @@ extension MainViewController {
254254
lineNote.axisDependency = YAxis.AxisDependency.right
255255
lineNote.valueFormatter = ChartYDataValueFormatter()
256256
lineNote.drawValuesEnabled = false
257+
258+
// Setup COB Prediction line details
259+
let COBpredictionChartEntry = [ChartDataEntry]()
260+
let COBlinePrediction = LineChartDataSet(entries:COBpredictionChartEntry, label: "")
261+
COBlinePrediction.circleRadius = CGFloat(globalVariables.dotBG)
262+
COBlinePrediction.circleColors = [NSUIColor.systemPurple]
263+
COBlinePrediction.colors = [NSUIColor.systemPurple]
264+
COBlinePrediction.drawCircleHoleEnabled = false
265+
COBlinePrediction.axisDependency = YAxis.AxisDependency.right
266+
COBlinePrediction.highlightEnabled = true
267+
COBlinePrediction.drawValuesEnabled = false
268+
269+
if UserDefaultsRepository.showLines.value {
270+
COBlinePrediction.lineWidth = 2
271+
} else {
272+
COBlinePrediction.lineWidth = 0
273+
}
274+
if UserDefaultsRepository.showDots.value {
275+
COBlinePrediction.drawCirclesEnabled = true
276+
} else {
277+
COBlinePrediction.drawCirclesEnabled = false
278+
}
279+
COBlinePrediction.setDrawHighlightIndicators(false)
280+
COBlinePrediction.valueFont.withSize(50)
281+
282+
// Setup IOB Prediction line details
283+
let IOBpredictionChartEntry = [ChartDataEntry]()
284+
let IOBlinePrediction = LineChartDataSet(entries:IOBpredictionChartEntry, label: "")
285+
IOBlinePrediction.circleRadius = CGFloat(globalVariables.dotBG)
286+
IOBlinePrediction.circleColors = [NSUIColor.systemPurple]
287+
IOBlinePrediction.colors = [NSUIColor.systemPurple]
288+
IOBlinePrediction.drawCircleHoleEnabled = false
289+
IOBlinePrediction.axisDependency = YAxis.AxisDependency.right
290+
IOBlinePrediction.highlightEnabled = true
291+
IOBlinePrediction.drawValuesEnabled = false
292+
293+
if UserDefaultsRepository.showLines.value {
294+
IOBlinePrediction.lineWidth = 2
295+
} else {
296+
IOBlinePrediction.lineWidth = 0
297+
}
298+
if UserDefaultsRepository.showDots.value {
299+
IOBlinePrediction.drawCirclesEnabled = true
300+
} else {
301+
IOBlinePrediction.drawCirclesEnabled = false
302+
}
303+
IOBlinePrediction.setDrawHighlightIndicators(false)
304+
IOBlinePrediction.valueFont.withSize(50)
305+
306+
// Setup UAM Prediction line details
307+
let UAMpredictionChartEntry = [ChartDataEntry]()
308+
let UAMlinePrediction = LineChartDataSet(entries:UAMpredictionChartEntry, label: "")
309+
UAMlinePrediction.circleRadius = CGFloat(globalVariables.dotBG)
310+
UAMlinePrediction.circleColors = [NSUIColor.systemPurple]
311+
UAMlinePrediction.colors = [NSUIColor.systemPurple]
312+
UAMlinePrediction.drawCircleHoleEnabled = false
313+
UAMlinePrediction.axisDependency = YAxis.AxisDependency.right
314+
UAMlinePrediction.highlightEnabled = true
315+
UAMlinePrediction.drawValuesEnabled = false
316+
317+
if UserDefaultsRepository.showLines.value {
318+
UAMlinePrediction.lineWidth = 2
319+
} else {
320+
UAMlinePrediction.lineWidth = 0
321+
}
322+
if UserDefaultsRepository.showDots.value {
323+
UAMlinePrediction.drawCirclesEnabled = true
324+
} else {
325+
UAMlinePrediction.drawCirclesEnabled = false
326+
}
327+
linePrediction.setDrawHighlightIndicators(false)
328+
linePrediction.valueFont.withSize(50)
329+
330+
// Setup ZT Prediction line details
331+
let ZTpredictionChartEntry = [ChartDataEntry]()
332+
let ZTlinePrediction = LineChartDataSet(entries:ZTpredictionChartEntry, label: "")
333+
ZTlinePrediction.circleRadius = CGFloat(globalVariables.dotBG)
334+
ZTlinePrediction.circleColors = [NSUIColor.systemPurple]
335+
ZTlinePrediction.colors = [NSUIColor.systemPurple]
336+
ZTlinePrediction.drawCircleHoleEnabled = false
337+
ZTlinePrediction.axisDependency = YAxis.AxisDependency.right
338+
ZTlinePrediction.highlightEnabled = true
339+
ZTlinePrediction.drawValuesEnabled = false
340+
341+
if UserDefaultsRepository.showLines.value {
342+
ZTlinePrediction.lineWidth = 2
343+
} else {
344+
ZTlinePrediction.lineWidth = 0
345+
}
346+
if UserDefaultsRepository.showDots.value {
347+
ZTlinePrediction.drawCirclesEnabled = true
348+
} else {
349+
ZTlinePrediction.drawCirclesEnabled = false
350+
}
351+
ZTlinePrediction.setDrawHighlightIndicators(false)
352+
ZTlinePrediction.valueFont.withSize(50)
257353

258354
// Setup the chart data of all lines
259355
let data = LineChartData()
@@ -270,7 +366,11 @@ extension MainViewController {
270366
data.append(lineResume) // Dataset 9
271367
data.append(lineSensor) // Dataset 10
272368
data.append(lineNote) // Dataset 11
273-
369+
data.append(ZTlinePrediction) // Dataset 12
370+
data.append(IOBlinePrediction) // Dataset 13
371+
data.append(COBlinePrediction) // Dataset 14
372+
data.append(UAMlinePrediction) // Dataset 15
373+
274374
data.setValueFont(UIFont.systemFont(ofSize: 12))
275375

276376
// Add marker popups for bolus and carbs
@@ -1137,6 +1237,58 @@ extension MainViewController {
11371237
lineNote.axisDependency = YAxis.AxisDependency.right
11381238
lineNote.valueFormatter = ChartYDataValueFormatter()
11391239
lineNote.drawValuesEnabled = false
1240+
1241+
// Setup COB Prediction line details
1242+
var COBpredictionChartEntry = [ChartDataEntry]()
1243+
let COBlinePrediction = LineChartDataSet(entries:COBpredictionChartEntry, label: "")
1244+
COBlinePrediction.drawCirclesEnabled = false
1245+
COBlinePrediction.setColor(NSUIColor.systemPurple)
1246+
COBlinePrediction.highlightEnabled = true
1247+
COBlinePrediction.drawHorizontalHighlightIndicatorEnabled = false
1248+
COBlinePrediction.drawVerticalHighlightIndicatorEnabled = false
1249+
COBlinePrediction.highlightColor = NSUIColor.label
1250+
COBlinePrediction.drawValuesEnabled = false
1251+
COBlinePrediction.lineWidth = 1.5
1252+
COBlinePrediction.axisDependency = YAxis.AxisDependency.right
1253+
1254+
// Setup IOB Prediction line details
1255+
var IOBpredictionChartEntry = [ChartDataEntry]()
1256+
let IOBlinePrediction = LineChartDataSet(entries:IOBpredictionChartEntry, label: "")
1257+
IOBlinePrediction.drawCirclesEnabled = false
1258+
IOBlinePrediction.setColor(NSUIColor.systemPurple)
1259+
IOBlinePrediction.highlightEnabled = true
1260+
IOBlinePrediction.drawHorizontalHighlightIndicatorEnabled = false
1261+
IOBlinePrediction.drawVerticalHighlightIndicatorEnabled = false
1262+
IOBlinePrediction.highlightColor = NSUIColor.label
1263+
IOBlinePrediction.drawValuesEnabled = false
1264+
IOBlinePrediction.lineWidth = 1.5
1265+
IOBlinePrediction.axisDependency = YAxis.AxisDependency.right
1266+
1267+
// Setup UAM Prediction line details
1268+
var UAMpredictionChartEntry = [ChartDataEntry]()
1269+
let UAMlinePrediction = LineChartDataSet(entries:UAMpredictionChartEntry, label: "")
1270+
UAMlinePrediction.drawCirclesEnabled = false
1271+
UAMlinePrediction.setColor(NSUIColor.systemPurple)
1272+
UAMlinePrediction.highlightEnabled = true
1273+
UAMlinePrediction.drawHorizontalHighlightIndicatorEnabled = false
1274+
UAMlinePrediction.drawVerticalHighlightIndicatorEnabled = false
1275+
UAMlinePrediction.highlightColor = NSUIColor.label
1276+
UAMlinePrediction.drawValuesEnabled = false
1277+
UAMlinePrediction.lineWidth = 1.5
1278+
UAMlinePrediction.axisDependency = YAxis.AxisDependency.right
1279+
1280+
// Setup ZT Prediction line details
1281+
var ZTpredictionChartEntry = [ChartDataEntry]()
1282+
let ZTlinePrediction = LineChartDataSet(entries:ZTpredictionChartEntry, label: "")
1283+
ZTlinePrediction.drawCirclesEnabled = false
1284+
ZTlinePrediction.setColor(NSUIColor.systemPurple)
1285+
ZTlinePrediction.highlightEnabled = true
1286+
ZTlinePrediction.drawHorizontalHighlightIndicatorEnabled = false
1287+
ZTlinePrediction.drawVerticalHighlightIndicatorEnabled = false
1288+
ZTlinePrediction.highlightColor = NSUIColor.label
1289+
ZTlinePrediction.drawValuesEnabled = false
1290+
ZTlinePrediction.lineWidth = 1.5
1291+
ZTlinePrediction.axisDependency = YAxis.AxisDependency.right
11401292

11411293
// Setup the chart data of all lines
11421294
let data = LineChartData()
@@ -1152,7 +1304,11 @@ extension MainViewController {
11521304
data.append(lineResume) // Dataset 9
11531305
data.append(lineSensor) // Dataset 10
11541306
data.append(lineNote) // Dataset 11
1155-
1307+
data.append(ZTlinePrediction) // Dataset 12
1308+
data.append(IOBlinePrediction) // Dataset 13
1309+
data.append(COBlinePrediction) // Dataset 14
1310+
data.append(UAMlinePrediction) // Dataset 15
1311+
11561312
BGChartFull.highlightPerDragEnabled = true
11571313
BGChartFull.leftAxis.enabled = false
11581314
BGChartFull.leftAxis.axisMaximum = maxBasal
@@ -1237,21 +1393,86 @@ extension MainViewController {
12371393
}
12381394
}
12391395

1240-
func formatPillText(line1: String, time: TimeInterval) -> String {
1396+
func formatPillText(line1: String, time: TimeInterval, line2: String? = nil) -> String {
12411397
let dateFormatter = DateFormatter()
1242-
//let timezoneOffset = TimeZone.current.secondsFromGMT()
1243-
//let epochTimezoneOffset = value + Double(timezoneOffset)
12441398
if dateTimeUtils.is24Hour() {
12451399
dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
12461400
} else {
12471401
dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
12481402
}
12491403

1250-
//let date = Date(timeIntervalSince1970: epochTimezoneOffset)
12511404
let date = Date(timeIntervalSince1970: time)
12521405
let formattedDate = dateFormatter.string(from: date)
1253-
1254-
return line1 + "\r\n" + formattedDate
1406+
1407+
if let line2 = line2 {
1408+
return line1 + "\r\n" + line2 + "\r\n" + formattedDate
1409+
} else {
1410+
return line1 + "\r\n" + formattedDate
1411+
}
12551412
}
12561413

1414+
func updatePredictionGraphGeneric(
1415+
dataIndex: Int,
1416+
predictionData: [ShareGlucoseData],
1417+
chartLabel: String,
1418+
color: UIColor
1419+
) {
1420+
let mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
1421+
let smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
1422+
mainChart.clear()
1423+
smallChart.clear()
1424+
1425+
var colors = [NSUIColor]()
1426+
let maxBGOffset: Float = 20
1427+
1428+
for i in 0..<predictionData.count {
1429+
let predictionVal = Double(predictionData[i].sgv)
1430+
if Float(predictionVal) > topBG - maxBGOffset {
1431+
topBG = Float(predictionVal) + maxBGOffset
1432+
}
1433+
1434+
if i == 0 {
1435+
if UserDefaultsRepository.showDots.value {
1436+
colors.append((color).withAlphaComponent(0.0))
1437+
} else {
1438+
colors.append((color).withAlphaComponent(1.0))
1439+
}
1440+
} else {
1441+
colors.append(color)
1442+
}
1443+
1444+
let value = ChartDataEntry(
1445+
x: predictionData[i].date,
1446+
y: predictionVal,
1447+
data: formatPillText(
1448+
line1: chartLabel,
1449+
time: predictionData[i].date,
1450+
line2: bgUnits.toDisplayUnits(String(predictionVal))
1451+
)
1452+
)
1453+
mainChart.addEntry(value)
1454+
smallChart.addEntry(value)
1455+
}
1456+
1457+
smallChart.circleColors.removeAll()
1458+
smallChart.colors.removeAll()
1459+
mainChart.colors.removeAll()
1460+
mainChart.circleColors.removeAll()
1461+
if colors.count > 0 {
1462+
for color in colors {
1463+
mainChart.addColor(color)
1464+
mainChart.circleColors.append(color)
1465+
smallChart.addColor(color)
1466+
smallChart.circleColors.append(color)
1467+
}
1468+
}
1469+
1470+
BGChart.rightAxis.axisMaximum = Double(topBG)
1471+
BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
1472+
BGChart.data?.notifyDataChanged()
1473+
BGChart.notifyDataSetChanged()
1474+
BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
1475+
BGChartFull.data?.notifyDataChanged()
1476+
BGChartFull.notifyDataSetChanged()
1477+
}
12571478
}

0 commit comments

Comments
 (0)