|
|
@@ -220,7 +220,7 @@ extension MainChartView {
|
|
|
/// temp targets
|
|
|
ForEach(ChartTempTargets, id: \.self) { tt in
|
|
|
BarMark(
|
|
|
- xStart:.value("Time", tt.start),
|
|
|
+ xStart: .value("Time", tt.start),
|
|
|
xEnd: .value("Time", tt.end),
|
|
|
y: .value("Value", tt.amount)
|
|
|
)
|
|
|
@@ -468,8 +468,8 @@ extension MainChartView {
|
|
|
|
|
|
// MARK: Calculations
|
|
|
|
|
|
-///calculates the glucose value thats the nearest to parameter 'time'
|
|
|
-///if time is later than all the arrays values return the last element of BloodGlucose
|
|
|
+/// calculates the glucose value thats the nearest to parameter 'time'
|
|
|
+/// if time is later than all the arrays values return the last element of BloodGlucose
|
|
|
extension MainChartView {
|
|
|
private func timeToNearestGlucose(time: TimeInterval) -> BloodGlucose {
|
|
|
var nextIndex = 0
|
|
|
@@ -496,14 +496,14 @@ extension MainChartView {
|
|
|
private func fullWidth(viewWidth: CGFloat) -> CGFloat {
|
|
|
viewWidth * CGFloat(hours) / CGFloat(min(max(screenHours, 2), 24))
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private func calculateCarbs() {
|
|
|
var calculatedCarbs: [Carb] = []
|
|
|
-
|
|
|
- ///check if carbs are not fpus before adding them to the chart
|
|
|
- ///this solves the problem of a first CARB entry with the amount of the single fpu entries that was made at current time when adding ONLY fpus
|
|
|
+
|
|
|
+ /// check if carbs are not fpus before adding them to the chart
|
|
|
+ /// this solves the problem of a first CARB entry with the amount of the single fpu entries that was made at current time when adding ONLY fpus
|
|
|
let realCarbs = carbs.filter { !($0.isFPU ?? false) }
|
|
|
-
|
|
|
+
|
|
|
realCarbs.forEach { carb in
|
|
|
let bg = timeToNearestGlucose(time: carb.createdAt.timeIntervalSince1970)
|
|
|
calculatedCarbs.append(Carb(amount: carb.carbs, timestamp: carb.createdAt, nearestGlucose: bg))
|
|
|
@@ -513,68 +513,68 @@ extension MainChartView {
|
|
|
|
|
|
private func calculateFpus() {
|
|
|
var calculatedFpus: [Carb] = []
|
|
|
-
|
|
|
- ///check for only fpus
|
|
|
+
|
|
|
+ /// check for only fpus
|
|
|
let fpus = carbs.filter { $0.isFPU ?? false }
|
|
|
-
|
|
|
+
|
|
|
fpus.forEach { fpu in
|
|
|
- let bg = timeToNearestGlucose(time: TimeInterval(rawValue: (fpu.actualDate?.timeIntervalSince1970)!) ?? fpu.createdAt.timeIntervalSince1970)
|
|
|
+ let bg = timeToNearestGlucose(
|
|
|
+ time: TimeInterval(rawValue: (fpu.actualDate?.timeIntervalSince1970)!) ?? fpu.createdAt
|
|
|
+ .timeIntervalSince1970
|
|
|
+ )
|
|
|
calculatedFpus
|
|
|
.append(Carb(amount: fpu.carbs, timestamp: fpu.actualDate ?? Date(), nearestGlucose: bg))
|
|
|
}
|
|
|
ChartFpus = calculatedFpus
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private func calculateBoluses() {
|
|
|
- var calculatedBoluses: [ChartBolus] = []
|
|
|
- boluses.forEach { bolus in
|
|
|
- let bg = timeToNearestGlucose(time: bolus.timestamp.timeIntervalSince1970)
|
|
|
- let yPosition = (bg.sgv ?? 120) + 30
|
|
|
- calculatedBoluses
|
|
|
- .append(ChartBolus(
|
|
|
- amount: bolus.amount ?? 0,
|
|
|
- timestamp: bolus.timestamp,
|
|
|
- nearestGlucose: bg,
|
|
|
- yPosition: yPosition
|
|
|
- ))
|
|
|
- }
|
|
|
- ChartBoluses = calculatedBoluses
|
|
|
- }
|
|
|
-
|
|
|
- ///calculations for temp target bar mark
|
|
|
- ///it is now quite complicated because the remove function in TempTargetStorage does not actually remove the current temp target but instead creates a new temp target with a duration of 0 minutes
|
|
|
- ///therefore it is necessary to check if a temp target was cancelled, i.e. the last temp target in the temp target array has a duration of 0 and then remove the last TWO elements of the array
|
|
|
+ var calculatedBoluses: [ChartBolus] = []
|
|
|
+ boluses.forEach { bolus in
|
|
|
+ let bg = timeToNearestGlucose(time: bolus.timestamp.timeIntervalSince1970)
|
|
|
+ let yPosition = (bg.sgv ?? 120) + 30
|
|
|
+ calculatedBoluses
|
|
|
+ .append(ChartBolus(
|
|
|
+ amount: bolus.amount ?? 0,
|
|
|
+ timestamp: bolus.timestamp,
|
|
|
+ nearestGlucose: bg,
|
|
|
+ yPosition: yPosition
|
|
|
+ ))
|
|
|
+ }
|
|
|
+ ChartBoluses = calculatedBoluses
|
|
|
+ }
|
|
|
+
|
|
|
+ /// calculations for temp target bar mark
|
|
|
+ /// it is now quite complicated because the remove function in TempTargetStorage does not actually remove the current temp target but instead creates a new temp target with a duration of 0 minutes
|
|
|
+ /// therefore it is necessary to check if a temp target was cancelled, i.e. the last temp target in the temp target array has a duration of 0 and then remove the last TWO elements of the array
|
|
|
private func calculateTTs() {
|
|
|
var calculatedTTs: [ChartTempTarget] = []
|
|
|
-
|
|
|
- ///check if last element has a duration of 0
|
|
|
+
|
|
|
+ /// check if last element has a duration of 0
|
|
|
if let lastTempTarget = tempTargets.last, lastTempTarget.duration == 0 {
|
|
|
-
|
|
|
- ///remove the last TWO elements if the last element has a duration of 0
|
|
|
+ /// remove the last TWO elements if the last element has a duration of 0
|
|
|
let filteredTempTargets = Array(tempTargets.dropLast(2))
|
|
|
-
|
|
|
- ///use filtered temp targets for calculation
|
|
|
+
|
|
|
+ /// use filtered temp targets for calculation
|
|
|
calculatedTTs = filteredTempTargets.compactMap { tt in
|
|
|
guard let targetTop = tt.targetTop else { return nil }
|
|
|
-
|
|
|
+
|
|
|
let end = tt.createdAt.addingTimeInterval(TimeInterval(tt.duration * 60))
|
|
|
return ChartTempTarget(amount: targetTop, start: tt.createdAt, end: end)
|
|
|
}
|
|
|
} else {
|
|
|
-
|
|
|
- ///if the last temp target has NOT a duration of 0 use unfiltered temp targets for calculation
|
|
|
+ /// if the last temp target has NOT a duration of 0 use unfiltered temp targets for calculation
|
|
|
calculatedTTs = tempTargets.compactMap { tt in
|
|
|
guard let targetTop = tt.targetTop else { return nil }
|
|
|
-
|
|
|
+
|
|
|
let end = tt.createdAt.addingTimeInterval(TimeInterval(tt.duration * 60))
|
|
|
return ChartTempTarget(amount: targetTop, start: tt.createdAt, end: end)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
ChartTempTargets = calculatedTTs
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private func calculatePredictions() {
|
|
|
var calculatedPredictions: [Prediction] = []
|
|
|
let uam = suggestion?.predictions?.uam ?? []
|
|
|
@@ -720,7 +720,7 @@ extension MainChartView {
|
|
|
return basalTruncatedPoints
|
|
|
}
|
|
|
|
|
|
- ///update start and end marker to fix scroll update problem with x axis
|
|
|
+ /// update start and end marker to fix scroll update problem with x axis
|
|
|
private func updateStartEndMarkers() {
|
|
|
startMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 - 86400))
|
|
|
endMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 + 10800))
|