diff --git a/xdrip/Managers/Charts/GlucoseChartManager.swift b/xdrip/Managers/Charts/GlucoseChartManager.swift index 3933b4d23..ba35d8210 100644 --- a/xdrip/Managers/Charts/GlucoseChartManager.swift +++ b/xdrip/Managers/Charts/GlucoseChartManager.swift @@ -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 @@ -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 { @@ -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 diff --git a/xdrip/View Controllers/Root View Controller/RootViewController.swift b/xdrip/View Controllers/Root View Controller/RootViewController.swift index 104914220..b12c298ac 100644 --- a/xdrip/View Controllers/Root View Controller/RootViewController.swift +++ b/xdrip/View Controllers/Root View Controller/RootViewController.swift @@ -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) }