Skip to content

Commit

Permalink
glucoseChartManager, searching for lastChartPointEarlierThanEndDate o…
Browse files Browse the repository at this point in the history
…ptimized
  • Loading branch information
JohanDegraeve committed Oct 18, 2020
1 parent b511954 commit e00d09b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
63 changes: 32 additions & 31 deletions xdrip/Managers/Charts/GlucoseChartManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public final class GlucoseChartManager {
private var maximumValueInGlucoseChartPoints:Double = ConstantsGlucoseChart.absoluteMinimumChartValueInMgdl.mgdlToMmol(mgdl: UserDefaults.standard.bloodGlucoseUnitIsMgDl)

/// - if glucoseChartPoints.count > 0, then this is the latest one that has timestamp less than endDate.
///
/// value calculated in loopThroughGlucoseChartPointsAndFindValues
private(set) var lastChartPointEarlierThanEndDate: ChartPoint?

/// is chart in panned state or not, meaning is it currently shifted back in time
Expand Down Expand Up @@ -193,7 +191,38 @@ public final class GlucoseChartManager {

}

self.loopThroughGlucoseChartPointsAndFindValues()
// closure will iterate through glucoseChartPoints and find latest date earlier than endDate and larger then startdate, if there isn't any it returns nil
let findLastChartPointEarlierThanEndDateInArray = { (_ glucoseChartPoints : [ChartPoint]) -> ChartPoint? in

for (_, glucoseChartPoint) in glucoseChartPoints.enumerated().reversed() {

if let chartAxisValueDate = glucoseChartPoint.x as? ChartAxisValueDate {

if chartAxisValueDate.date < endDate {

return glucoseChartPoint

} else if let startDate = startDate, chartAxisValueDate.date < startDate {

return nil

}

}
}

return nil

}

// now calculate lastChartPointEarlierThanEndDate by using findLastChartPointEarlierThanEndDateInArray for newGlucoseChartPointsToAppend, newGlucoseChartPointsToPrepend and glucoseChartPoints
self.lastChartPointEarlierThanEndDate = findLastChartPointEarlierThanEndDateInArray(newGlucoseChartPointsToAppend)
if self.lastChartPointEarlierThanEndDate == nil && reUseExistingChartPointList {
self.lastChartPointEarlierThanEndDate = findLastChartPointEarlierThanEndDateInArray(self.glucoseChartPoints)
}
if self.lastChartPointEarlierThanEndDate == nil {
self.lastChartPointEarlierThanEndDate = findLastChartPointEarlierThanEndDateInArray(newGlucoseChartPointsToPrepend)
}

DispatchQueue.main.async {

Expand Down Expand Up @@ -572,34 +601,6 @@ public final class GlucoseChartManager {

}
}


/// function to be called when glucoseChartPoints array is updated, as first function in generateGlucoseChartWithFrame. Will loop through glucoseChartPoints and find :
/// - the maximum bg value of the chartPoints between start and end date
/// - the timeStamp of the chartPoint with the highest timestamp that is still lower than the endDate, in the list of glucoseChartPoints
private func loopThroughGlucoseChartPointsAndFindValues() {

lastChartPointEarlierThanEndDate = nil

for glucoseChartPoint in glucoseChartPoints {

if let lastChartPointEarlierThanEndDate = lastChartPointEarlierThanEndDate {

if (glucoseChartPoint.x as! ChartAxisValueDate).date <= endDate && (lastChartPointEarlierThanEndDate.x as! ChartAxisValueDate).date < (glucoseChartPoint.x as! ChartAxisValueDate).date {

self.lastChartPointEarlierThanEndDate = glucoseChartPoint

}

} else {

lastChartPointEarlierThanEndDate = glucoseChartPoint

}

}

}

/// - set data to nil, will be called eg to clean up memory when going to the background
/// - all needed variables will will be reinitialized as soon as data() is called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class RootViewController: UIViewController {

} else {

// this should normally not happen because lastChartPointEarlierThanEndDate should normally always be set
// this would only be the case if there's no readings withing the shown timeframe
self.updateLabelsAndChart(overrideApplicationState: false)

}
Expand Down

0 comments on commit e00d09b

Please sign in to comment.