import Charts import CoreData import SwiftUI let screenSize: CGRect = UIScreen.main.bounds let calendar = Calendar.current private struct BasalProfile: Hashable { let amount: Double var isOverwritten: Bool let startDate: Date let endDate: Date? init(amount: Double, isOverwritten: Bool, startDate: Date, endDate: Date? = nil) { self.amount = amount self.isOverwritten = isOverwritten self.startDate = startDate self.endDate = endDate } } private struct ChartTempTarget: Hashable { let amount: Decimal let start: Date let end: Date } struct MainChartView: View { private enum Config { static let bolusSize: CGFloat = 5 static let bolusScale: CGFloat = 1 static let carbsSize: CGFloat = 5 static let carbsScale: CGFloat = 0.3 static let fpuSize: CGFloat = 10 static let maxGlucose = 270 static let minGlucose = 45 } var geo: GeometryProxy @Binding var units: GlucoseUnits @Binding var announcement: [Announcement] @Binding var hours: Int @Binding var maxBasal: Decimal @Binding var autotunedBasalProfile: [BasalProfileEntry] @Binding var basalProfile: [BasalProfileEntry] @Binding var tempTargets: [TempTarget] @Binding var smooth: Bool @Binding var highGlucose: Decimal @Binding var lowGlucose: Decimal @Binding var screenHours: Int16 @Binding var displayXgridLines: Bool @Binding var displayYgridLines: Bool @Binding var thresholdLines: Bool @Binding var isTempTargetActive: Bool @StateObject var state: Home.StateModel @State var didAppearTrigger = false <<<<<<< HEAD @State private var basalProfiles: [BasalProfile] = [] @State private var chartTempTargets: [ChartTempTarget] = [] @State private var count: Decimal = 1 @State private var startMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 - 86400)) @State private var endMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 + 10800)) @State private var minValue: Decimal = 45 @State private var maxValue: Decimal = 270 @State private var selection: Date? = nil ======= @State private var glucoseDots: [CGRect] = [] @State private var unSmoothedGlucoseDots: [CGRect] = [] @State private var manualGlucoseDots: [CGRect] = [] @State private var predictionDots: [PredictionType: [CGRect]] = [:] @State private var bolusDots: [DotInfo] = [] @State private var bolusPath = Path() @State private var tempBasalPath = Path() @State private var regularBasalPath = Path() @State private var tempTargetsPath = Path() @State private var suspensionsPath = Path() @State private var carbsDots: [DotInfo] = [] @State private var carbsPath = Path() @State private var fpuDots: [DotInfo] = [] @State private var fpuPath = Path() @State private var glucoseYRange: GlucoseYRange = (0, 0, 0, 0) @State private var offset: CGFloat = 0 @State private var cachedMaxBasalRate: Decimal? >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133 private let now = Date.now private let context = CoreDataStack.shared.persistentContainer.viewContext @Environment(\.colorScheme) var colorScheme @Environment(\.calendar) var calendar private var bolusFormatter: NumberFormatter { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.minimumIntegerDigits = 0 formatter.maximumFractionDigits = 2 formatter.decimalSeparator = "." return formatter } private var carbsFormatter: NumberFormatter { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.maximumFractionDigits = 0 return formatter } private var conversionFactor: Decimal { units == .mmolL ? 0.0555 : 1 } private var upperLimit: Decimal { units == .mgdL ? 400 : 22.2 } private var defaultBolusPosition: Int { units == .mgdL ? 120 : 7 } private var bolusOffset: Decimal { units == .mgdL ? 30 : 1.66 } private var interpolationFactor: Double { Double(state.determinationsFromPersistence.first?.cob ?? 1) * 10 } private var selectedGlucose: GlucoseStored? { if let selection = selection { let lowerBound = selection.addingTimeInterval(-120) let upperBound = selection.addingTimeInterval(120) return state.glucoseFromPersistence.first { $0.date ?? now >= lowerBound && $0.date ?? now <= upperBound } } else { return nil } } var body: some View { VStack { ZStack { VStack(spacing: 5) { dummyBasalChart staticYAxisChart Spacer() dummyCobChart } ScrollViewReader { scroller in ScrollView(.horizontal, showsIndicators: false) { VStack(spacing: 5) { basalChart mainChart Spacer() ZStack { cobChart iobChart } }.onChange(of: screenHours) { _ in updateStartEndMarkers() yAxisChartData() scroller.scrollTo("MainChart", anchor: .trailing) } .onChange(of: state.glucoseFromPersistence.last?.glucose) { _ in updateStartEndMarkers() yAxisChartData() scroller.scrollTo("MainChart", anchor: .trailing) } .onChange(of: state.determinationsFromPersistence.last?.deliverAt) { _ in updateStartEndMarkers() scroller.scrollTo("MainChart", anchor: .trailing) } .onChange(of: state.tempBasals) { _ in updateStartEndMarkers() scroller.scrollTo("MainChart", anchor: .trailing) } .onChange(of: units) { _ in yAxisChartData() } .onAppear { updateStartEndMarkers() scroller.scrollTo("MainChart", anchor: .trailing) } } } } // legendPanel.padding(.top, 8) } } } // MARK: - Components struct Backport { let content: Content } extension View { var backport: Backport { Backport(content: self) } } extension Backport { @ViewBuilder func chartXSelection(value: Binding) -> some View { if #available(iOS 17, *) { content.chartXSelection(value: value) } else { content } } } extension MainChartView { /// empty chart that just shows the Y axis and Y grid lines. Created separately from `mainChart` to allow main chart to scroll horizontally while having a fixed Y axis private var staticYAxisChart: some View { Chart { /// high and low threshold lines if thresholdLines { RuleMark(y: .value("High", highGlucose * conversionFactor)).foregroundStyle(Color.loopYellow) .lineStyle(.init(lineWidth: 1, dash: [5])) RuleMark(y: .value("Low", lowGlucose * conversionFactor)).foregroundStyle(Color.loopRed) .lineStyle(.init(lineWidth: 1, dash: [5])) } } .id("DummyMainChart") .frame(minHeight: geo.size.height * 0.28) .frame(width: screenSize.width - 10) .chartXAxis { mainChartXAxis } .chartXScale(domain: startMarker ... endMarker) .chartXAxis(.hidden) .chartYAxis { mainChartYAxis } .chartYScale(domain: minValue ... maxValue) .chartLegend(.hidden) } private var dummyBasalChart: some View { Chart {} .id("DummyBasalChart") .frame(minHeight: geo.size.height * 0.05) .frame(width: screenSize.width - 10) .chartXAxis { basalChartXAxis } .chartXAxis(.hidden) .chartYAxis(.hidden) .chartLegend(.hidden) } private var dummyCobChart: some View { Chart { drawCOB(dummy: true) } .id("DummyCobChart") .frame(minHeight: geo.size.height * 0.12) .frame(width: screenSize.width - 10) .chartXScale(domain: startMarker ... endMarker) .chartXAxis { basalChartXAxis } .chartXAxis(.hidden) .chartYAxis { cobChartYAxis } .chartYAxis(.hidden) .chartLegend(.hidden) } private var mainChart: some View { VStack { Chart { drawStartRuleMark() drawEndRuleMark() drawCurrentTimeMarker() drawFpus() drawBoluses() drawTempTargets() drawActiveOverrides() drawOverrideRunStored() drawForecasts() drawGlucose(dummy: false) drawManualGlucose() drawCarbs() /// show glucose value when hovering over it if let selectedGlucose { RuleMark(x: .value("Selection", selectedGlucose.date ?? now, unit: .minute)) .foregroundStyle(Color.tabBar) .offset(yStart: 70) .lineStyle(.init(lineWidth: 2, dash: [5])) .annotation(position: .top) { selectionPopover } } } .id("MainChart") .onChange(of: state.insulinFromPersistence) { _ in state.roundedTotalBolus = state.calculateTINS() } .onChange(of: tempTargets) { _ in calculateTTs() } .onChange(of: didAppearTrigger) { _ in calculateTTs() } .frame(minHeight: geo.size.height * 0.28) .frame(width: fullWidth(viewWidth: screenSize.width)) .chartXScale(domain: startMarker ... endMarker) .chartXAxis { mainChartXAxis } .chartYAxis { mainChartYAxis } .chartYAxis(.hidden) .backport.chartXSelection(value: $selection) .chartYScale(domain: minValue ... maxValue) .chartForegroundStyleScale([ "zt": Color.zt, "uam": Color.uam, "cob": .orange, "iob": .blue ]) .chartLegend(.hidden) } } @ViewBuilder var selectionPopover: some View { if let sgv = selectedGlucose?.glucose { let glucoseToShow = Decimal(sgv) * conversionFactor VStack { <<<<<<< HEAD Text(selectedGlucose?.date?.formatted(.dateTime.hour().minute(.twoDigits)) ?? "") HStack { Text(glucoseToShow.formatted(.number.precision(units == .mmolL ? .fractionLength(1) : .fractionLength(0)))) .fontWeight(.bold) .foregroundStyle( Decimal(sgv) < lowGlucose ? Color .red : (Decimal(sgv) > highGlucose ? Color.orange : Color.primary) ) Text(units.rawValue).foregroundColor(.secondary) ======= ZStack { xGridView(fullSize: fullSize) carbsView(fullSize: fullSize) fpuView(fullSize: fullSize) bolusView(fullSize: fullSize) if smooth { unSmoothedGlucoseView(fullSize: fullSize) } glucoseView(fullSize: fullSize) manualGlucoseView(fullSize: fullSize) predictionsView(fullSize: fullSize) } timeLabelsView(fullSize: fullSize) } } .frame(width: fullGlucoseWidth(viewWidth: fullSize.width) + additionalWidth(viewWidth: fullSize.width)) } @Environment(\.colorScheme) var colorScheme private func xGridView(fullSize: CGSize) -> some View { let useColour = displayXgridLines ? Color.secondary : Color.clear return ZStack { Path { path in for hour in 0 ..< hours + hours { let x = firstHourPosition(viewWidth: fullSize.width) + oneSecondStep(viewWidth: fullSize.width) * CGFloat(hour) * CGFloat(1.hours.timeInterval) path.move(to: CGPoint(x: x, y: 0)) path.addLine(to: CGPoint(x: x, y: fullSize.height - 20)) >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133 } } .padding(6) .background { RoundedRectangle(cornerRadius: 4) .fill(Color.gray.opacity(0.1)) .shadow(color: .blue, radius: 2) } } } private var basalChart: some View { VStack { Chart { drawStartRuleMark() drawEndRuleMark() drawCurrentTimeMarker() drawTempBasals(dummy: false) drawBasalProfile() drawSuspensions() }.onChange(of: state.tempBasals) { _ in calculateBasals() } <<<<<<< HEAD .onChange(of: maxBasal) { _ in calculateBasals() ======= path.addLines(lines) } .stroke(Color.loopGray, lineWidth: 0.5) .onChange(of: glucose) { _ in update(fullSize: fullSize) } .onChange(of: didAppearTrigger) { _ in update(fullSize: fullSize) } .onReceive(Foundation.NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in update(fullSize: fullSize) } } private func manualGlucoseView(fullSize: CGSize) -> some View { ZStack { Path { path in for rect in manualGlucoseDots { path.addEllipse(in: rect) } } .fill(Color.loopRed) Path { path in for rect in manualGlucoseDots { path.addEllipse(in: rect) } } .stroke(Color.primary, lineWidth: 0.5) } .onChange(of: glucose) { _ in update(fullSize: fullSize) } .onChange(of: didAppearTrigger) { _ in update(fullSize: fullSize) } .onReceive(Foundation.NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in update(fullSize: fullSize) } } private func bolusView(fullSize: CGSize) -> some View { ZStack { bolusPath .fill(Color.insulin) bolusPath .stroke(Color.primary, lineWidth: 0.5) ForEach(bolusDots, id: \.rect.minX) { info -> AnyView in let position = CGPoint(x: info.rect.midX, y: info.rect.maxY + 8) return Text(bolusFormatter.string(from: info.value as NSNumber)!).font(.caption2) .position(position) .asAny() >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133 } .onChange(of: autotunedBasalProfile) { _ in calculateBasals() } .onChange(of: didAppearTrigger) { _ in calculateBasals() }.onChange(of: basalProfile) { _ in calculateBasals() } .frame(minHeight: geo.size.height * 0.05) .frame(width: fullWidth(viewWidth: screenSize.width)) .chartXScale(domain: startMarker ... endMarker) .chartXAxis { basalChartXAxis } .chartXAxis(.hidden) .chartYAxis(.hidden) .rotationEffect(.degrees(180)) .scaleEffect(x: -1, y: 1, anchor: .center) } } private var iobChart: some View { VStack { Chart { drawIOB() } .frame(minHeight: geo.size.height * 0.12) .frame(width: fullWidth(viewWidth: screenSize.width)) .chartXScale(domain: startMarker ... endMarker) .chartXAxis { basalChartXAxis } // .chartXAxis(.hidden) .chartYAxis { cobChartYAxis } .chartYAxis(.hidden) } } private var cobChart: some View { Chart { drawCurrentTimeMarker() drawCOB(dummy: false) } .frame(minHeight: geo.size.height * 0.12) .frame(width: fullWidth(viewWidth: screenSize.width)) .chartXScale(domain: startMarker ... endMarker) .chartXAxis { basalChartXAxis } // .chartXAxis(.hidden) .chartYAxis { cobChartYAxis } // .chartYAxis(.hidden) } var legendPanel: some View { HStack(spacing: 10) { Spacer() LegendItem(color: .loopGreen, label: "BG") LegendItem(color: .insulin, label: "IOB") LegendItem(color: .zt, label: "ZT") LegendItem(color: .loopYellow, label: "COB") LegendItem(color: .uam, label: "UAM") Spacer() } .padding(.horizontal, 10) .frame(maxWidth: .infinity) } } // MARK: - Calculations extension MainChartView { <<<<<<< HEAD private func drawBoluses() -> some ChartContent { ForEach(state.insulinFromPersistence) { insulin in let amount = insulin.bolus?.amount ?? 0 as NSDecimalNumber let bolusDate = insulin.timestamp ?? Date() if amount != 0, let glucose = timeToNearestGlucose(time: bolusDate.timeIntervalSince1970)?.glucose { let yPosition = (Decimal(glucose) * conversionFactor) + bolusOffset let size = (Config.bolusSize + CGFloat(truncating: amount) * Config.bolusScale) * 1.8 PointMark( x: .value("Time", bolusDate, unit: .second), y: .value("Value", yPosition) ) .symbol { Image(systemName: "arrowtriangle.down.fill").font(.system(size: size)).foregroundStyle(Color.insulin) } .annotation(position: .top) { Text(bolusFormatter.string(from: amount) ?? "") .font(.caption2) .foregroundStyle(Color.insulin) ======= private func update(fullSize: CGSize) { calculatePredictionDots(fullSize: fullSize, type: .iob) calculatePredictionDots(fullSize: fullSize, type: .cob) calculatePredictionDots(fullSize: fullSize, type: .zt) calculatePredictionDots(fullSize: fullSize, type: .uam) calculateGlucoseDots(fullSize: fullSize) calculateUnSmoothedGlucoseDots(fullSize: fullSize) calculateManualGlucoseDots(fullSize: fullSize) calculateBolusDots(fullSize: fullSize) calculateCarbsDots(fullSize: fullSize) calculateFPUsDots(fullSize: fullSize) calculateTempTargetsRects(fullSize: fullSize) calculateBasalPoints(fullSize: fullSize) calculateSuspensions(fullSize: fullSize) } private func calculateGlucoseDots(fullSize: CGSize) { calculationQueue.async { let sgvs = glucose .filter { $0.type != "Manual" } // as fingerpricks will be drawn differently, slightly larger and red - so do not draw them here let dots = sgvs.concurrentMap { value -> CGRect in let position = glucoseToCoordinate(value, fullSize: fullSize) return CGRect(x: position.x - 2, y: position.y - 2, width: 4, height: 4) } let range = self.getGlucoseYRange(fullSize: fullSize) DispatchQueue.main.async { glucoseYRange = range glucoseDots = dots } } } private func calculateUnSmoothedGlucoseDots(fullSize: CGSize) { calculationQueue.async { let sgvs = glucose.filter { $0.type == "sgv" } let dots = sgvs.concurrentMap { value -> CGRect in let position = UnSmoothedGlucoseToCoordinate(value, fullSize: fullSize) return CGRect(x: position.x - 2, y: position.y - 2, width: 4, height: 4) } let range = self.getGlucoseYRange(fullSize: fullSize) DispatchQueue.main.async { glucoseYRange = range unSmoothedGlucoseDots = dots } } } private func calculateManualGlucoseDots(fullSize: CGSize) { calculationQueue.async { let manuals = glucose.filter { $0.type == "Manual" } let dots = manuals.concurrentMap { value -> CGRect in let position = glucoseToCoordinate(value, fullSize: fullSize) return CGRect(x: position.x - 2, y: position.y - 2, width: 6, height: 6) } let range = self.getGlucoseYRange(fullSize: fullSize) DispatchQueue.main.async { glucoseYRange = range manualGlucoseDots = dots } } } private func calculateBolusDots(fullSize: CGSize) { calculationQueue.async { let dots = boluses.map { value -> DotInfo in let center = timeToInterpolatedPoint(value.timestamp.timeIntervalSince1970, fullSize: fullSize) let size = Config.bolusSize + CGFloat(value.amount ?? 0) * Config.bolusScale let rect = CGRect(x: center.x - size / 2, y: center.y - size / 2, width: size, height: size) return DotInfo(rect: rect, value: value.amount ?? 0) } let path = Path { path in for dot in dots { path.addEllipse(in: dot.rect) >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133 } } } } private func drawCarbs() -> some ChartContent { /// carbs ForEach(state.carbsFromPersistence) { carb in let carbAmount = carb.carbs let carbDate = carb.date ?? Date() if let glucose = timeToNearestGlucose(time: carbDate.timeIntervalSince1970)?.glucose { let yPosition = (Decimal(glucose) * conversionFactor) - bolusOffset let size = (Config.carbsSize + CGFloat(carbAmount) * Config.carbsScale) PointMark( x: .value("Time", carbDate, unit: .second), y: .value("Value", yPosition) ) .symbol { Image(systemName: "arrowtriangle.down.fill").font(.system(size: size)).foregroundStyle(Color.orange) .rotationEffect(.degrees(180)) } .annotation(position: .bottom) { Text(carbsFormatter.string(from: carbAmount as NSNumber)!).font(.caption2) .foregroundStyle(Color.orange) } } } } private func drawFpus() -> some ChartContent { /// fpus ForEach(state.fpusFromPersistence, id: \.id) { fpu in let fpuAmount = fpu.carbs let size = (Config.fpuSize + CGFloat(fpuAmount) * Config.carbsScale) * 1.8 let yPosition = minValue PointMark( x: .value("Time", fpu.date ?? Date(), unit: .second), y: .value("Value", yPosition) ) .symbolSize(size) .foregroundStyle(Color.brown) } } private func drawGlucose(dummy _: Bool) -> some ChartContent { /// glucose point mark /// filtering for high and low bounds in settings ForEach(state.glucoseFromPersistence) { item in if smooth { if item.glucose > Int(highGlucose) { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.orange.gradient).symbolSize(20).interpolationMethod(.cardinal) } else if item.glucose < Int(lowGlucose) { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.red.gradient).symbolSize(20).interpolationMethod(.cardinal) } else { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.green.gradient).symbolSize(20).interpolationMethod(.cardinal) } } else { if item.glucose > Int(highGlucose) { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.orange.gradient).symbolSize(20) } else if item.glucose < Int(lowGlucose) { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.red.gradient).symbolSize(20) } else { PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(item.glucose) * conversionFactor) ).foregroundStyle(Color.green.gradient).symbolSize(20) } } } } private func timeForIndex(_ index: Int32) -> Date { let currentTime = Date() let timeInterval = TimeInterval(index * 300) return currentTime.addingTimeInterval(timeInterval) } private func drawForecasts() -> some ChartContent { ForEach(state.preprocessedData, id: \.id) { tuple in let forecastValue = tuple.forecastValue let forecast = tuple.forecast LineMark( x: .value("Time", timeForIndex(forecastValue.index)), y: .value("Value", Int(forecastValue.value)) ) .foregroundStyle(by: .value("Predictions", forecast.type ?? "")) } } private func drawCurrentTimeMarker() -> some ChartContent { RuleMark( x: .value( "", Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970)), unit: .second ) ).lineStyle(.init(lineWidth: 2, dash: [3])).foregroundStyle(Color(.systemGray2)) } private func drawStartRuleMark() -> some ChartContent { RuleMark( x: .value( "", startMarker, unit: .second ) ).foregroundStyle(Color.clear) } private func drawEndRuleMark() -> some ChartContent { RuleMark( x: .value( "", endMarker, unit: .second ) ).foregroundStyle(Color.clear) } private func drawTempTargets() -> some ChartContent { /// temp targets ForEach(chartTempTargets, id: \.self) { target in let targetLimited = min(max(target.amount, 0), upperLimit) RuleMark( xStart: .value("Start", target.start), xEnd: .value("End", target.end), y: .value("Value", targetLimited) ) .foregroundStyle(Color.purple.opacity(0.5)).lineStyle(.init(lineWidth: 8)) } } private func drawActiveOverrides() -> some ChartContent { ForEach(state.overrides) { override in let start: Date = override.date ?? .distantPast let duration = state.calculateDuration(override: override) let end: Date = start.addingTimeInterval(duration) let target = state.calculateTarget(override: override) RuleMark( xStart: .value("Start", start, unit: .second), xEnd: .value("End", end, unit: .second), y: .value("Value", target) ) .foregroundStyle(Color.purple.opacity(0.6)) .lineStyle(.init(lineWidth: 8)) // .annotation(position: .overlay, spacing: 0) { // if let name = override.name { // Text("\(name)").foregroundStyle(.secondary).font(.footnote) // } // } } } private func drawOverrideRunStored() -> some ChartContent { ForEach(state.overrideRunStored) { overrideRunStored in let start: Date = overrideRunStored.startDate ?? .distantPast let end: Date = overrideRunStored.endDate ?? Date() let target = overrideRunStored.target?.decimalValue ?? 100 RuleMark( xStart: .value("Start", start, unit: .second), xEnd: .value("End", end, unit: .second), y: .value("Value", target) ) .foregroundStyle(Color.purple.opacity(0.4)) .lineStyle(.init(lineWidth: 8)) // .annotation(position: .bottom, spacing: 0) { // if let name = overrideRunStored.override?.name { // Text("\(name)").foregroundStyle(.secondary).font(.footnote) // } // } } } private func drawManualGlucose() -> some ChartContent { /// manual glucose mark ForEach(state.manualGlucoseFromPersistence) { item in let manualGlucose = item.glucose PointMark( x: .value("Time", item.date ?? Date(), unit: .second), y: .value("Value", Decimal(manualGlucose) * conversionFactor) ) .symbol { Image(systemName: "drop.fill").font(.system(size: 10)).symbolRenderingMode(.monochrome) .foregroundStyle(.red) } } } private func drawSuspensions() -> some ChartContent { let suspensions = state.suspensions return ForEach(suspensions) { suspension in let now = Date() if let type = suspension.type, type == EventType.pumpSuspend.rawValue, let suspensionStart = suspension.timestamp { let suspensionEnd = min( ( suspensions .first(where: { $0.timestamp ?? now > suspensionStart && $0.type == EventType.pumpResume.rawValue })? .timestamp ) ?? now, now ) let basalProfileDuringSuspension = basalProfiles.first(where: { $0.startDate <= suspensionStart }) let suspensionMarkHeight = basalProfileDuringSuspension?.amount ?? 1 RectangleMark( xStart: .value("start", suspensionStart), xEnd: .value("end", suspensionEnd), yStart: .value("suspend-start", 0), yEnd: .value("suspend-end", suspensionMarkHeight) ) .foregroundStyle(Color.loopGray.opacity(colorScheme == .dark ? 0.3 : 0.8)) } } } private func drawIOB() -> some ChartContent { ForEach(state.enactedAndNonEnactedDeterminations) { iob in let amount: Double = (iob.iob?.doubleValue ?? 0 / interpolationFactor) let date: Date = iob.deliverAt ?? Date() LineMark(x: .value("Time", date), y: .value("Amount", amount)) .foregroundStyle(Color.darkerBlue) AreaMark(x: .value("Time", date), y: .value("Amount", amount)) .foregroundStyle( LinearGradient( gradient: Gradient( colors: [ Color.darkerBlue.opacity(0.8), Color.darkerBlue.opacity(0.01) ] ), startPoint: .top, endPoint: .bottom ) ) } } private func drawCOB(dummy: Bool) -> some ChartContent { ForEach(state.enactedAndNonEnactedDeterminations) { cob in let amount = Int(cob.cob) let date: Date = cob.deliverAt ?? Date() if dummy { LineMark(x: .value("Time", date), y: .value("Value", amount)) .foregroundStyle(Color.clear) AreaMark(x: .value("Time", date), y: .value("Value", amount)).foregroundStyle( Color.clear ) } else { LineMark(x: .value("Time", date), y: .value("Value", amount)) .foregroundStyle(Color.orange.gradient) AreaMark(x: .value("Time", date), y: .value("Value", amount)).foregroundStyle( LinearGradient( gradient: Gradient( colors: [ Color.orange.opacity(0.8), Color.orange.opacity(0.01) ] ), startPoint: .top, endPoint: .bottom ) ) } } } private func prepareTempBasals() -> [(start: Date, end: Date, rate: Double)] { let now = Date() let tempBasals = state.tempBasals return tempBasals.compactMap { temp -> (start: Date, end: Date, rate: Double)? in let duration = temp.tempBasal?.duration ?? 0 let timestamp = temp.timestamp ?? Date() let end = min(timestamp + duration.minutes, now) let isInsulinSuspended = state.suspensions.contains { $0.timestamp ?? now >= timestamp && $0.timestamp ?? now <= end } let rate = Double(truncating: temp.tempBasal?.rate ?? Decimal.zero as NSDecimalNumber) * (isInsulinSuspended ? 0 : 1) // Check if there's a subsequent temp basal to determine the end time guard let nextTemp = state.tempBasals.first(where: { $0.timestamp ?? .distantPast > timestamp }) else { return (timestamp, end, rate) } return (timestamp, nextTemp.timestamp ?? Date(), rate) // end defaults to current time } } private func drawTempBasals(dummy: Bool) -> some ChartContent { ForEach(prepareTempBasals(), id: \.rate) { basal in if dummy { RectangleMark( xStart: .value("start", basal.start), xEnd: .value("end", basal.end), yStart: .value("rate-start", 0), yEnd: .value("rate-end", basal.rate) ).foregroundStyle(Color.clear) LineMark(x: .value("Start Date", basal.start), y: .value("Amount", basal.rate)) .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.clear) LineMark(x: .value("End Date", basal.end), y: .value("Amount", basal.rate)) .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.clear) } else { RectangleMark( xStart: .value("start", basal.start), xEnd: .value("end", basal.end), yStart: .value("rate-start", 0), yEnd: .value("rate-end", basal.rate) ).foregroundStyle(Color.insulin.opacity(0.2)) LineMark(x: .value("Start Date", basal.start), y: .value("Amount", basal.rate)) .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin) LineMark(x: .value("End Date", basal.end), y: .value("Amount", basal.rate)) .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin) } } } private func drawBasalProfile() -> some ChartContent { /// dashed profile line ForEach(basalProfiles, id: \.self) { profile in LineMark( x: .value("Start Date", profile.startDate), y: .value("Amount", profile.amount), series: .value("profile", "profile") ).lineStyle(.init(lineWidth: 2, dash: [2, 4])).foregroundStyle(Color.insulin) LineMark( x: .value("End Date", profile.endDate ?? endMarker), y: .value("Amount", profile.amount), series: .value("profile", "profile") ).lineStyle(.init(lineWidth: 2.5, dash: [2, 4])).foregroundStyle(Color.insulin) } } /// calculates the glucose value thats the nearest to parameter 'time' private func timeToNearestGlucose(time: TimeInterval) -> GlucoseStored? { guard !state.glucoseFromPersistence.isEmpty else { return nil } // sort by date let sortedGlucose = state.glucoseFromPersistence .sorted { $0.date?.timeIntervalSince1970 ?? 0 < $1.date?.timeIntervalSince1970 ?? 0 } var low = 0 var high = sortedGlucose.count - 1 var closestGlucose: GlucoseStored? // binary search to find next glucose while low <= high { let mid = low + (high - low) / 2 let midTime = sortedGlucose[mid].date?.timeIntervalSince1970 ?? 0 if midTime == time { return sortedGlucose[mid] } else if midTime < time { low = mid + 1 } else { high = mid - 1 } // update if necessary if closestGlucose == nil || abs(midTime - time) < abs(closestGlucose!.date?.timeIntervalSince1970 ?? 0 - time) { closestGlucose = sortedGlucose[mid] } } return closestGlucose } private func fullWidth(viewWidth: CGFloat) -> CGFloat { viewWidth * CGFloat(hours) / CGFloat(min(max(screenHours, 2), 24)) } /// calculations for temp target bar mark private func calculateTTs() { var groupedPackages: [[TempTarget]] = [] var currentPackage: [TempTarget] = [] var calculatedTTs: [ChartTempTarget] = [] for target in tempTargets { if target.duration > 0 { if !currentPackage.isEmpty { groupedPackages.append(currentPackage) currentPackage = [] } currentPackage.append(target) } else { if let lastNonZeroTempTarget = currentPackage.last(where: { $0.duration > 0 }) { if target.createdAt >= lastNonZeroTempTarget.createdAt, target.createdAt <= lastNonZeroTempTarget.createdAt .addingTimeInterval(TimeInterval(lastNonZeroTempTarget.duration * 60)) { currentPackage.append(target) } } } } // appends last package, if exists if !currentPackage.isEmpty { groupedPackages.append(currentPackage) } for package in groupedPackages { guard let firstNonZeroTarget = package.first(where: { $0.duration > 0 }) else { continue } var end = firstNonZeroTarget.createdAt.addingTimeInterval(TimeInterval(firstNonZeroTarget.duration * 60)) let earliestCancelTarget = package.filter({ $0.duration == 0 }).min(by: { $0.createdAt < $1.createdAt }) if let earliestCancelTarget = earliestCancelTarget { end = min(earliestCancelTarget.createdAt, end) } let now = Date() isTempTargetActive = firstNonZeroTarget.createdAt <= now && now <= end if firstNonZeroTarget.targetTop != nil { calculatedTTs .append(ChartTempTarget( amount: (firstNonZeroTarget.targetTop ?? 0) * conversionFactor, start: firstNonZeroTarget.createdAt, end: end )) } } chartTempTargets = calculatedTTs } private func findRegularBasalPoints( timeBegin: TimeInterval, timeEnd: TimeInterval, autotuned: Bool ) -> [BasalProfile] { guard timeBegin < timeEnd else { return [] } let beginDate = Date(timeIntervalSince1970: timeBegin) let calendar = Calendar.current let startOfDay = calendar.startOfDay(for: beginDate) let profile = autotuned ? autotunedBasalProfile : basalProfile let basalNormalized = profile.map { ( time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval).timeIntervalSince1970, rate: $0.rate ) } + profile.map { ( time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 1.days.timeInterval) .timeIntervalSince1970, rate: $0.rate ) } + profile.map { ( time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 2.days.timeInterval) .timeIntervalSince1970, rate: $0.rate ) } let basalTruncatedPoints = basalNormalized.windows(ofCount: 2) .compactMap { window -> BasalProfile? in let window = Array(window) if window[0].time < timeBegin, window[1].time < timeBegin { return nil } if window[0].time < timeBegin, window[1].time >= timeBegin { let startDate = Date(timeIntervalSince1970: timeBegin) let rate = window[0].rate return BasalProfile(amount: Double(rate), isOverwritten: false, startDate: startDate) } if window[0].time >= timeBegin, window[0].time < timeEnd { let startDate = Date(timeIntervalSince1970: window[0].time) let rate = window[0].rate return BasalProfile(amount: Double(rate), isOverwritten: false, startDate: startDate) } return nil } return basalTruncatedPoints } /// 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)) } private func calculateBasals() { let dayAgoTime = Date().addingTimeInterval(-1.days.timeInterval).timeIntervalSince1970 let regularPoints = findRegularBasalPoints( timeBegin: dayAgoTime, timeEnd: endMarker.timeIntervalSince1970, autotuned: false ) let autotunedBasalPoints = findRegularBasalPoints( timeBegin: dayAgoTime, timeEnd: endMarker.timeIntervalSince1970, autotuned: true ) var totalBasal = regularPoints + autotunedBasalPoints totalBasal.sort { $0.startDate.timeIntervalSince1970 < $1.startDate.timeIntervalSince1970 } var basals: [BasalProfile] = [] totalBasal.indices.forEach { index in basals.append(BasalProfile( amount: totalBasal[index].amount, isOverwritten: totalBasal[index].isOverwritten, startDate: totalBasal[index].startDate, endDate: totalBasal.count > index + 1 ? totalBasal[index + 1].startDate : endMarker )) } basalProfiles = basals } // MARK: - Chart formatting private func yAxisChartData() { let glucoseMapped = state.glucoseFromPersistence.map { Decimal($0.glucose) } let forecastValues = state.preprocessedData.map { Decimal($0.forecastValue.value) } guard let minGlucose = glucoseMapped.min(), let maxGlucose = glucoseMapped.max(), let minForecast = forecastValues.min(), let maxForecast = forecastValues.max() else { // default values minValue = 45 * conversionFactor - 20 * conversionFactor maxValue = 270 * conversionFactor + 50 * conversionFactor return } let minOverall = min(minGlucose, minForecast) let maxOverall = max(maxGlucose, maxForecast) minValue = minOverall * conversionFactor - 50 * conversionFactor maxValue = maxOverall * conversionFactor + 80 * conversionFactor debug(.default, "min \(minValue)") debug(.default, "max \(maxValue)") } private func basalChartPlotStyle(_ plotContent: ChartPlotContent) -> some View { plotContent .rotationEffect(.degrees(180)) .scaleEffect(x: -1, y: 1) .chartXAxis(.hidden) } private var mainChartXAxis: some AxisContent { AxisMarks(values: .stride(by: .hour, count: screenHours == 24 ? 4 : 2)) { _ in if displayXgridLines { AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3])) } else { AxisGridLine(stroke: .init(lineWidth: 0, dash: [2, 3])) } } } private var basalChartXAxis: some AxisContent { AxisMarks(values: .stride(by: .hour, count: screenHours == 24 ? 4 : 2)) { _ in if displayXgridLines { AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3])) } else { AxisGridLine(stroke: .init(lineWidth: 0, dash: [2, 3])) } AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top) .font(.footnote).foregroundStyle(Color.primary) } } private var mainChartYAxis: some AxisContent { AxisMarks(position: .trailing) { value in if displayXgridLines { AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3])) } else { AxisGridLine(stroke: .init(lineWidth: 0, dash: [2, 3])) } if let glucoseValue = value.as(Double.self), glucoseValue > 0 { /// fix offset between the two charts... if units == .mmolL { AxisTick(length: 7, stroke: .init(lineWidth: 7)).foregroundStyle(Color.clear) } AxisValueLabel().font(.footnote).foregroundStyle(Color.primary) } } } private var cobChartYAxis: some AxisContent { AxisMarks(position: .trailing) { _ in if displayXgridLines { AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3])) } else { AxisGridLine(stroke: .init(lineWidth: 0, dash: [2, 3])) } // AxisValueLabel().font(.system(.footnote)) } } } struct LegendItem: View { var color: Color var label: String var body: some View { Group { Circle().fill(color).frame(width: 8, height: 8) Text(label) .font(.system(size: 10, weight: .bold)) .foregroundColor(color) } } } extension Int16 { var minutes: TimeInterval { TimeInterval(self) * 60 } }