Fix boundary bugs, route side effects through MoodLogger, add data listeners
- Fix ExtensionDataProvider <= boundary to < in date queries (prevented cross-day leaks) - Replace force-unwraps with guards and add error logging in DataControllerGET and ExtensionDataProvider - Route DayViewViewModel update/delete through MoodLogger.shared (was duplicating side effects) - Add data listeners to InsightsViewModel and YearViewModel for cross-tab refresh - Add HealthKitManager.deleteMood(for:) for single-date cleanup - Add SharedModelContainer.isUsingInMemoryFallback flag with critical logging - Add analytics events: entryDeleted, allDataCleared, duplicatesRemoved, storageFallbackActivated Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,11 +7,17 @@
|
||||
|
||||
import SwiftData
|
||||
import Foundation
|
||||
import os.log
|
||||
|
||||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.tt.feels", category: "DataControllerGET")
|
||||
|
||||
extension DataController {
|
||||
func getEntry(byDate date: Date) -> MoodEntryModel? {
|
||||
let startDate = Calendar.current.startOfDay(for: date)
|
||||
let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate)!
|
||||
guard let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) else {
|
||||
logger.error("Failed to calculate end date for getEntry")
|
||||
return nil
|
||||
}
|
||||
|
||||
var descriptor = FetchDescriptor<MoodEntryModel>(
|
||||
predicate: #Predicate { entry in
|
||||
@@ -21,7 +27,12 @@ extension DataController {
|
||||
)
|
||||
descriptor.fetchLimit = 1
|
||||
|
||||
return try? modelContext.fetch(descriptor).first
|
||||
do {
|
||||
return try modelContext.fetch(descriptor).first
|
||||
} catch {
|
||||
logger.error("Failed to fetch entry: \(error.localizedDescription)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func getData(startDate: Date, endDate: Date, includedDays: [Int]) -> [MoodEntryModel] {
|
||||
@@ -36,7 +47,12 @@ extension DataController {
|
||||
sortBy: [SortDescriptor(\.forDate, order: .forward)]
|
||||
)
|
||||
|
||||
return (try? modelContext.fetch(descriptor)) ?? []
|
||||
do {
|
||||
return try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
logger.error("Failed to fetch entries: \(error.localizedDescription)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the current mood streak efficiently using a single batch query.
|
||||
|
||||
@@ -106,17 +106,25 @@ final class ExtensionDataProvider {
|
||||
/// Get a single entry for a specific date
|
||||
func getEntry(byDate date: Date) -> MoodEntryModel? {
|
||||
let startDate = Calendar.current.startOfDay(for: date)
|
||||
let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate)!
|
||||
guard let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) else {
|
||||
Self.logger.error("Failed to calculate end date for getEntry")
|
||||
return nil
|
||||
}
|
||||
|
||||
var descriptor = FetchDescriptor<MoodEntryModel>(
|
||||
predicate: #Predicate { entry in
|
||||
entry.forDate >= startDate && entry.forDate <= endDate
|
||||
entry.forDate >= startDate && entry.forDate < endDate
|
||||
},
|
||||
sortBy: [SortDescriptor(\.forDate, order: .forward)]
|
||||
)
|
||||
descriptor.fetchLimit = 1
|
||||
|
||||
return try? modelContext.fetch(descriptor).first
|
||||
do {
|
||||
return try modelContext.fetch(descriptor).first
|
||||
} catch {
|
||||
Self.logger.error("Failed to fetch entry: \(error.localizedDescription)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Get today's mood entry
|
||||
@@ -141,7 +149,12 @@ final class ExtensionDataProvider {
|
||||
sortBy: [SortDescriptor(\.forDate, order: .forward)]
|
||||
)
|
||||
|
||||
return (try? modelContext.fetch(descriptor)) ?? []
|
||||
do {
|
||||
return try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
Self.logger.error("Failed to fetch entries: \(error.localizedDescription)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the earliest entry in the database
|
||||
@@ -150,7 +163,12 @@ final class ExtensionDataProvider {
|
||||
sortBy: [SortDescriptor(\.forDate, order: .forward)]
|
||||
)
|
||||
descriptor.fetchLimit = 1
|
||||
return try? modelContext.fetch(descriptor).first
|
||||
do {
|
||||
return try modelContext.fetch(descriptor).first
|
||||
} catch {
|
||||
Self.logger.error("Failed to fetch earliest entry: \(error.localizedDescription)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the latest entry in the database
|
||||
@@ -159,14 +177,24 @@ final class ExtensionDataProvider {
|
||||
sortBy: [SortDescriptor(\.forDate, order: .reverse)]
|
||||
)
|
||||
descriptor.fetchLimit = 1
|
||||
return try? modelContext.fetch(descriptor).first
|
||||
do {
|
||||
return try modelContext.fetch(descriptor).first
|
||||
} catch {
|
||||
Self.logger.error("Failed to fetch latest entry: \(error.localizedDescription)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current streak count
|
||||
/// - Parameter includedDays: Weekdays to include in streak calculation (empty = all days)
|
||||
func getCurrentStreak(includedDays: [Int] = []) -> Int {
|
||||
guard let yearAgo = Calendar.current.date(byAdding: .day, value: -365, to: Date()) else {
|
||||
Self.logger.error("Failed to calculate year-ago date for streak")
|
||||
return 0
|
||||
}
|
||||
|
||||
let entries = getData(
|
||||
startDate: Calendar.current.date(byAdding: .day, value: -365, to: Date())!,
|
||||
startDate: yearAgo,
|
||||
endDate: Date(),
|
||||
includedDays: includedDays
|
||||
).sorted { $0.forDate > $1.forDate }
|
||||
@@ -179,7 +207,11 @@ final class ExtensionDataProvider {
|
||||
|
||||
if entryDate == currentDate && entry.mood != .missing && entry.mood != .placeholder {
|
||||
streak += 1
|
||||
currentDate = Calendar.current.date(byAdding: .day, value: -1, to: currentDate)!
|
||||
guard let previousDate = Calendar.current.date(byAdding: .day, value: -1, to: currentDate) else {
|
||||
Self.logger.error("Failed to calculate previous date in streak")
|
||||
break
|
||||
}
|
||||
currentDate = previousDate
|
||||
} else if entryDate < currentDate {
|
||||
break
|
||||
}
|
||||
@@ -202,7 +234,11 @@ final class ExtensionDataProvider {
|
||||
modelContext.delete(entry)
|
||||
}
|
||||
if !existing.isEmpty {
|
||||
try? modelContext.save()
|
||||
do {
|
||||
try modelContext.save()
|
||||
} catch {
|
||||
Self.logger.error("Failed to save after deleting existing entries: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
let entry = MoodEntryModel(
|
||||
@@ -227,16 +263,24 @@ final class ExtensionDataProvider {
|
||||
/// Get ALL entries for a specific date (not just the first one)
|
||||
func getAllEntries(byDate date: Date) -> [MoodEntryModel] {
|
||||
let startDate = Calendar.current.startOfDay(for: date)
|
||||
let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate)!
|
||||
guard let endDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) else {
|
||||
Self.logger.error("Failed to calculate end date for getAllEntries")
|
||||
return []
|
||||
}
|
||||
|
||||
let descriptor = FetchDescriptor<MoodEntryModel>(
|
||||
predicate: #Predicate { entry in
|
||||
entry.forDate >= startDate && entry.forDate <= endDate
|
||||
entry.forDate >= startDate && entry.forDate < endDate
|
||||
},
|
||||
sortBy: [SortDescriptor(\.forDate, order: .forward)]
|
||||
)
|
||||
|
||||
return (try? modelContext.fetch(descriptor)) ?? []
|
||||
do {
|
||||
return try modelContext.fetch(descriptor)
|
||||
} catch {
|
||||
Self.logger.error("Failed to fetch all entries: \(error.localizedDescription)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/// Invalidate cached container (call when data might have changed)
|
||||
|
||||
@@ -27,6 +27,10 @@ enum SharedModelContainerError: LocalizedError {
|
||||
enum SharedModelContainer {
|
||||
private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.tt.feels", category: "SharedModelContainer")
|
||||
|
||||
/// Indicates whether the app is running with in-memory storage due to a failed App Group container.
|
||||
/// When `true`, all data will be lost on app restart.
|
||||
static private(set) var isUsingInMemoryFallback = false
|
||||
|
||||
/// Creates a ModelContainer with the appropriate configuration for app group sharing
|
||||
/// - Parameter useCloudKit: Whether to enable CloudKit sync (defaults to true)
|
||||
/// - Returns: Configured ModelContainer
|
||||
@@ -68,6 +72,11 @@ enum SharedModelContainer {
|
||||
return try create(useCloudKit: useCloudKit)
|
||||
} catch {
|
||||
logger.warning("Falling back to in-memory storage due to: \(error.localizedDescription)")
|
||||
logger.critical("App is using in-memory storage — all mood data will be lost on restart. App Group container failed: \(error.localizedDescription)")
|
||||
#if DEBUG
|
||||
assertionFailure("SharedModelContainer fell back to in-memory storage. App Group container is unavailable: \(error.localizedDescription)")
|
||||
#endif
|
||||
isUsingInMemoryFallback = true
|
||||
// Fall back to in-memory storage
|
||||
let schema = Schema([MoodEntryModel.self])
|
||||
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
|
||||
|
||||
Reference in New Issue
Block a user