changes to pending notif checker

add migration, if events dont have notifs
fix runtime error about bg threads by using await MainActor.run
add cancelallnotifs
bump version
This commit is contained in:
neon443
2025-05-09 14:09:06 +01:00
parent 7af96834d6
commit 5f52b423db
2 changed files with 39 additions and 29 deletions

View File

@@ -12,6 +12,6 @@ TEAM_ID = 8JGND254B7
BUNDLE_ID = com.neon443.NearFuture BUNDLE_ID = com.neon443.NearFuture
BUNDLE_ID_WIDGETS = com.neon443.NearFuture.widgets BUNDLE_ID_WIDGETS = com.neon443.NearFuture.widgets
GROUP_ID = group.NearFuture GROUP_ID = group.NearFuture
VERSION = 3.3.1 VERSION = 4.0.0
NAME = Near Future NAME = Near Future
BUILD_NUMBER = 0 BUILD_NUMBER = 0

View File

@@ -129,13 +129,10 @@ class SettingsViewModel: ObservableObject {
init(load: Bool = true) { init(load: Bool = true) {
if load { if load {
loadSettings() loadSettings()
UNUserNotificationCenter.current().getNotificationSettings { settings in Task {
if settings.authorizationStatus == .authorized { let requestResult = await requestNotifs()
self.notifsGranted = true await MainActor.run {
} else { self.notifsGranted = requestResult
Task {
self.notifsGranted = await requestNotifs()
}
} }
} }
} }
@@ -237,30 +234,35 @@ class EventViewModel: ObservableObject {
} }
func checkPendingNotifs(_ pending: [UNNotificationRequest]) { func checkPendingNotifs(_ pending: [UNNotificationRequest]) {
var eventUUIDs = events.map({$0.id.uuidString})
for req in pending { for req in pending {
checkNotif(notif: req) //match the notif to an event
} if let index = events.firstIndex(where: {$0.id.uuidString == req.identifier}) {
} if let remove = eventUUIDs.firstIndex(where: {$0 == req.identifier}) {
eventUUIDs.remove(at: remove)
func checkNotif(notif: UNNotificationRequest) { }
//match the notif to an event let components = getDateComponents(events[index].date)
if let index = events.firstIndex(where: {$0.id.uuidString == notif.identifier}) { //check the notif matches event details
let components = getDateComponents(events[index].date) if req.content.title == events[index].name,
//check the notif matches event details req.content.subtitle == events[index].notes,
if notif.content.title == events[index].name, req.trigger == UNCalendarNotificationTrigger(dateMatching: components, repeats: false) {
notif.content.subtitle == events[index].notes, //if it does, make sure the notif delets if u complete the veent
notif.trigger == UNCalendarNotificationTrigger(dateMatching: components, repeats: false) { if events[index].complete {
//if it does, make sure the notif delets if u complete the veent cancelNotif(req.identifier)
if events[index].complete { }
cancelNotif(notif.identifier) } else {
cancelNotif(req.identifier)
scheduleEventNotif(events[index])
} }
} else { } else {
cancelNotif(notif.identifier) //cancel if the event is deleted
scheduleEventNotif(events[index]) cancelNotif(req.identifier)
}
}
for uuid in eventUUIDs {
if let event = events.first(where: {$0.id.uuidString == uuid}) {
scheduleEventNotif(event)
} }
} else {
//cancel if the event is deleted
cancelNotif(notif.identifier)
} }
} }
@@ -275,10 +277,10 @@ class EventViewModel: ObservableObject {
icloudStore.synchronize() icloudStore.synchronize()
updateSyncStatus() updateSyncStatus()
loadEvents()
Task { Task {
await checkPendingNotifs(getNotifs()) await checkPendingNotifs(getNotifs())
} }
loadEvents()
WidgetCenter.shared.reloadAllTimelines()//reload all widgets when saving events WidgetCenter.shared.reloadAllTimelines()//reload all widgets when saving events
objectWillChange.send() objectWillChange.send()
} }
@@ -372,6 +374,7 @@ class EventViewModel: ObservableObject {
UserDefaults.standard.removeObject(forKey: "events") UserDefaults.standard.removeObject(forKey: "events")
appGroupUserDefaults.removeObject(forKey: "events") appGroupUserDefaults.removeObject(forKey: "events")
events.removeAll() events.removeAll()
cancelAllNotifs()
updateSyncStatus() updateSyncStatus()
} }
@@ -379,6 +382,7 @@ class EventViewModel: ObservableObject {
icloudStore.removeObject(forKey: "events") icloudStore.removeObject(forKey: "events")
icloudStore.synchronize() icloudStore.synchronize()
icloudData.removeAll() icloudData.removeAll()
cancelAllNotifs()
updateSyncStatus() updateSyncStatus()
} }
@@ -394,6 +398,7 @@ class EventViewModel: ObservableObject {
} }
events.removeAll() events.removeAll()
cancelAllNotifs()
updateSyncStatus() updateSyncStatus()
} }
@@ -404,6 +409,7 @@ class EventViewModel: ObservableObject {
} }
icloudStore.synchronize() icloudStore.synchronize()
icloudData.removeAll() icloudData.removeAll()
cancelAllNotifs()
updateSyncStatus() updateSyncStatus()
} }
} }
@@ -506,3 +512,7 @@ func getDateComponents(_ date: Date) -> DateComponents {
func cancelNotif(_ id: String) { func cancelNotif(_ id: String) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [id]) UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [id])
} }
func cancelAllNotifs() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}