import Foundation import SwiftData extension ModelContext { /// Toggles completion for a habit on a given day: removes the entry if it /// exists, otherwise inserts one normalized to the start of that day. func toggleCompletion(for habit: Habit, on day: Date) { let calendar = Calendar.current if let entry = habit.entries.first(where: { calendar.isDate($0.date, inSameDayAs: day) }) { delete(entry) } else { let entry = HabitEntry(date: calendar.startOfDay(for: day), habit: habit) insert(entry) } WidgetSync.refresh(self) } }