From c1c824f288854f01e795f256b2b5aaf629d41513 Mon Sep 17 00:00:00 2001 From: Trey t Date: Sun, 12 Apr 2026 10:13:08 -0500 Subject: [PATCH] fix: completed care tasks reappearing as overdue after reopening When a user marked a care task as complete, the task would disappear from the upcoming tasks section. However, upon navigating away and returning to the plant detail, the task would reappear as incomplete and overdue. The root cause was that PlantDetailView only used .task to load schedule data, which runs once on first appearance. When the view was recreated (e.g., after navigating back from the collection list), the Core Data fetch could return stale data due to context isolation in NSPersistentCloudKitContainer. Added .onAppear to reload the care schedule from Core Data every time the view appears, matching the pattern already used in TodayView. Also exposed a refreshSchedule() method on the ViewModel for this purpose. Fixes #2 Co-Authored-By: Claude Opus 4.6 (1M context) --- .../Presentation/Scenes/PlantDetail/PlantDetailView.swift | 8 ++++++++ .../Scenes/PlantDetail/PlantDetailViewModel.swift | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailView.swift b/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailView.swift index 4324ed6..7590433 100644 --- a/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailView.swift +++ b/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailView.swift @@ -116,6 +116,14 @@ struct PlantDetailView: View { .task { await viewModel.loadCareInfo() } + .onAppear { + // Reload schedule from Core Data every time the view appears. + // .task only runs on first appearance; this ensures task completion + // states are always current when navigating back to this view. + Task { + await viewModel.refreshSchedule() + } + } .refreshable { await viewModel.refresh() } diff --git a/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailViewModel.swift b/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailViewModel.swift index cd1db0b..0200b63 100644 --- a/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailViewModel.swift +++ b/PlantGuide/Presentation/Scenes/PlantDetail/PlantDetailViewModel.swift @@ -144,6 +144,12 @@ final class PlantDetailViewModel { isLoading = false } + /// Reloads the care schedule from the repository. + /// Call on view reappearance to pick up persisted task completions. + func refreshSchedule() async { + await loadExistingSchedule() + } + /// Loads an existing care schedule from the repository private func loadExistingSchedule() async { do {