91 lines
2.7 KiB
Swift
91 lines
2.7 KiB
Swift
|
//
|
||
|
// ContentView.swift
|
||
|
// Screaming Reminder
|
||
|
//
|
||
|
// Created by Amine Bou on 11/07/2024.
|
||
|
//
|
||
|
|
||
|
import SwiftUI
|
||
|
import SwiftData
|
||
|
|
||
|
struct ContentView: View {
|
||
|
@Query private var reminders: [Reminder]
|
||
|
|
||
|
@State var shouldPresentSheet = false
|
||
|
@State var selectedLabel: String = ""
|
||
|
@State var selectedReminder: Reminder?
|
||
|
@State var selectedDate = [Date()]
|
||
|
@State var showingAlert: Bool = false
|
||
|
|
||
|
var body: some View {
|
||
|
NavigationSplitView {
|
||
|
List {
|
||
|
ForEach(reminders) { reminder in
|
||
|
Button {
|
||
|
showModal(reminder: reminder)
|
||
|
} label: {
|
||
|
Text("\(reminder)")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
.toolbar {
|
||
|
ToolbarItem {
|
||
|
Button {
|
||
|
showModal()
|
||
|
} label : {
|
||
|
Label("Add Item", systemImage: "plus")
|
||
|
}
|
||
|
}
|
||
|
#if targetEnvironment(simulator)
|
||
|
ToolbarItem {
|
||
|
Button(action: testNotifs) {
|
||
|
Label("Test notifs", systemImage: "volume")
|
||
|
}
|
||
|
}
|
||
|
ToolbarItem {
|
||
|
Button(action: {
|
||
|
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
|
||
|
}) {
|
||
|
Label("Delete", systemImage: "speaker.slash")
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
}.sheet(isPresented: $shouldPresentSheet) {
|
||
|
selectedDate = [Date()]
|
||
|
selectedReminder = nil
|
||
|
selectedLabel = ""
|
||
|
} content: {
|
||
|
SheetView(selectedLabel: $selectedLabel, selectedReminder: $selectedReminder, selectedDate: $selectedDate)
|
||
|
}
|
||
|
} detail: {
|
||
|
Text("Select a reminder")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func testNotifs() {
|
||
|
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {
|
||
|
for notification in $0 {
|
||
|
print("\(notification.identifier) - \(notification.trigger!) \((notification.trigger! as! UNCalendarNotificationTrigger).nextTriggerDate())")
|
||
|
}
|
||
|
|
||
|
})
|
||
|
}
|
||
|
|
||
|
private func showModal(reminder: Reminder? = nil) {
|
||
|
withAnimation {
|
||
|
if (reminder != nil) {
|
||
|
selectedReminder = reminder
|
||
|
selectedLabel = reminder!.label
|
||
|
selectedDate = reminder!.when
|
||
|
}
|
||
|
shouldPresentSheet = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
#Preview {
|
||
|
ContentView()
|
||
|
.modelContainer(for: Reminder.self, inMemory: true)
|
||
|
}
|