Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions SleepFocus/Models/HomeSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ nonisolated struct HomeSummaryMonthPayload: Decodable {
let updatedAt: String
}

nonisolated struct HomeSummary: Decodable {
nonisolated struct HomeSummary: Codable {
let userId: String
let selectedDate: String?
let sleepQuality: SleepQualitySummary
Expand All @@ -29,25 +29,25 @@ nonisolated struct HomeSummary: Decodable {
let processing: ProcessingSummary?
}

nonisolated struct SleepQualitySummary: Decodable {
nonisolated struct SleepQualitySummary: Codable {
let score: Double?
let maxScore: Double?
let status: String?
let source: String?
}

nonisolated struct FocusPredictionSummary: Decodable {
nonisolated struct FocusPredictionSummary: Codable {
let score: Int?
let label: String?
let source: String?
}

nonisolated struct CircadianRhythmSummary: Decodable {
nonisolated struct CircadianRhythmSummary: Codable {
let averageWakeTimeMinutes: Int
let confidence: Double
}

nonisolated struct ProcessingSummary: Decodable {
nonisolated struct ProcessingSummary: Codable {
let state: String
let lastJobId: String?
let updatedAt: String?
Expand Down
22 changes: 21 additions & 1 deletion SleepFocus/Services/HomeSummaryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class HomeSummaryStore: ObservableObject {

isWaitingForModel = false
isSyncing = false
summary = monthSummariesByDay[selectedMonthKey]?[selectedDayKey]
summary = monthSummariesByDay[selectedMonthKey]?[selectedDayKey] ?? loadPersistedSummary(for: selectedDayKey)
sleepMetrics = nil
sleepStageBreakdown = nil

Expand All @@ -56,6 +56,7 @@ final class HomeSummaryStore: ObservableObject {
var resolvedSummary = monthSummariesByDay[selectedMonthKey]?[selectedDayKey]
if let resolvedSummary {
summary = resolvedSummary
cacheSummary(resolvedSummary, fallbackDate: selectedDate)
}

if resolvedSummary == nil {
Expand Down Expand Up @@ -195,6 +196,25 @@ final class HomeSummaryStore: ObservableObject {
var monthMap = monthSummariesByDay[monthKey] ?? [:]
monthMap[dayKey] = summary
monthSummariesByDay[monthKey] = monthMap

persistSummary(summary, for: dayKey)
}

private func loadPersistedSummary(for dayKey: String) -> HomeSummary? {
guard let data = UserDefaults.standard.data(forKey: Self.persistenceKey(for: dayKey)),
let decoded = try? JSONDecoder().decode(HomeSummary.self, from: data) else {
return nil
}
return decoded
}

private func persistSummary(_ summary: HomeSummary, for dayKey: String) {
guard let data = try? JSONEncoder().encode(summary) else { return }
UserDefaults.standard.set(data, forKey: Self.persistenceKey(for: dayKey))
}

private static func persistenceKey(for dayKey: String) -> String {
"homeSummary.\(dayKey)"
}

// Ramp up polling to improve fetching performance without spamming backend
Expand Down
Loading