Skip to content

Sleep: prompt to extend early-ended night records bounded by Bedtime - #149

Open
foureight84 wants to merge 3 commits into
mainfrom
feat/148-bedtime-sleep-tail-prompt
Open

foureight84 wants to merge 3 commits into
mainfrom
feat/148-bedtime-sleep-tail-prompt

Conversation

@foureight84

Copy link
Copy Markdown
Owner

Closes #148

Summary

When a YCBT ring closes its final sleep record early while Android Bedtime/DND stays active with no steps before Bedtime ends, lying still looks identical to an idle ring sensor. Rather than modifying the night silently, this adds a confirmable "Were you still asleep?" prompt on the Sleep screen that hands off to the existing SleepRecordEditDialog (#106) pre-filled with the suggested end time.

Behavior & guardrails

  • Detector (SleepTailSuggestionDetector):
    • Requires active Bedtime/DND windows and picks the closed Bedtime window containing/spanning the last ring record's end (never fires while Bedtime is still open).
    • Waits until lastFullSyncAt >= bedtimeEnd (when a device sync timestamp is present) and observes deviceDao().currentFlow() lastFullSyncAt in SleepViewModel.init so the Sleep tab updates immediately once a post-Bedtime sync completes (even when no new sleep rows arrived).
    • Bounds the gap between lastRun.endAt and bedtimeEnd to 15–120 minutes (MAX_OUTWARD_EXTENSION_MINUTES).
    • Suppresses if any non-zero ActivityBucketEntity (15-min bucket) lies wholly inside (lastRun.endAt .. bedtimeEnd] (startEpoch >= lastRun.endAt && startEpoch + 15m <= bedtimeEnd), ignoring pre-end get-up buckets and wake-up straddling buckets.
    • Skips demo sessions, already-extended (ADDED) or manually edited (MeasurementDeletionDao.getSleepEdit) records, dismissed records, and cases where another non-demo session overlaps (lastRun.endAt, bedtimeEnd).
    • Snaps suggestedEndAt onto the ring record's minute grid and enforces recordEditLimits (120m outward cap & 7 PM waking-day boundary via clampEndPick), rechecking the 15-min minimum after clamping.
  • Dismissal store (SleepTailSuggestionStore):
    • Persists per-record "No" dismissals keyed by sessionDate_recordStartAt with 45-day pruning and immutable StringSet copies.
  • UI & l10n (SleepScreen, EN + RU):
    • Renders SleepTailSuggestionCard under SessionHero on matching Day-view single or carousel pages.
    • Tapping Extend to Y opens SleepRecordEditDialog with initialEndAt = suggestedEndAt (keeping the dialog editable and immediately saveable); tapping No hides and remembers dismissal for that record.
  • Tests: Added 15 JVM unit tests in SleepTailSuggestionTest covering reporter examples (Oct 12 / Nov 3 / Nov 15), interior step suppression & bucket boundaries, Bedtime/sync/gap/edit/dismissal/demo/multi-session/7 PM-clamp edge cases, and store key pruning.

When the ring closes its last sleep record early while Android Bedtime
stays active for 15-120 minutes with no steps after a post-Bedtime full
sync, surface a confirmable prompt on the Sleep screen ("Were you still
asleep?") instead of extending silently. Tapping Extend opens the
existing SleepRecordEditDialog pre-filled with the Bedtime end time
(snapped to the record's minute grid and clamped to 7 PM / 120m limits);
tapping No dismisses the prompt for that ring record (pruned at 45d).
…il prompt

Fix SleepScore.calculate's span calculation when ADDED blocks are
present: measure span across the ring's own measured blocks instead of
session.spanMinutes, so outward extensions keep the same coverage ratio
on 0-awake nights (fixing the 81 -> 75 score drop on Nov 16) and don't
dilute awakePct, while preserving interior between-record gaps (#106).

Enrich SleepTailSuggestion with non-demo heart-rate samples inside the
suggested extension gap and the night's mean HR so SleepTailSuggestionCard
can surface readings like "Heart rate at 08:00: 58 bpm · night avg 59 bpm"
(or compact list / avg+range summary when denser HR sampling is present).
…and UI

Address independent review findings across YCBT sync, tail detection,
ViewModel concurrency, and Sleep UI:
- Prevent partial YCBT syncs (syncSleepNow, querySleep, syncVitalsHistory)
  from emitting HistorySyncFinished so lastFullSyncAt only advances once
  SPORT and HEART history have synced too.
- Require non-null lastFullSyncAt >= bedtimeEnd, intraday step-bucket
  support with family-aware bucket duration (1m JRing, 15m YCBT/Colmi),
  at least one aligned observable bucket inside the gap, and no deleted
  trailing phantom record in the gap.
- Prefer primary/longest sleep session over daytime DND naps, check
  session overlaps through grid-rounded suggestedEndAt, serialize
  SleepViewModel rebuild() jobs to prevent dismissal resurrection or
  day/range races, deduplicate night HR per minute, exclude post-Bedtime
  HR, show raw bedtimeEndedAt in body copy, and use FlowRow for actions.
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.

Sleep: the ring sometimes ends the night early, could Bedtime help catch it?

1 participant