Fix 7 data mutation layer risks identified in audit
- save()/saveAndRunDataListeners() now return @discardableResult Bool; listeners only fire on successful save - MoodLogger.updateMood() now recalculates streak, updates Live Activity, and notifies watch (was missing these side effects) - CSV import uses new addBatch()/importMoods() for O(1) side effects instead of O(n) per-row widget reloads and streak calcs - Foreground task ordering: fillInMissingDates() now runs before removeDuplicates() so backfill-created duplicates are caught same cycle - WidgetMoodSaver deletes ALL entries for date (was fetchLimit=1, leaving CloudKit sync duplicates behind) - cleanupPhotoIfNeeded logs warning on failed photo deletion instead of silently orphaning files Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,16 +7,24 @@
|
||||
|
||||
import SwiftData
|
||||
import Foundation
|
||||
import os.log
|
||||
|
||||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.tt.feels", category: "DataControllerADD")
|
||||
|
||||
extension DataController {
|
||||
func add(mood: Mood, forDate date: Date, entryType: EntryType) {
|
||||
// Delete ALL existing entries for this date (handles duplicates)
|
||||
let existing = getAllEntries(byDate: date)
|
||||
for entry in existing {
|
||||
cleanupPhotoIfNeeded(for: entry)
|
||||
modelContext.delete(entry)
|
||||
}
|
||||
if !existing.isEmpty {
|
||||
try? modelContext.save()
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
logger.error("Failed to save after deleting existing entries: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
let entry = MoodEntryModel(
|
||||
@@ -34,21 +42,32 @@ extension DataController {
|
||||
let currentOnboarding = UserDefaultsStore.getOnboarding()
|
||||
var endDate = ShowBasedOnVoteLogics.getCurrentVotingDate(onboardingData: currentOnboarding)
|
||||
// Since it's for views, take away the last date so vote is enabled
|
||||
endDate = Calendar.current.date(byAdding: .day, value: -1, to: endDate)!
|
||||
guard let adjustedEndDate = Calendar.current.date(byAdding: .day, value: -1, to: endDate) else {
|
||||
logger.error("Failed to calculate adjusted end date")
|
||||
return
|
||||
}
|
||||
endDate = adjustedEndDate
|
||||
|
||||
let descriptor = FetchDescriptor<MoodEntryModel>(
|
||||
sortBy: [SortDescriptor(\.forDate, order: .reverse)]
|
||||
)
|
||||
|
||||
guard let entries = try? modelContext.fetch(descriptor),
|
||||
let firstEntry = entries.last else { return }
|
||||
|
||||
let allDates: [Date] = Date.dates(from: firstEntry.forDate, toDate: endDate, includingToDate: true).map {
|
||||
Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0)!
|
||||
let entries: [MoodEntryModel]
|
||||
do {
|
||||
entries = try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
logger.error("Failed to fetch entries for fill-in: \(error.localizedDescription)")
|
||||
return
|
||||
}
|
||||
|
||||
let existingDates: Set<Date> = Set(entries.map {
|
||||
Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0.forDate)!
|
||||
guard let firstEntry = entries.last else { return }
|
||||
|
||||
let allDates: [Date] = Date.dates(from: firstEntry.forDate, toDate: endDate, includingToDate: true).compactMap {
|
||||
Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0)
|
||||
}
|
||||
|
||||
let existingDates: Set<Date> = Set(entries.compactMap {
|
||||
Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: $0.forDate)
|
||||
})
|
||||
|
||||
let missing = Array(Set(allDates).subtracting(existingDates)).sorted(by: >)
|
||||
@@ -58,7 +77,10 @@ extension DataController {
|
||||
// Batch insert all missing dates without triggering listeners
|
||||
for date in missing {
|
||||
// Add 12 hours to avoid UTC offset issues
|
||||
let adjustedDate = Calendar.current.date(byAdding: .hour, value: 12, to: date)!
|
||||
guard let adjustedDate = Calendar.current.date(byAdding: .hour, value: 12, to: date) else {
|
||||
logger.error("Failed to calculate adjusted date for \(date)")
|
||||
continue
|
||||
}
|
||||
let entry = MoodEntryModel(
|
||||
forDate: adjustedDate,
|
||||
mood: .missing,
|
||||
@@ -77,11 +99,26 @@ extension DataController {
|
||||
for entry in data {
|
||||
entry.weekDay = Calendar.current.component(.weekday, from: entry.forDate)
|
||||
}
|
||||
save()
|
||||
saveAndRunDataListeners()
|
||||
}
|
||||
|
||||
func removeNoForDates() {
|
||||
// Note: With SwiftData's non-optional forDate, this is essentially a no-op
|
||||
// Keeping for API compatibility
|
||||
}
|
||||
|
||||
/// Batch insert mood entries without per-entry analytics or listener notifications.
|
||||
/// Used for CSV import where side effects should fire once at the end.
|
||||
func addBatch(entries: [(mood: Mood, date: Date, entryType: EntryType)]) {
|
||||
for (mood, date, entryType) in entries {
|
||||
let existing = getAllEntries(byDate: date)
|
||||
for entry in existing {
|
||||
cleanupPhotoIfNeeded(for: entry)
|
||||
modelContext.delete(entry)
|
||||
}
|
||||
let entry = MoodEntryModel(forDate: date, mood: mood, entryType: entryType)
|
||||
modelContext.insert(entry)
|
||||
}
|
||||
saveAndRunDataListeners()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user