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) <noreply@anthropic.com>
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user