|
| 1 | +// |
| 2 | +// InsulinModelChart.swift |
| 3 | +// LoopKitUI |
| 4 | +// |
| 5 | +// Copyright © 2019 LoopKit Authors. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import LoopKit |
| 10 | +import SwiftCharts |
| 11 | + |
| 12 | + |
| 13 | +public class InsulinModelChart: GlucoseChart, ChartProviding { |
| 14 | + /// The chart points for the selected model |
| 15 | + public private(set) var selectedInsulinModelChartPoints: [ChartPoint] = [] { |
| 16 | + didSet { |
| 17 | + if let lastDate = selectedInsulinModelChartPoints.last?.x as? ChartAxisValueDate { |
| 18 | + updateEndDate(lastDate.date) |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public private(set) var unselectedInsulinModelChartPoints: [[ChartPoint]] = [] { |
| 24 | + didSet { |
| 25 | + for points in unselectedInsulinModelChartPoints { |
| 26 | + if let lastDate = points.last?.x as? ChartAxisValueDate { |
| 27 | + updateEndDate(lastDate.date) |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public private(set) var endDate: Date? |
| 34 | + |
| 35 | + private func updateEndDate(_ date: Date) { |
| 36 | + if endDate == nil || date > endDate! { |
| 37 | + self.endDate = date |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +extension InsulinModelChart { |
| 43 | + public func didReceiveMemoryWarning() { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + public func generate(withFrame frame: CGRect, xAxisModel: ChartAxisModel, xAxisValues: [ChartAxisValue], axisLabelSettings: ChartLabelSettings, guideLinesLayerSettings: ChartGuideLinesLayerSettings, colors: ChartColorPalette, chartSettings: ChartSettings, labelsWidthY: CGFloat, gestureRecognizer: UIGestureRecognizer?, traitCollection: UITraitCollection) -> Chart |
| 48 | + { |
| 49 | + let yAxisValues = ChartAxisValuesStaticGenerator.generateYAxisValuesWithChartPoints(glucoseDisplayRangePoints, |
| 50 | + minSegmentCount: 2, |
| 51 | + maxSegmentCount: 5, |
| 52 | + multiple: glucoseUnit.chartableIncrement / 2, |
| 53 | + axisValueGenerator: { |
| 54 | + ChartAxisValueDouble(round($0), labelSettings: axisLabelSettings) |
| 55 | + }, |
| 56 | + addPaddingSegmentIfEdge: false |
| 57 | + ) |
| 58 | + |
| 59 | + let yAxisModel = ChartAxisModel(axisValues: yAxisValues, lineColor: colors.axisLine, labelSpaceReservationMode: .fixed(labelsWidthY)) |
| 60 | + |
| 61 | + let coordsSpace = ChartCoordsSpaceLeftBottomSingleAxis( |
| 62 | + chartSettings: chartSettings, |
| 63 | + chartFrame: frame, |
| 64 | + xModel: xAxisModel, |
| 65 | + yModel: yAxisModel |
| 66 | + ) |
| 67 | + |
| 68 | + // Grid lines |
| 69 | + let gridLayer = ChartGuideLinesForValuesLayer( |
| 70 | + xAxis: coordsSpace.xAxisLayer.axis, |
| 71 | + yAxis: coordsSpace.yAxisLayer.axis, |
| 72 | + settings: guideLinesLayerSettings, |
| 73 | + axisValuesX: Array(xAxisValues.dropFirst().dropLast()), |
| 74 | + axisValuesY: yAxisValues |
| 75 | + ) |
| 76 | + |
| 77 | + // Selected line |
| 78 | + var selectedLayer: ChartLayer? |
| 79 | + |
| 80 | + if selectedInsulinModelChartPoints.count > 1 { |
| 81 | + let lineModel = ChartLineModel.predictionLine( |
| 82 | + points: selectedInsulinModelChartPoints, |
| 83 | + color: colors.glucoseTint, |
| 84 | + width: 2 |
| 85 | + ) |
| 86 | + |
| 87 | + selectedLayer = ChartPointsLineLayer( |
| 88 | + xAxis: coordsSpace.xAxisLayer.axis, |
| 89 | + yAxis: coordsSpace.yAxisLayer.axis, |
| 90 | + lineModels: [lineModel] |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + var unselectedLineModels = [ChartLineModel]() |
| 95 | + |
| 96 | + for points in unselectedInsulinModelChartPoints where points.count > 1 { |
| 97 | + unselectedLineModels.append(ChartLineModel.predictionLine( |
| 98 | + points: points, |
| 99 | + color: UIColor.secondaryLabelColor, |
| 100 | + width: 1 |
| 101 | + )) |
| 102 | + } |
| 103 | + |
| 104 | + // Unselected lines |
| 105 | + var unselectedLayer: ChartLayer? |
| 106 | + |
| 107 | + if !unselectedLineModels.isEmpty { |
| 108 | + unselectedLayer = ChartPointsLineLayer( |
| 109 | + xAxis: coordsSpace.xAxisLayer.axis, |
| 110 | + yAxis: coordsSpace.yAxisLayer.axis, |
| 111 | + lineModels: unselectedLineModels |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + let layers: [ChartLayer?] = [ |
| 116 | + gridLayer, |
| 117 | + coordsSpace.xAxisLayer, |
| 118 | + coordsSpace.yAxisLayer, |
| 119 | + unselectedLayer, |
| 120 | + selectedLayer |
| 121 | + ] |
| 122 | + |
| 123 | + return Chart( |
| 124 | + frame: frame, |
| 125 | + innerFrame: coordsSpace.chartInnerFrame, |
| 126 | + settings: chartSettings, |
| 127 | + layers: layers.compactMap { $0 } |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +extension InsulinModelChart { |
| 133 | + public func setSelectedInsulinModelValues(_ values: [GlucoseValue]) { |
| 134 | + self.selectedInsulinModelChartPoints = glucosePointsFromValues(values) |
| 135 | + } |
| 136 | + |
| 137 | + public func setUnselectedInsulinModelValues(_ values: [[GlucoseValue]]) { |
| 138 | + self.unselectedInsulinModelChartPoints = values.map(glucosePointsFromValues) |
| 139 | + } |
| 140 | +} |
0 commit comments