Skip to content

Fix: Sleep score not shown on app relaunch after extended background#20

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-sleep-score-issue
Draft

Fix: Sleep score not shown on app relaunch after extended background#20
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-sleep-score-issue

Conversation

Copilot AI commented Mar 21, 2026

Copy link
Copy Markdown

HomeSummaryStore cached sleep scores in memory only. After iOS terminates the app (memory pressure, extended background), that cache is gone — the score shows "–" until a fresh API round-trip completes, and any network failure leaves it permanently missing.

Changes

Models/HomeSummary.swift

  • Widened HomeSummary, SleepQualitySummary, FocusPredictionSummary, CircadianRhythmSummary, and ProcessingSummary from DecodableCodable to enable encoding for UserDefaults storage.

Services/HomeSummaryStore.swift

  • persistSummary(_:for:) / loadPersistedSummary(for:) — write/read JSON-encoded HomeSummary to UserDefaults keyed by date string (homeSummary.yyyy-MM-dd).
  • cacheSummary() write-through — every path that updates the in-memory cache (direct fetch, month-cache, historical sync, polling) now also persists to UserDefaults.
  • Warm-start in load() — seeds summary from UserDefaults before any network I/O, so the last-known score is visible instantly on relaunch:
// Before
summary = monthSummariesByDay[selectedMonthKey]?[selectedDayKey]

// After — falls back to durable cache when in-memory cache is cold
summary = monthSummariesByDay[selectedMonthKey]?[selectedDayKey]
       ?? loadPersistedSummary(for: selectedDayKey)

The month-cache path in load() now also calls cacheSummary() so bulk-fetched monthly summaries are persisted — previously they bypassed cacheSummary() entirely.

Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: Sleep score missing</issue_title>
<issue_description>Describe the bug
Sleep score is not shown on launch when it's already computed.

To Reproduce
Steps to reproduce the behavior:

  1. Wake up, view score after loading watch synced.
  2. Score is shown.
  3. Close app for 8 hours.
  4. Reopen application.
  5. Score is not shown.

Expected behavior
Score should be shown to user, even after closing the application for a prolonged period of time.

Additional context
Needs in-depth debugging to figure out.
Local storage should have the sleep score computed and stored. Need to figure out if it's even stored in local storage?
</issue_description>

<agent_instructions>Open pull request. Suggest best fixes.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

HomeSummary and its child structs are now Codable. HomeSummaryStore
seeds summary from UserDefaults when the in-memory cache is cold (on
app relaunch), and writes through to UserDefaults in cacheSummary() so
every score source (month-cache, direct fetch, historical sync, polling)
is persisted. The month-cache path in load() now calls cacheSummary()
so those summaries are also durably stored."

Co-authored-by: austinkimchi <40729751+austinkimchi@users.noreply.github.com>
Agent-Logs-Url: https://github.com/SleepFocus/SleepFocus_Frontend/sessions/9c610f16-b098-4e0f-96c7-8f8dc2bf14d3
Copilot AI changed the title [WIP] Fix bug where sleep score is missing after app relaunch Fix: Sleep score not shown on app relaunch after extended background Mar 21, 2026
Copilot AI requested a review from austinkimchi March 21, 2026 04:09
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.

Bug: Sleep score missing

2 participants