Stabilize planner UI, fix reset flow, and force Premium on TestFlight
This commit is contained in:
@@ -429,6 +429,33 @@ final class HomeViewModel: ObservableObject {
|
||||
}
|
||||
}()
|
||||
|
||||
// Clean up any legacy duplicates for the same day+mealType key to keep UI mapping stable.
|
||||
var groupedByKey: [SlotKey: [MealSlot]] = [:]
|
||||
for slot in plan.slots {
|
||||
let key = SlotKey(dayOfWeek: slot.dayOfWeek, mealType: slot.mealType)
|
||||
groupedByKey[key, default: []].append(slot)
|
||||
}
|
||||
|
||||
var changed = false
|
||||
for (_, duplicates) in groupedByKey where duplicates.count > 1 {
|
||||
let keeper = MealSlot.preferredForDuplicateResolution(duplicates)
|
||||
for duplicate in duplicates where duplicate.id != keeper.id {
|
||||
if keeper.dishId == nil, let dishId = duplicate.dishId {
|
||||
keeper.dishId = dishId
|
||||
keeper.isRuleOverridden = duplicate.isRuleOverridden
|
||||
if keeper.calendarEventId == nil {
|
||||
keeper.calendarEventId = duplicate.calendarEventId
|
||||
}
|
||||
} else if let duplicateEventId = duplicate.calendarEventId {
|
||||
CalendarService.shared.deleteEvent(eventId: duplicateEventId)
|
||||
}
|
||||
|
||||
plan.slots.removeAll { $0.id == duplicate.id }
|
||||
context.delete(duplicate)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
var desired = Set<SlotKey>()
|
||||
for day in 0...maxDay {
|
||||
for mealType in mealTypes {
|
||||
@@ -443,7 +470,6 @@ final class HomeViewModel: ObservableObject {
|
||||
existingByKey[key] = slot
|
||||
}
|
||||
}
|
||||
var changed = false
|
||||
|
||||
let toDelete = plan.slots.filter { slot in
|
||||
!desired.contains(SlotKey(dayOfWeek: slot.dayOfWeek, mealType: slot.mealType))
|
||||
|
||||
@@ -68,26 +68,43 @@ final class SettingsViewModel: ObservableObject {
|
||||
|
||||
func resetAllData(context: ModelContext) {
|
||||
do {
|
||||
// WeekPlan cascades MealSlot, so deleting both can corrupt the reset flow.
|
||||
try deleteAll(of: WeekPlan.self, in: context)
|
||||
try deleteAll(of: Dish.self, in: context)
|
||||
try deleteAll(of: Tag.self, in: context)
|
||||
DefaultDataService.createDefaultTags(context: context, saveImmediately: false)
|
||||
|
||||
let settingsDescriptor = FetchDescriptor<AppSettings>()
|
||||
let targetSettings: AppSettings
|
||||
|
||||
if let existingSettings = try context.fetch(settingsDescriptor).first {
|
||||
resetSettings(existingSettings)
|
||||
targetSettings = existingSettings
|
||||
} else {
|
||||
DefaultDataService.createDefaultSettings(context: context)
|
||||
if let createdSettings = try context.fetch(settingsDescriptor).first {
|
||||
resetSettings(createdSettings)
|
||||
}
|
||||
let created = AppSettings()
|
||||
context.insert(created)
|
||||
targetSettings = created
|
||||
}
|
||||
|
||||
// Phase 1: switch app flow out of Home before destructive deletes.
|
||||
resetSettings(targetSettings)
|
||||
try context.save()
|
||||
|
||||
Task { @MainActor in
|
||||
do {
|
||||
try? await Task.sleep(nanoseconds: 200_000_000)
|
||||
|
||||
try deleteAll(of: WeekPlan.self, in: context)
|
||||
try deleteAll(of: Dish.self, in: context)
|
||||
try deleteAll(of: Tag.self, in: context)
|
||||
DefaultDataService.createDefaultTags(context: context, saveImmediately: false)
|
||||
|
||||
// Ensure a true "fresh launch" onboarding flow.
|
||||
UserDefaults.standard.set(false, forKey: OnboardingViewModel.pendingAutoAssignPromptKey)
|
||||
UserDefaults.standard.set(false, forKey: OnboardingViewModel.pendingPremiumPromptKey)
|
||||
try context.save()
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("Failed to complete reset all data: \(error)")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("Failed to reset all data: \(error)")
|
||||
print("Failed to start reset all data: \(error)")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user