improve notification handling

fix crash on ocmpleting events
This commit is contained in:
neon443
2025-06-25 20:20:38 +01:00
parent 9841574d37
commit 6f870ffa4f
2 changed files with 16 additions and 5 deletions

View File

@@ -35,7 +35,7 @@ struct CompleteEventButton: View {
progress = 0 progress = 0
timer = Timer(timeInterval: 0.02, repeats: true) { timer in timer = Timer(timeInterval: 0.02, repeats: true) { timer in
DispatchQueue.main.sync { DispatchQueue.main.async {
guard completeInProgress else { return } guard completeInProgress else { return }
guard let timer = self.timer else { return } guard let timer = self.timer else { return }
guard timer.isValid else { return } guard timer.isValid else { return }

View File

@@ -160,20 +160,24 @@ class EventViewModel: ObservableObject, @unchecked Sendable {
eventUUIDs.remove(at: remove) eventUUIDs.remove(at: remove)
} }
let components = getDateComponents(events[index].date) let components = getDateComponents(events[index].date)
//check the notif matches event details //check the notif matches event details
if req.content.title == events[index].name, if req.content.title == events[index].name,
req.content.subtitle == events[index].notes, req.content.subtitle == events[index].notes,
req.trigger == UNCalendarNotificationTrigger(dateMatching: components, repeats: false) { req.trigger == UNCalendarNotificationTrigger(dateMatching: components, repeats: false) {
//if it does, make sure the notif delets if u complete the veent //if it does, make sure the notif delets if u complete the veent or in the past
if events[index].complete { if events[index].complete || events[index].date > .now {
cancelNotif(req.identifier) cancelNotif(req.identifier)
} else {
//dont cancel the notif
} }
} else { } else {
//reschedult it because the event details have changed
cancelNotif(req.identifier) cancelNotif(req.identifier)
scheduleEventNotif(events[index]) scheduleEventNotif(events[index])
} }
} else { } else {
//cancel if the event is deleted //cancel notif if the event is deleted (doesnt exist/cannot be matched)
cancelNotif(req.identifier) cancelNotif(req.identifier)
} }
} }
@@ -182,6 +186,14 @@ class EventViewModel: ObservableObject, @unchecked Sendable {
scheduleEventNotif(event) scheduleEventNotif(event)
} }
} }
Task {
try? await UNUserNotificationCenter.current().setBadgeCount(await getNotifs().count)
}
print(eventUUIDs.count)
print(events.count(where: {!$0.complete && $0.date < .now}))
print(events.count(where: {!$0.complete && $0.date > .now}))
print(events.count(where: {!$0.complete}))
print(events.count(where: {$0.complete}))
} }
// save to local and icloud // save to local and icloud
@@ -246,7 +258,6 @@ class EventViewModel: ObservableObject, @unchecked Sendable {
} }
if let eventToModify = eventToModify { if let eventToModify = eventToModify {
self.events.remove(at: eventToModify) self.events.remove(at: eventToModify)
self.saveEvents()
} }
saveEvents() //sync local and icl saveEvents() //sync local and icl
} }