Skip to content

Feature: Endocrinologist Visit Report (PDF Export)#671

Open
greyghost99 wants to merge 2 commits into
loopandlearn:devfrom
type1ghost:feature/endo-report
Open

Feature: Endocrinologist Visit Report (PDF Export)#671
greyghost99 wants to merge 2 commits into
loopandlearn:devfrom
type1ghost:feature/endo-report

Conversation

@greyghost99
Copy link
Copy Markdown

@greyghost99 greyghost99 commented Jun 5, 2026

✨ Feature: Endocrinologist Visit Report (PDF Export)

Summary

This PR adds a PDF report generator to LoopFollow that produces a professional, clinic-ready endocrinologist visit report directly from Nightscout data — no third-party services, no external dependencies, and no internet connection required beyond the existing Nightscout connection.

The report is inspired by the LoopInsights report format and designed to give endocrinologists a clean summary of a patient's AID performance over any chosen period.

Motivation

Patients using DIY AID systems (Loop, Trio, iAPS) often arrive at endo appointments without structured data summaries. Apps like Dexcom Clarity and Tidepool produce reports but don't have visibility into Loop/Trio-specific data. LoopFollow already fetches and displays all the relevant data — this feature simply packages it into a shareable PDF that patients can AirDrop, email, or print before their visit.


What's New

Report Contents (all sections are individually toggleable)

  • Glucose Summary — eA1C/GMI estimate, Time in Range (standard + tight 70–140), average glucose, standard deviation, CV%, and total readings
  • Time in Range Distribution — colour-coded vertical bar with ADA target annotations (1% ≈ 15 min)
  • Glucose by Time of Day — six-period strip (Night / Early AM / Morning / Afternoon / Evening / Late) showing average BG per period
  • Ambulatory Glucose Profile (AGP) — full median + IQR + 5–95th percentile bands across the report period
  • Insulin Delivery — avg TDD, basal/bolus split %, correction boluses, SMB/auto-correction count, programmed vs actual basal, positive/negative temp basal adjustments
  • Nutrition & Meals — avg daily carbs, meal count, per-meal average
  • Current Therapy Settings — CR, ISF, basal rate, target glucose (manually entered or auto-filled from Nightscout profile API)
  • Devices & System — AID system, pump, CGM, insulin type
  • Daily Breakdown (Pages 2+) — one row per day, newest to oldest, with glucose scatter plot (colour-coded dots), bolus bars, SMB bars, basal fill/line, carb markers, and per-day stats (avg BG, TIR%, bolus total, scheduled basal, Bolus:Basal ratio, data coverage)

Report Options

  • Date range presets: 3 / 7 / 14 / 30 / 90 days, plus custom start/end date pickers
  • mg/dL or mmol/L toggle
  • Custom accent colour picker
  • Toggle each section on/off individually
  • Daily breakdown pages on/off toggle
  • Fat & protein entry toggle

Auto-Fill from Nightscout

A "Auto-Fill from Nightscout" button in the Therapy Settings section calls /api/v1/profile/current.json using the existing NightscoutUtils infrastructure and pre-fills CR, ISF, basal rate schedule, and target glucose — no manual re-entry needed.


Files Changed

Files Added

  • LoopFollow/Stats/EndoReportGenerator.swift — Core PDF rendering engine using PDFKit + CoreGraphics. Contains EndoReportConfig, EndoReportGenerator, per-page layout, all chart drawing, and the daily breakdown row renderer. No external dependencies.
  • LoopFollow/Stats/EndoReportView.swift — SwiftUI sheet UI for configuring and generating the report. Contains EndoReportView, NightscoutProfileFetcher, ShareSheet, and Color/UIColor hex extensions. All settings are persisted via @AppStorage.

Files Modified

  • LoopFollow/Stats/AggregatedStatsView.swift — Added @State private var showEndoReport = false, a toolbar button (doc.richtext icon) next to the existing Refresh button, and a .sheet presentation of EndoReportView.

Technical Notes

  • No new dependencies — uses only PDFKit, UIKit, CoreGraphics, and SwiftUI, all already available in the project.
  • Reuses existing calculatorsAGPCalculator, TIRCalculator, GRICalculator, SimpleStatsViewModel, and StatsDataService are used directly. No data is re-fetched.
  • Nightscout profile pull — uses the existing NightscoutUtils.executeRequest with .profile event type. Includes a lenient three-layer decoder that handles missing fields gracefully across Nightscout variants (Trio, Loop, standard).
  • Multi-page pagination — daily breakdown pages calculate dynamically. Each page fits as many day rows as space allows and overflows automatically.
  • Thread safety — PDF generation runs on a background DispatchQueue and dispatches UI updates back to main.
  • Settings persistence — all report configuration (patient name, provider, devices, therapy settings, colour, toggles) is persisted via @AppStorage.

Testing

Tested on iPhone simulator (iOS 26.5) with real Nightscout data. Verified:

  • PDF generates and opens correctly in iOS Quick Look
  • Share sheet correctly presents AirDrop, Mail, Save to Files
  • Auto-fill correctly parses single-entry and multi-entry CR/ISF/basal schedules from Nightscout profile
  • mmol/L conversion renders correctly throughout

Date range presets tested:

  • 3 days
  • 7 days
  • 14 days
  • 30 days
  • 90 days
  • Custom date range (manual start/end date picker)
  • Daily breakdown pagination tested across all ranges — confirmed correct number of pages generated and all days present, newest to oldest, for each preset and custom range.

Screenshots

Report Configuration UI

Report Period & Patient Info Devices & Therapy Settings
1 2
Therapy Settings Auto-Filled Schedule Mode Preview Section Toggles & Theme
3 4 5

Generated PDF

Page 1 — Summary Dashboard

6

Page 2 — Daily Glucose Breakdown

7

Checklist

  • New files added to correct target (LoopFollow)
  • No new external dependencies introduced
  • Existing calculators and data services reused, not duplicated
  • All settings persisted via @AppStorage
  • UI follows existing LoopFollow patterns (SwiftUI, Storage.shared, dateTimeUtils)
  • PDF generation is off-main-thread
  • Nightscout profile auto-fill uses existing NightscoutUtils infrastructure
  • Unit tests (PDF layout is inherently visual — manual testing performed)

@greyghost99 greyghost99 marked this pull request as ready for review June 5, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant