Fix residence detail caching to check both data sources
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -393,11 +393,21 @@ object APILayer {
|
||||
}
|
||||
|
||||
suspend fun getResidence(id: Int, forceRefresh: Boolean = false): ApiResult<ResidenceResponse> {
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user