Skip to content

Refactor/routine viewmodel - #31

Open
ChrisC392 wants to merge 4 commits into
mainfrom
refactor/routine-viewmodel
Open

ChrisC392 wants to merge 4 commits into
mainfrom
refactor/routine-viewmodel

Conversation

@ChrisC392

Copy link
Copy Markdown
Contributor

No description provided.

SunlightActivity held ten Compose state fields, the routine, the debug
clock, the tick loop, DataStore access, both pickers, the camera
permission flow and window control, with TooManyFunctions suppressed.
Because the session lived in Activity fields, rotation or process death
discarded an in-progress routine, and a one-shot settings read in
onCreate could overwrite a wake-up time saved before it finished.

- SunlightViewModel owns the RoutineSession and one StateFlow of
  SunlightScreenState. Phase is derived from MorningRoutine (SETUP means
  no session) and the session is mirrored into SavedStateHandle after
  each change, so it survives configuration changes and process death.
- RoutineSession fixes a RoutineClock (wall or DebugRoutineClock over
  DebugClock) at start; debug tests ignore camera frames.
- SettingsRepository wraps the existing DataStore keys and validation
  and exposes a Flow, removing the load race.
- The Activity is a thin host: native pickers (NativePickers.kt),
  display mode driven by state (FocusDisplay.kt), camera permission and
  binding. runClock() runs under repeatOnLifecycle(RESUMED), so time
  still advances only while the preview is resumed.
- Camera luminance is computed off the main thread.

No new dependencies. Recorded in ADR-003.
onCameraEvent(PERMISSION_MISSING) always published state with a new
display time. The Activity reports it from its state collector, so each
report caused the next without suspending. On an Android 17 emulator
this ran about 256,000 times over 9 seconds on the main thread and held
up the permission prompt.

onLightReading ran the full update for every frame, saving the session
and recomposing many times a second even though the state only changes
when the phase does.

- A permission condition already in state is no longer republished.
- Frames publish only when they change the phase. The announced phase
  is tracked separately so the bright-light notice still appears.
Starting a routine now returns early for any active session, so a real
start can no longer replace a running countdown. The debug path already
had this check.

An out-of-range duration is ignored by the ViewModel instead of being
coerced to the default, which would have overwritten a valid saved
value. The repository's duplicate coercion and its misleading test are
removed, and ADR-003's dependency note is corrected.

@Da-man525 Da-man525 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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.

2 participants