Version casi lista
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
import EventKit
|
||||
|
||||
@MainActor
|
||||
final class SettingsViewModel: ObservableObject {
|
||||
@Published var availableCalendars: [EKCalendar] = []
|
||||
@Published var showCalendarPermissionAlert: Bool = false
|
||||
|
||||
func loadCalendars() {
|
||||
availableCalendars = CalendarService.shared.availableCalendars()
|
||||
}
|
||||
|
||||
func requestCalendarAccess() async -> Bool {
|
||||
let granted = await CalendarService.shared.requestAccess()
|
||||
if !granted {
|
||||
showCalendarPermissionAlert = true
|
||||
} else {
|
||||
loadCalendars()
|
||||
}
|
||||
return granted
|
||||
}
|
||||
|
||||
func openSystemSettings() {
|
||||
if let url = URL(string: UIApplication.openSettingsURLString) {
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
}
|
||||
|
||||
func updateCalendarEvents(settings: AppSettings, plan: WeekPlan?) {
|
||||
guard let plan = plan, settings.syncEnabled else { return }
|
||||
CalendarService.shared.updateEventsTime(
|
||||
slots: plan.slots,
|
||||
weekStartDate: plan.weekStartDate,
|
||||
settings: settings
|
||||
)
|
||||
}
|
||||
|
||||
func resetAllData(context: ModelContext) {
|
||||
do {
|
||||
try deleteAll(of: MealSlot.self, in: context)
|
||||
try deleteAll(of: WeekPlan.self, in: context)
|
||||
try deleteAll(of: Dish.self, in: context)
|
||||
try deleteAll(of: Tag.self, in: context)
|
||||
try deleteAll(of: AppSettings.self, in: context)
|
||||
|
||||
DefaultDataService.createDefaultSettings(context: context)
|
||||
DefaultDataService.createDefaultTags(context: context)
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("Failed to reset all data: \(error)")
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteAll<T: PersistentModel>(of type: T.Type, in context: ModelContext) throws {
|
||||
let descriptor = FetchDescriptor<T>()
|
||||
let models = try context.fetch(descriptor)
|
||||
for model in models {
|
||||
context.delete(model)
|
||||
}
|
||||
try context.save()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user