Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / health / AGENTS.md
| 1 | # AGENTS.md — health/ (':health') |
| 2 | |
| 3 | > Scoped supplement. Global rules in root `AGENTS.md` apply. Stay inside `health/` + allowed shared modules. Do not root-scan. |
| 4 | |
| 5 | _Install: ./install health (dev by default)._ |
| 6 | - Gradle: :health / dir: health/ |
| 7 | - Package roots present: ui, data, domain, platform, service, notifications, intents |
| 8 | - Entry files: MainActivity.kt, Route.kt (sealed nav destinations incl. DialogPage dialog routes), Navigation.kt + ui/NavigationEntries.kt |
| 9 | - Deps: :library:room |
| 10 | - Metadata: metadata_data/health.md (present) |
| 11 | - Rust: no / screenshotTest: yes |
| 12 | |
| 13 | ## Overview |
| 14 | **Purpose:** Offline health-metric manager (Google Fit replacement) — activity/vitals/body/exercise/sleep/hydration/nutrition plus FHIR medical records, backed by Health Connect. No account, no cloud sync. |
| 15 | **Key features:** |
| 16 | - Per-metric dashboards with charts/history (`GenericLineChart`/`GenericBarChart`, hypnogram for sleep) |
| 17 | - Medications carry their own dosage timing (dose times/repeat) and an optional planned end date (shown only while Taking), with exact reminders as a literal on/off toggle on the medication record (`DoseScheduler`, `DoseSoundService`, boot-rescheduled; reminders fire per the medication timing and stop after its planned end date — there is no separate reminder schedule/end date); vaccinations from bundled CVX, drugs from RxNorm; card attachments (PDF/photo) |
| 18 | - Food logging against a bundled database + search API; AI insights via the on-device OpenAssistant model; file export |
| 19 | **Architecture / data-flow:** Dialogs that act on a record/metric are `Route.…Dialog` destinations (`DialogPage()` entries in `ui/NavigationEntries.kt`) pushed from clicks, with stateless content under `ui/dialogs/`; the ingredient search→quantity chain in `RecipeEditorPage` is the one local exception (see its `DIALOG LOCAL EXCEPTION` marker). Screenshot coverage lives in `src/screenshotTest` `MetadataPreviews` (stateless screens: Today/Nutrition/Body/metric detail/nutrition breakdown/recipe list+editor/sleep/exercise + date-time and simple dialog routes); VM-draft routes are exempted per-route in `Route.kt`. `ui/` Today + record pages → `platform/MedicalViewModel` (+ per-area `Medical*Ops`) → `data/HealthRepository` over Room (`Record`, `MedicalRecords`; dosage timing/planned end/reminder toggle are columns on `MedicationEntry`) synced with Health Connect; `domain/` holds pure FHIR (Observations, Medication, Immunization, Allergy/Condition) + `DoseSchedule` (next-dose arithmetic over a `MedicationEntry`); reminders flow `DoseScheduler` (armed per medication) → receivers → `DoseReminderActivity` + `service/DoseSoundService`. DB is v14 (migration 13→14 adds `LabDocument`, `LabTestCatalogEntry` and `LabResultEntry.sourceDocumentId/sourcePage/panel`); the medication timing/end/toggle columns live on `MedicationEntry`. Lab PDF import: Medical tab FAB → `MedicalLabImportOps.startLabPdfImport` stores the PDF in `files/lab_documents/` (backed up via `AppBackupAgent`), renders pages health-locally with platform `PdfRenderer` (`LabPdfRenderer`; the Rust engine is :pdf-app-private), renders at 1024 px (the vision tower's decode side), caps at 50 pages with the cap reported in the review failures, extracts per page via `sdk:openassistant` `generateJsonWithImages` + `domain/LabExtraction` schema/clamping, and writes only on confirm in `Route.LabImportReview` (dedupe by normalized test+date+value; unmatched tests become `LabTestCatalogEntry` rows). Deleting a PDF keeps its results, whose source link then reports it gone. The lab-test picker (`CatalogKind.LabTest`) lists a merged catalogue — recorded tests, custom catalogue rows, bundled LOINC names/units from `ReferenceCatalog` (no invented reference ranges) — and pre-fills unit/range where known. |
| 20 | **Entry points + reading order:** `MainActivity.kt` → `ui/TodayPage.kt` → `platform/MedicalViewModel.kt` → `data/HealthRepository.kt` → `domain/FhirRecords.kt` → `platform/DoseScheduler.kt`. |
| 21 | **Gotchas:** |
| 22 | - Day views use half-open local-calendar ranges (`HealthChartData.dayRange`, DAO `startTime in [start, end)`, bucketed by start): DST days are 23/25h, midnight point records belong to the new day, and intervals crossing midnight count on their start day. Day-tab line metrics average their hourly averages; bar metrics sum their hourly sums; basal metabolic rate (stored kcal/day) is an averaged line metric, while Today hydration converts stored litres to ml. Month/year tabs bucket bar metrics into weekly/monthly sums: their per-day average divides the period total by the calendar days in the month/year, and a history row's goal is the daily goal scaled by the days its bucket covers (line metrics keep averaging and comparing bucket averages directly). |
| 23 | - `util/` is NOT a closed package root — don't add more files there |
| 24 | - Medical data is dual-homed (Room + Health Connect) — writes must update both or they diverge |
| 25 | - Dose reminders need the boot path re-armed like clock alarms; sound runs through a foreground service |
| 26 | |
| 27 | ## Key files |
| 28 | ### Root — entry |
| 29 | - com/vayunmathur/health/MainActivity.kt |
| 30 | - com/vayunmathur/health/Route.kt |
| 31 | - com/vayunmathur/health/Navigation.kt |
| 32 | ### data/ — Room entities + repository |
| 33 | - com/vayunmathur/health/data/HealthRepository.kt |
| 34 | - com/vayunmathur/health/data/Record.kt — also owns `DB_NAME` (`passwords-db`) and `healthDbConfigs` shared by the backup agent |
| 35 | - com/vayunmathur/health/data/HealthConverters.kt — Room type converters (`Converters`) |
| 36 | - com/vayunmathur/health/data/Record.kt |
| 37 | - com/vayunmathur/health/data/NutritionScaling.kt |
| 38 | - com/vayunmathur/health/data/MedicalRecords.kt |
| 39 | - com/vayunmathur/health/data/FoodModels.kt |
| 40 | - com/vayunmathur/health/data/ReferenceCatalog.kt |
| 41 | - com/vayunmathur/health/data/VaccineCatalog.kt |
| 42 | ### domain/ — pure FHIR + dose schedule (no Android) |
| 43 | - com/vayunmathur/health/domain/FhirRecords.kt |
| 44 | - com/vayunmathur/health/domain/FhirObservations.kt |
| 45 | - com/vayunmathur/health/domain/FhirMedication.kt |
| 46 | - com/vayunmathur/health/domain/FhirImmunization.kt |
| 47 | - com/vayunmathur/health/domain/FhirAllergyCondition.kt |
| 48 | - com/vayunmathur/health/domain/FhirJson.kt |
| 49 | - com/vayunmathur/health/domain/DoseSchedule.kt |
| 50 | - com/vayunmathur/health/domain/SocialHistoryQuestions.kt |
| 51 | ### platform/ — ViewModel, medical ops, reminders |
| 52 | - com/vayunmathur/health/platform/MedicalViewModel.kt |
| 53 | - com/vayunmathur/health/platform/MedicalClinicalOps.kt |
| 54 | - com/vayunmathur/health/platform/MedicalImportOps.kt |
| 55 | - com/vayunmathur/health/platform/MedicalMedicationOps.kt |
| 56 | - com/vayunmathur/health/platform/MedicalProfileOps.kt |
| 57 | - com/vayunmathur/health/platform/MedicalVaccinationOps.kt |
| 58 | - com/vayunmathur/health/platform/PersonalHealthRecords.kt |
| 59 | - com/vayunmathur/health/platform/AttachmentStore.kt |
| 60 | - com/vayunmathur/health/platform/DoseScheduler.kt |
| 61 | - com/vayunmathur/health/platform/DoseReceiver.kt |
| 62 | - com/vayunmathur/health/platform/DoseActionReceiver.kt |
| 63 | - com/vayunmathur/health/platform/DoseReminderActivity.kt |
| 64 | - com/vayunmathur/health/platform/HealthBootReceiver.kt |
| 65 | - com/vayunmathur/health/platform/AppBackupAgent.kt — platform backup: `SqlCipherDbCodec` re-keys `passwords-db` into the backup payload (raw encrypted file is excluded by `res/xml/data_extraction_rules.xml` on both channels) and carries `files/medical_attachments/` as extra files; food index and sync bookkeeping are not backed up |
| 66 | ### intents/ — assistant IPC |
| 67 | - com/vayunmathur/health/intents/GetMetricSummaryIntent.kt — OpenAssistant `health_get_metric_summary`: validated metric/range over a recent local-calendar window, SQL aggregates only (latest/average/min/max/total/count via `HealthRepository` aggregate queries); no raw records cross in the `library.intents.health` DTO |
| 68 | ### notifications/ + service/ — channels and dose sound |
| 69 | - com/vayunmathur/health/notifications/DoseNotification.kt |
| 70 | - com/vayunmathur/health/notifications/MedicationChannels.kt |
| 71 | - com/vayunmathur/health/service/DoseSoundService.kt |
| 72 | ### util/ — food DB, sync worker, ViewModel |
| 73 | - com/vayunmathur/health/util/HealthViewModel.kt |
| 74 | - com/vayunmathur/health/util/HealthUiContract.kt |
| 75 | - com/vayunmathur/health/util/HealthSyncWorker.kt |
| 76 | - com/vayunmathur/health/util/HealthAPI.kt |
| 77 | - com/vayunmathur/health/util/FoodDatabase.kt |
| 78 | - com/vayunmathur/health/util/FoodSearchAPI.kt |
| 79 | - com/vayunmathur/health/util/Utils.kt |
| 80 | ### ui/ + ui/components/ — Today, records, charts, logging |
| 81 | - com/vayunmathur/health/ui/NavigationEntries.kt |
| 82 | - com/vayunmathur/health/ui/BodyScreen.kt |
| 83 | - com/vayunmathur/health/ui/dialogs/LogBodyMetricDialogContent.kt |
| 84 | - com/vayunmathur/health/ui/dialogs/LogHydrationDialogContent.kt |
| 85 | - com/vayunmathur/health/ui/dialogs/AboutYouOptionDialog.kt |
| 86 | - com/vayunmathur/health/ui/dialogs/RenameIngredientDialogContent.kt |
| 87 | - com/vayunmathur/health/ui/TodayPage.kt |
| 88 | - com/vayunmathur/health/ui/TodayHero.kt |
| 89 | - com/vayunmathur/health/ui/RecordsPage.kt |
| 90 | - com/vayunmathur/health/ui/BodyPage.kt |
| 91 | - com/vayunmathur/health/ui/SleepDetailsPage.kt |
| 92 | - com/vayunmathur/health/ui/ExerciseDetailsPage.kt |
| 93 | - com/vayunmathur/health/ui/NutritionPage.kt |
| 94 | - com/vayunmathur/health/ui/NutritionDetailsPage.kt |
| 95 | - com/vayunmathur/health/ui/FoodLoggingDialogs.kt |
| 96 | - com/vayunmathur/health/ui/FoodDatabaseCard.kt |
| 97 | - com/vayunmathur/health/ui/IngredientSearchDialog.kt |
| 98 | - com/vayunmathur/health/ui/IngredientQuantityDialog.kt |
| 99 | - com/vayunmathur/health/ui/RecipeEditorPage.kt |
| 100 | - com/vayunmathur/health/ui/RecipesPage.kt — Recipes sibling route of recipe management (`SwipeRouteHost` in `Navigation.kt` owns its tab row/FAB; swipe switches routes) |
| 101 | - com/vayunmathur/health/ui/IngredientsPage.kt — Ingredients sibling route of recipe management |
| 102 | - com/vayunmathur/health/ui/MedicationPage.kt |
| 103 | - com/vayunmathur/health/ui/DoseReminderScreen.kt |
| 104 | - com/vayunmathur/health/ui/VaccinationsPage.kt |
| 105 | - com/vayunmathur/health/ui/AllergiesPage.kt |
| 106 | - com/vayunmathur/health/ui/AddAllergyPage.kt |
| 107 | - com/vayunmathur/health/ui/ConditionsPage.kt |
| 108 | - com/vayunmathur/health/ui/AddConditionPage.kt |
| 109 | - com/vayunmathur/health/ui/AddLabResultPage.kt |
| 110 | - com/vayunmathur/health/ui/LabResultsPage.kt |
| 111 | - com/vayunmathur/health/ui/AddMedicationPage.kt |
| 112 | - com/vayunmathur/health/ui/AddVaccinationPage.kt |
| 113 | - com/vayunmathur/health/ui/AboutYouPage.kt |
| 114 | - com/vayunmathur/health/ui/CatalogPickerPage.kt |
| 115 | - com/vayunmathur/health/ui/BodyLoggingDialog.kt |
| 116 | - com/vayunmathur/health/ui/GenericLineChart.kt |
| 117 | - com/vayunmathur/health/ui/GenericBarChart.kt |
| 118 | - com/vayunmathur/health/ui/BarChartDetails.kt |
| 119 | - com/vayunmathur/health/ui/BarChartDetailsScreen.kt |
| 120 | - com/vayunmathur/health/ui/ChartAxes.kt |
| 121 | - com/vayunmathur/health/ui/MetricChartHeader.kt |
| 122 | - com/vayunmathur/health/ui/MetricDashboardData.kt |
| 123 | - com/vayunmathur/health/ui/HealthColors.kt |
| 124 | - com/vayunmathur/health/ui/HealthFormat.kt |
| 125 | - com/vayunmathur/health/ui/HealthMetricConfig.kt |
| 126 | - com/vayunmathur/health/ui/PermissionsRationaleActivity.kt |
| 127 | - com/vayunmathur/health/ui/components/HealthComponents.kt |
| 128 | - com/vayunmathur/health/ui/components/HealthHypnogram.kt |
| 129 | - com/vayunmathur/health/ui/components/AttachmentChip.kt |
| 130 | - com/vayunmathur/health/ui/components/MedicalStorageNotice.kt |
| 131 | - com/vayunmathur/health/ui/components/PickerField.kt |
| 132 | - com/vayunmathur/health/ui/components/DosageTimingSection.kt — dosage timing fields on the medication form |
| 133 | - com/vayunmathur/health/ui/components/ReminderToggleSection.kt — the literal reminder on/off toggle (+ full-screen-intent warning) |
| 134 | - com/vayunmathur/health/ui/components/SectionLabel.kt |
| 135 | - com/vayunmathur/health/ui/components/AnswerPickerDialog.kt |
| 136 | |
| 137 | ## Verify (this module only) |
| 138 | ``` |
| 139 | ./gradlew :health:compileDevKotlin |
| 140 | ./gradlew :health:lint |
| 141 | ./gradlew :health:checkMetadata |
| 142 | ``` |