mirror of
https://github.com/neon443/NearFuture.git
synced 2026-03-11 06:49:12 +00:00
ok now notifs seem doen
neew event -> notif edited event- cancel old, new notif completed events- cancel notif moved notif granting to settings struct add: getNotifs, checkPendingNotifs, checkNotif, scheduleEventNotif, getDateComponents, cancelNotif fix scheduleNotif migration next
This commit is contained in:
@@ -114,6 +114,7 @@ class SettingsViewModel: ObservableObject {
|
|||||||
showCompletedInHome: false,
|
showCompletedInHome: false,
|
||||||
tint: ColorCodable(uiColor: UIColor(named: "AccentColor")!)
|
tint: ColorCodable(uiColor: UIColor(named: "AccentColor")!)
|
||||||
)
|
)
|
||||||
|
@Published var notifsGranted: Bool = false
|
||||||
|
|
||||||
@Published var accentChoices: [Color] = [
|
@Published var accentChoices: [Color] = [
|
||||||
Color(UIColor(named: "uiColors/red")!),
|
Color(UIColor(named: "uiColors/red")!),
|
||||||
@@ -128,6 +129,15 @@ class SettingsViewModel: ObservableObject {
|
|||||||
init(load: Bool = true) {
|
init(load: Bool = true) {
|
||||||
if load {
|
if load {
|
||||||
loadSettings()
|
loadSettings()
|
||||||
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
||||||
|
if settings.authorizationStatus == .authorized {
|
||||||
|
self.notifsGranted = true
|
||||||
|
} else {
|
||||||
|
Task {
|
||||||
|
self.notifsGranted = await requestNotifs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,6 +232,38 @@ class EventViewModel: ObservableObject {
|
|||||||
updateSyncStatus()
|
updateSyncStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getNotifs() async -> [UNNotificationRequest] {
|
||||||
|
return await UNUserNotificationCenter.current().pendingNotificationRequests()
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkPendingNotifs(_ pending: [UNNotificationRequest]) {
|
||||||
|
for req in pending {
|
||||||
|
checkNotif(notif: req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkNotif(notif: UNNotificationRequest) {
|
||||||
|
//match the notif to an event
|
||||||
|
if let index = events.firstIndex(where: {$0.id.uuidString == notif.identifier}) {
|
||||||
|
let components = getDateComponents(events[index].date)
|
||||||
|
//check the notif matches event details
|
||||||
|
if notif.content.title == events[index].name,
|
||||||
|
notif.content.subtitle == events[index].notes,
|
||||||
|
notif.trigger == UNCalendarNotificationTrigger(dateMatching: components, repeats: false) {
|
||||||
|
//if it does, make sure the notif delets if u complete the veent
|
||||||
|
if events[index].complete {
|
||||||
|
cancelNotif(notif.identifier)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cancelNotif(notif.identifier)
|
||||||
|
scheduleEventNotif(events[index])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//cancel if the event is deleted
|
||||||
|
cancelNotif(notif.identifier)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// save to local and icloud
|
// save to local and icloud
|
||||||
func saveEvents() {
|
func saveEvents() {
|
||||||
let encoder = JSONEncoder()
|
let encoder = JSONEncoder()
|
||||||
@@ -233,6 +275,9 @@ class EventViewModel: ObservableObject {
|
|||||||
icloudStore.synchronize()
|
icloudStore.synchronize()
|
||||||
|
|
||||||
updateSyncStatus()
|
updateSyncStatus()
|
||||||
|
Task {
|
||||||
|
await checkPendingNotifs(getNotifs())
|
||||||
|
}
|
||||||
loadEvents()
|
loadEvents()
|
||||||
WidgetCenter.shared.reloadAllTimelines()//reload all widgets when saving events
|
WidgetCenter.shared.reloadAllTimelines()//reload all widgets when saving events
|
||||||
objectWillChange.send()
|
objectWillChange.send()
|
||||||
@@ -253,6 +298,7 @@ class EventViewModel: ObservableObject {
|
|||||||
|
|
||||||
func addEvent(newEvent: Event) {
|
func addEvent(newEvent: Event) {
|
||||||
events.append(newEvent)
|
events.append(newEvent)
|
||||||
|
scheduleEventNotif(newEvent)
|
||||||
saveEvents() //sync with icloud
|
saveEvents() //sync with icloud
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,13 +476,33 @@ func requestNotifs() async -> Bool {
|
|||||||
return result ?? false
|
return result ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
func scheduleNotif() {
|
func scheduleNotif(title: String, sub: String, date: Date, id: String = UUID().uuidString) {
|
||||||
let content = UNMutableNotificationContent()
|
let content = UNMutableNotificationContent()
|
||||||
content.title = "hi"
|
content.title = title
|
||||||
content.subtitle = "sss"
|
content.subtitle = sub
|
||||||
content.sound = .default
|
content.sound = .default
|
||||||
let identifier = UUID().uuidString
|
|
||||||
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
|
let identifier = id
|
||||||
|
let dateComponents = getDateComponents(date)
|
||||||
|
|
||||||
|
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
|
||||||
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
|
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
|
||||||
UNUserNotificationCenter.current().add(request)
|
UNUserNotificationCenter.current().add(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scheduleEventNotif(_ event: Event) {
|
||||||
|
scheduleNotif(
|
||||||
|
title: event.name,
|
||||||
|
sub: event.notes,
|
||||||
|
date: event.date,
|
||||||
|
id: event.id.uuidString
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDateComponents(_ date: Date) -> DateComponents {
|
||||||
|
return Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: date)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cancelNotif(_ id: String) {
|
||||||
|
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [id])
|
||||||
|
}
|
||||||
|
|||||||
@@ -138,11 +138,6 @@ struct ContentView: View {
|
|||||||
AddEventButton(showingAddEventView: $showingAddEventView)
|
AddEventButton(showingAddEventView: $showingAddEventView)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear() {
|
|
||||||
Task {
|
|
||||||
await requestNotifs()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.tabItem {
|
.tabItem {
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ struct SettingsView: View {
|
|||||||
@State private var localCountEqualToiCloud: Bool = false
|
@State private var localCountEqualToiCloud: Bool = false
|
||||||
@State private var icloudCountEqualToLocal: Bool = false
|
@State private var icloudCountEqualToLocal: Bool = false
|
||||||
@State private var importStr: String = ""
|
@State private var importStr: String = ""
|
||||||
@State private var notifsGranted: Bool = false
|
|
||||||
|
|
||||||
func updateStatus() {
|
func updateStatus() {
|
||||||
let vm = viewModel
|
let vm = viewModel
|
||||||
@@ -70,10 +69,10 @@ struct SettingsView: View {
|
|||||||
}
|
}
|
||||||
NavigationLink() {
|
NavigationLink() {
|
||||||
List {
|
List {
|
||||||
if !notifsGranted {
|
if !settingsModel.notifsGranted {
|
||||||
Button("Request Notifications") {
|
Button("Request Notifications") {
|
||||||
Task {
|
Task {
|
||||||
notifsGranted = await requestNotifs()
|
settingsModel.notifsGranted = await requestNotifs()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Text("\(Image(systemName: "xmark")) Notifications disabled for Near Future")
|
Text("\(Image(systemName: "xmark")) Notifications disabled for Near Future")
|
||||||
@@ -166,11 +165,6 @@ struct SettingsView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear() {
|
|
||||||
Task {
|
|
||||||
notifsGranted = await requestNotifs()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.scrollContentBackground(.hidden)
|
.scrollContentBackground(.hidden)
|
||||||
.navigationTitle("Settings")
|
.navigationTitle("Settings")
|
||||||
.apply {
|
.apply {
|
||||||
|
|||||||
Reference in New Issue
Block a user