From 7d76393e40d4429a821f98c24fb774a8799386c3 Mon Sep 17 00:00:00 2001 From: Trey t Date: Wed, 17 Dec 2025 14:00:28 -0600 Subject: [PATCH] Fix residence detail caching to check both data sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - APILayer.getResidence() now checks both residences and myResidences caches - Home screen loads myResidences, but getResidence() only checked residences, causing unnecessary network calls on every residence detail visit - Remove flawed iOS-side cache check in TaskViewModel that had race condition with Kotlin StateFlow - let Kotlin handle all caching logic 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../com/example/casera/network/APILayer.kt | 20 ++++++++++++++----- iosApp/iosApp/Task/TaskViewModel.swift | 16 +-------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/example/casera/network/APILayer.kt b/composeApp/src/commonMain/kotlin/com/example/casera/network/APILayer.kt index 77a0cfd..420c57c 100644 --- a/composeApp/src/commonMain/kotlin/com/example/casera/network/APILayer.kt +++ b/composeApp/src/commonMain/kotlin/com/example/casera/network/APILayer.kt @@ -393,11 +393,21 @@ object APILayer { } suspend fun getResidence(id: Int, forceRefresh: Boolean = false): ApiResult { - // Check DataManager first - return cached if valid and not forcing refresh - if (!forceRefresh && DataManager.isCacheValid(DataManager.residencesCacheTime)) { - val cached = DataManager.residences.value.find { it.id == id } - if (cached != null) { - return ApiResult.Success(cached) + // Check DataManager caches first - return cached if valid and not forcing refresh + if (!forceRefresh) { + // Check residences list + if (DataManager.isCacheValid(DataManager.residencesCacheTime)) { + val cached = DataManager.residences.value.find { it.id == id } + if (cached != null) { + return ApiResult.Success(cached) + } + } + // Also check myResidences (loaded from home screen) + if (DataManager.isCacheValid(DataManager.myResidencesCacheTime)) { + val cached = DataManager.myResidences.value?.residences?.find { it.id == id } + if (cached != null) { + return ApiResult.Success(cached) + } } } diff --git a/iosApp/iosApp/Task/TaskViewModel.swift b/iosApp/iosApp/Task/TaskViewModel.swift index 1f0aa8b..67a4446 100644 --- a/iosApp/iosApp/Task/TaskViewModel.swift +++ b/iosApp/iosApp/Task/TaskViewModel.swift @@ -300,7 +300,7 @@ class TaskViewModel: ObservableObject { } /// Load tasks - either all tasks or filtered by residence - /// Checks cache first, then fetches if needed. + /// Kotlin's APILayer handles caching - returns cached data if valid without network call. /// - Parameters: /// - residenceId: Optional residence ID to filter by. If nil, loads all tasks. /// - forceRefresh: Whether to bypass cache @@ -309,20 +309,6 @@ class TaskViewModel: ObservableObject { currentResidenceId = residenceId tasksError = nil - - // Check if we have cached data and don't need to refresh - if !forceRefresh { - if let resId = residenceId { - if DataManagerObservable.shared.tasksByResidence[resId] != nil { - // Data already available via observation, no API call needed - return - } - } else if DataManagerObservable.shared.allTasks != nil { - // Data already available via observation, no API call needed - return - } - } - isLoadingTasks = true // Kick off API call - DataManager will be updated, which updates DataManagerObservable,