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:
@@ -377,6 +377,9 @@ extension AnalyticsManager {
|
||||
case photoAdded
|
||||
case photoDeleted
|
||||
case missingEntriesFilled(count: Int)
|
||||
case entryDeleted(mood: Int)
|
||||
case allDataCleared
|
||||
case duplicatesRemoved(count: Int)
|
||||
|
||||
// MARK: Customization
|
||||
case themeChanged(themeId: String)
|
||||
@@ -448,6 +451,9 @@ extension AnalyticsManager {
|
||||
// MARK: Sharing
|
||||
case shareTemplateViewed(template: String)
|
||||
|
||||
// MARK: Error
|
||||
case storageFallbackActivated
|
||||
|
||||
// MARK: Legal
|
||||
case eulaViewed
|
||||
case privacyPolicyViewed
|
||||
@@ -468,6 +474,12 @@ extension AnalyticsManager {
|
||||
return ("photo_deleted", nil)
|
||||
case .missingEntriesFilled(let count):
|
||||
return ("missing_entries_filled", ["count": count])
|
||||
case .entryDeleted(let mood):
|
||||
return ("entry_deleted", ["mood": mood])
|
||||
case .allDataCleared:
|
||||
return ("all_data_cleared", nil)
|
||||
case .duplicatesRemoved(let count):
|
||||
return ("duplicates_removed", ["count": count])
|
||||
|
||||
// Customization
|
||||
case .themeChanged(let id):
|
||||
@@ -591,6 +603,10 @@ extension AnalyticsManager {
|
||||
case .shareTemplateViewed(let template):
|
||||
return ("share_template_viewed", ["template": template])
|
||||
|
||||
// Error
|
||||
case .storageFallbackActivated:
|
||||
return ("storage_fallback_activated", nil)
|
||||
|
||||
// Legal
|
||||
case .eulaViewed:
|
||||
return ("eula_viewed", nil)
|
||||
|
||||
@@ -311,6 +311,38 @@ class HealthKitManager: ObservableObject {
|
||||
return deletedCount
|
||||
}
|
||||
|
||||
// MARK: - Delete Mood for Date from HealthKit
|
||||
|
||||
/// Deletes State of Mind samples created by this app for a specific date
|
||||
/// Note: HealthKit only allows deleting samples that your app created
|
||||
func deleteMood(for date: Date) async throws {
|
||||
guard isHealthKitAvailable else {
|
||||
throw HealthKitError.notAvailable
|
||||
}
|
||||
|
||||
guard let stateOfMindType = stateOfMindType else {
|
||||
throw HealthKitError.typeNotAvailable
|
||||
}
|
||||
|
||||
guard checkAuthorizationStatus() == .sharingAuthorized else {
|
||||
throw HealthKitError.notAuthorized
|
||||
}
|
||||
|
||||
let calendar = Calendar.current
|
||||
let dayStart = calendar.startOfDay(for: date)
|
||||
guard let dayEnd = calendar.date(byAdding: .day, value: 1, to: dayStart) else { return }
|
||||
|
||||
let samples = try await fetchMoods(from: dayStart, to: dayEnd)
|
||||
|
||||
guard !samples.isEmpty else {
|
||||
logger.info("No State of Mind samples found for \(date) to delete")
|
||||
return
|
||||
}
|
||||
|
||||
try await healthStore.delete(samples)
|
||||
logger.info("Deleted \(samples.count) State of Mind samples for \(date)")
|
||||
}
|
||||
|
||||
// MARK: - Read Mood from HealthKit
|
||||
|
||||
func fetchMoods(from startDate: Date, to endDate: Date) async throws -> [HKStateOfMind] {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
import WidgetKit
|
||||
|
||||
@MainActor
|
||||
class DayViewViewModel: ObservableObject {
|
||||
@@ -61,25 +60,8 @@ class DayViewViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
public func update(entry: MoodEntryModel, toMood mood: Mood) {
|
||||
if !DataController.shared.update(entryDate: entry.forDate, withMood: mood) {
|
||||
if !MoodLogger.shared.updateMood(entryDate: entry.forDate, withMood: mood) {
|
||||
print("Failed to update mood entry")
|
||||
return
|
||||
}
|
||||
|
||||
// Sync to HealthKit for past day updates (only if user has full access)
|
||||
guard mood != .missing && mood != .placeholder else { return }
|
||||
|
||||
let healthKitEnabled = GroupUserDefaults.groupDefaults.bool(forKey: UserDefaultsStore.Keys.healthKitEnabled.rawValue)
|
||||
let hasAccess = !IAPManager.shared.shouldShowPaywall
|
||||
if healthKitEnabled && hasAccess {
|
||||
Task {
|
||||
try? await HealthKitManager.shared.saveMood(mood, for: entry.forDate)
|
||||
}
|
||||
}
|
||||
|
||||
// Reload widgets asynchronously to avoid UI delay
|
||||
Task { @MainActor in
|
||||
WidgetCenter.shared.reloadAllTimelines()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,13 +77,9 @@ class DayViewViewModel: ObservableObject {
|
||||
entriesToDelete.append(obj)
|
||||
}
|
||||
entriesToDelete.forEach({ entry in
|
||||
let entryDate = entry.forDate
|
||||
DataController.shared.modelContext.delete(entry)
|
||||
self.add(mood: .missing, forDate: entryDate, entryType: .listView)
|
||||
MoodLogger.shared.deleteMood(forDate: entry.forDate)
|
||||
})
|
||||
}
|
||||
|
||||
DataController.shared.save()
|
||||
}
|
||||
|
||||
static func updateTitleHeader(forEntry entry: MoodEntryModel?) -> String {
|
||||
|
||||
@@ -51,6 +51,19 @@ class InsightsViewModel: ObservableObject {
|
||||
|
||||
init() {
|
||||
isAIAvailable = insightService.isAvailable
|
||||
|
||||
DataController.shared.addNewDataListener { [weak self] in
|
||||
self?.onDataChanged()
|
||||
}
|
||||
}
|
||||
|
||||
/// Called when mood data changes in another tab. Invalidates cached insights
|
||||
/// so they are regenerated with fresh data on next view appearance.
|
||||
private func onDataChanged() {
|
||||
insightService.invalidateCache()
|
||||
monthLoadingState = .idle
|
||||
yearLoadingState = .idle
|
||||
allTimeLoadingState = .idle
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
@@ -25,9 +25,19 @@ class YearViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
init() {
|
||||
DataController.shared.addNewDataListener { [weak self] in
|
||||
self?.refreshData()
|
||||
}
|
||||
updateData()
|
||||
}
|
||||
|
||||
/// Re-fetch data using the current date range. Called by the data listener
|
||||
/// when mood entries change in other tabs.
|
||||
public func refreshData() {
|
||||
updateData()
|
||||
filterEntries(startDate: entryStartDate, endDate: entryEndDate)
|
||||
}
|
||||
|
||||
private func updateData() {
|
||||
let filteredEntries = DataController.shared.getData(startDate: Date(timeIntervalSince1970: 0),
|
||||
endDate: Date(),
|
||||
|
||||
Reference in New Issue
Block a user