MedTrack
Designing a calm, offline-first Android companion for medication routines and symptom history—without letting UI state and storage tangle together做一款安静、离线优先的 Android 用药与症状伴侣,同时不让界面状态和本地存储搅成一团
March 20262026年3月
Most medication apps treat pills like calendar alarms: a buzz goes off, you tap dismiss, and all the context around that dose disappears. Weeks later at a clinic appointment, trying to recall whether a bout of dizziness followed a morning blood-pressure tablet or whether you skipped lunch doses for three days turns into guesswork across scattered notes.
MedTrack started as an Android mobile development project in Kotlin and Jetpack Compose, built around a simple premise: a personal health companion should feel calm enough to open every morning and structured enough that daily adherence and symptom logs live side by side. Rather than defaulting to sterile hospital blues and cluttered dashboards, I wanted to see what happened if we treated a patient-facing Android app with the same typographic restraint and architectural discipline as a web application.
Because this was an offline-first prototype, the most interesting decisions weren't about cloud sync or complex backends—they were about how to keep every screen predictable, testable, and resilient when a patient signs up, logs in, checks off doses, and records symptoms on a phone.
大多数吃药提醒应用把药片当成日历闹钟:铃声一响,顺手划掉,这次服药前后的上下文也就跟着没了。几周后坐在诊室里,想回忆那阵头晕是不是出现在早起吃降压药之后,或者是不是连着三天漏了午饭那顿,往往只能翻着散落各处的备忘录猜。
MedTrack 起初是一个用 Kotlin 和 Jetpack Compose 写的 Android 课程项目,出发点很朴素:个人健康伴侣应当安静到每天早上都愿意打开,结构清楚到日常服药和症状史并排可查。我没有照搬冷冰冰的医院蓝和拥挤仪表盘,而是想试试把面向患者的 Android 应用当成克制排版的网页产品来做,会是什么样。
因为是离线优先原型,最有意思的取舍不在云同步或复杂后端,而在于怎么让注册、登录、打卡服药和记症状每一步都可预测、好单测,经得起真实输入折腾。
A warm visual system instead of clinical glare用暖色刊物感取代刺眼的临床冷白
Health apps often reach for high-contrast cyan and stark white cards that make opening the app feel like reading a lab invoice. In MedTrack's Material 3 theme, I went in the opposite direction: a warm sand-and-ink light palette paired with a cocoa-and-cream dark palette, accented by muted brown, moss green, and rust red for semantic states.1 I also deliberately disabled Android's dynamic wallpaper color override by default so status badges, error messages, and surface hierarchy never shift unpredictably across different phones.
Shared primitives—editorial uppercase section labels, hairline-bordered cards, pill status badges, and a consistent top bar and bottom navigation shell—keep the Welcome, Login, Sign Up, Home, Symptoms, and Add Medication screens visually cohesive without repeating styling rules inside every composable.
健康应用总爱用高对比青色配惨白卡片,打开就像看化验单。在 MedTrack 的 Material 3 主题里我反着走了一步:浅色模式用暖沙配深墨,深色模式用可可配暖奶油,再用克制的暖棕、苔绿和铁锈红区分语义状态。1 我还刻意默认关掉 Android 动态壁纸取色覆盖,免得状态徽章、报错提示和层级表面在不同手机上乱漂。
一套共享基础组件(刊物感大写分节标签、细线描边卡片、胶囊状态徽章和统一的顶栏底栏外壳),让 Welcome、Login、Sign Up、Home、Symptoms、Add Medication 六个页面自然统一起来,不必在每个 Composable 里重复写样式。
Keeping navigation and validation pure with a reducer用纯 Reducer 守住导航与表单校验
A common trap in early Jetpack Compose apps is letting screens mutate state, validate text fields, talk to SharedPreferences, and trigger navigation all inside button click handlers. It works for two screens and turns brittle by the fifth. In MedTrack, I centralized application state into an immutable state tree driven by a single pure reducer: every user interaction dispatches a typed action, and the reducer returns both the next UI state and an optional explicit side-effect descriptor.2
That boundary keeps the core app completely deterministic. Navigating between Welcome, Login, Sign Up, and Dashboard—including Android's hardware back behavior, where backing out of Add Medication steps cleanly back to Home rather than exiting the dashboard—lives in pure Kotlin functions. Side effects such as opening the Monash Health clinic website, authenticating against patient records, registering a new account, persisting or clearing the active session ID in SharedPreferences, and showing snackbar confirmations run strictly at the outer app shell.
The payoff showed up immediately in form validation and testing. Instead of failing on the first invalid field and forcing a patient to submit a sign-up form four times, the pure validator evaluates every rule in one pass—checking blank name, 10-digit Australian mobile format after stripping punctuation, minimum eight-character passwords containing both a letter and a digit, confirmation match, and cross-source phone uniqueness—and returns independent flags for each error so the UI can clear them the moment the user edits that specific field.3
Keeping every form rule and screen transition inside a pure reducer turns an Android UI prototype into something you can reason about—and unit-test—without booting an emulator.
早期 Jetpack Compose 应用最容易踩的坑,是在按钮点击回调里同时改状态、校验输入框、读写 SharedPreferences 再跳页面。两个页面还能凑合,到第五个就开始到处漏风。在 MedTrack 里,我把全局状态收进一棵不可变状态树,交给单个纯 Reducer 驱动:每次交互派发类型化 Action,Reducer 返回下一份 UI 状态和可选的显式副作用描述。2
这道边界让核心逻辑完全确定。Welcome、Login、Sign Up 和 Dashboard 之间的跳转(包括 Android 返回键在 Add Medication 页退回 Home 而不是直接退掉仪表盘)全写在纯 Kotlin 函数里。打开 Monash Health 诊所网页、校验患者账号、注册新用户、向 SharedPreferences 写入或清掉会话 ID、弹 Snackbar 提示这些副作用,统统关在最外层应用壳里执行。
好处在表单校验和单测里立竿见影。纯校验器不会卡在第一个错字段、逼患者把注册表提交四遍,而是一遍跑完所有规则(空姓名、去标点后的 10 位澳大利亚手机号、至少 8 位且含字母数字的密码、确认密码一致、跨源手机号查重),返回各自独立的错误标记,患者一改对应输入框,报错立刻消失。3
把每条表单规则和页面跳转都关进纯 Reducer,Android 界面原型不用起模拟器也能推演明白、直接跑完单元测试。
Offline-first patient data that survives messy files离线优先,且扛得住凌乱 CSV 的数据层
MedTrack runs completely offline by combining bundled CSV assets (patients, medication schedules, and symptom logs) with locally registered accounts stored as JSON via Gson in SharedPreferences. On cold launch, the app checks for a persisted patient ID and hydrates that patient's sorted medication schedule and reverse-chronological symptom history straight into the Dashboard.
Even with bundled assets, hard-coding CSV column indices is a classic source of silent bugs whenever a schema shifts. Every repository parses its file by normalized header names—accepting variations like pid, patientid, or patient_id—skips malformed rows gracefully instead of crashing login, and synthesizes stable deterministic IDs for each medication and symptom row. When a new patient registers locally, the repository checks phone-number uniqueness across both the bundled CSV and local storage after stripping formatting characters, and generates collision-safe patient IDs so local accounts never collide with seeded records.4
MedTrack 把内置 CSV 资源(患者、用药计划、症状史)和用 Gson 存在 SharedPreferences 里的本地注册账号合在一起,彻底离线运行。冷启动时应用检查持久化患者 ID,把该患者按时间排好的用药清单和倒序症状史直接水合进 Dashboard。
哪怕用的是内置文件,硬编码 CSV 列号也是字段一变就静默出错的老坑。每个仓库都按归一化表头名解析(兼容 pid、patientid、patient_id 等写法),碰上残缺行优雅跳过而不崩掉登录,并为每条用药和症状合成稳定确定性 ID。新患者本地注册时,仓库去掉格式符号后跨内置 CSV 与本地存储双边查重,并生成防碰撞患者 ID,保证本地账号绝不撞上预置记录。4
Calm daily workflows, and what remains intentionally scoped安静的日常动线,以及坦诚的原型边界
Once signed in, the app splits into three focused destinations so daily check-ins never get buried under configuration menus. Home greets the patient by name, puts today's date in a single summary badge alongside a live progress bar so the header stays uncluttered, and lists each medication in scheduled-time order with dosage, frequency, formatted 12-hour time, notes, and a quick Taken switch.
The Symptoms screen pairs a structured entry card—category dropdown (Pain, Nausea, Dizziness, Fatigue, Headache, Skin Reaction, or Other), a discrete 1-to-10 severity slider with a live badge, optional multi-line notes, and a one-tap 'Use current date & time' shortcut—with a reverse-chronological history feed that defensively re-sorts entries even if upstream data arrives out of order. Add Medication keeps friction low by requiring only medication name and dosage, leaving frequency, scheduled time, formulation type (tablet, capsule, liquid, injection, topical, or other), and notes optional so a patient can capture partial details quickly and clear or save with immediate snackbar feedback.
I also want to be clear about where this prototype stops. Per the coursework scope, daily Taken toggles on Home stay intentionally local to that screen (resetting when you navigate away), and submitting the Symptoms or Add Medication forms validates, resets, and confirms via snackbar without writing back to a local database or scheduling background alarm notifications yet.5 Separating the pure reducer from the repository layer means swapping those static CSV assets and screen-local states for Room persistence and WorkManager reminders is a clean next step rather than a rewrite.
登录后应用只分三个聚焦页面,日常打卡不被埋进设置菜单。Home 用姓名问候患者,把今天日期收进一枚摘要徽章配一根实时进度条保持顶栏清爽,下方按计划时间列出每种药的剂量、频次、12 小时制时间、备注和一键「已服用」开关。
Symptoms 页把结构化录入卡(分类下拉:Pain、Nausea、Dizziness、Fatigue、Headache、Skin Reaction、Other,带实时徽章的 1–10 离散严重度滑块,可选多行备注,以及一键「使用当前日期时间」)和倒序历史流放一块,上游数据乱序到达也会防御性重排。Add Medication 尽量压低录入阻力,只必填药名和剂量,频次、时间、剂型(片剂、胶囊、液体、注射、外用、其他)与备注全留可选,方便先记个大概并获得即时 Snackbar 反馈。
原型停在哪我也想说清楚。按课程范围,Home 页的「已服用」开关刻意留在当前页本地状态(切走即重置),提交 Symptoms 或 Add Medication 表单会走完校验、重置并弹 Snackbar 确认,还没回写本地数据库或挂后台闹钟提醒。5 正因为把纯 Reducer 和仓库层切开了,往后把静态 CSV 和页面本地状态换成 Room 持久化与 WorkManager 提醒,是顺滑替换,不是推倒重来。
Notes注释
- MedTrackTheme supplies both light (SandBackground, InkForeground, AccentBrown, MossGreen, RustRed) and dark (CocoaBackground, CreamForeground) Material 3 color schemes alongside custom gradient brushes exposed through a CompositionLocal.↩︎MedTrackTheme 提供浅色(SandBackground、InkForeground、AccentBrown、MossGreen、RustRed)与深色(CocoaBackground、CreamForeground)两套 Material 3 配色,并通过 CompositionLocal 暴露自定义渐变画刷。↩︎
- reduceMedTrackState(state, action) returns a MedTrackUpdate(state, effect) covering OpenExternalUrl, AuthenticatePatient, RegisterPatient, ShowSnackbar, and ClearLoggedInPatientId—all handled inside MedTrackApp outside the pure reducer.↩︎reduceMedTrackState(state, action) 返回 MedTrackUpdate(state, effect),覆盖 OpenExternalUrl、AuthenticatePatient、RegisterPatient、ShowSnackbar 与 ClearLoggedInPatientId,全部在纯 Reducer 之外的 MedTrackApp 内执行。↩︎
- Phone numbers are normalized to digits before validating the 10-digit '04' prefix and comparing credentials, so spaces or hyphens in user input never cause false login failures or duplicate accounts.↩︎手机号在校验 10 位 '04' 前缀和比对凭据前先归一化为纯数字,用户输入的空格或连字符绝不会造成误判登录失败或重复注册。↩︎
- New local accounts receive timestamp-based IDs ('P' + epoch milliseconds with a zero-padded suffix on collision) and are merged seamlessly with bundled CSV records inside PatientAuthRepository.↩︎新建本地账号分配基于时间戳的 ID('P' + 毫秒时间戳,碰撞时补零后缀),并在 PatientAuthRepository 里与内置 CSV 记录无缝合并。↩︎
- Unit tests in MedTrackReducerTest verify login/sign-up validation, authentication rejection paths, destination switching, back-stack rules, and logout state cleanup directly on the JVM.↩︎MedTrackReducerTest 的单元测试直接在 JVM 上验证登录/注册校验、认证拒绝路径、目标页切换、返回栈规则与退出登录状态清理。↩︎
Built in Kotlin, Jetpack Compose, Material 3, and Gson for Android mobile application development, with pure reducer unit tests.使用 Kotlin、Jetpack Compose、Material 3 与 Gson 构建的 Android 移动应用原型,配有纯 Reducer 单元测试。
Stack: Kotlin, Jetpack Compose, Material 3, Android, Unidirectional State.技术栈:Kotlin、Jetpack Compose、Material 3、Android、Unidirectional State。