Skip to content

Commit

Permalink
Add event ends notification
Browse files Browse the repository at this point in the history
  • Loading branch information
leits committed Jun 17, 2024
1 parent 02a706f commit 13d27ac
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 35 deletions.
122 changes: 88 additions & 34 deletions MeetingBar/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,50 +133,104 @@ func displayAlert(title: String, text: String) {
}

func scheduleEventNotification(_ event: MBEvent) {
if !Defaults[.joinEventNotification] && !Defaults[.endOfEventNotification] {
return
}

requestNotificationAuthorization() // By the apple best practices

let now = Date()
let notificationTime = Double(Defaults[.joinEventNotificationTime].rawValue)
let timeInterval = event.startDate.timeIntervalSince(now) - notificationTime

if timeInterval < 0.5 {
return
}
// Event start notification
if Defaults[.joinEventNotification] {
let notificationTime = Double(Defaults[.joinEventNotificationTime].rawValue)
let timeInterval = event.startDate.timeIntervalSince(now) - notificationTime

removePendingNotificationRequests(withID: notificationIDs.event_starts)
if timeInterval < 0.5 {
return
}

let center = UNUserNotificationCenter.current()
removePendingNotificationRequests(withID: notificationIDs.event_starts)

let content = UNMutableNotificationContent()
if Defaults[.hideMeetingTitle] {
content.title = "general_meeting".loco()
} else {
content.title = event.title
}
if #available(macOS 12.0, *) {
content.interruptionLevel = .timeSensitive
}
let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
if Defaults[.hideMeetingTitle] {
content.title = "general_meeting".loco()
} else {
content.title = event.title
}
if #available(macOS 12.0, *) {
content.interruptionLevel = .timeSensitive
}

switch Defaults[.joinEventNotificationTime] {
case .atStart:
content.body = "notifications_event_start_soon_body".loco()
case .minuteBefore:
content.body = "notifications_event_start_one_minute_body".loco()
case .threeMinuteBefore:
content.body = "notifications_event_start_three_minutes_body".loco()
case .fiveMinuteBefore:
content.body = "notifications_event_start_five_minutes_body".loco()
switch Defaults[.joinEventNotificationTime] {
case .atStart:
content.body = "notifications_event_start_soon_body".loco()
case .minuteBefore:
content.body = "notifications_event_start_one_minute_body".loco()
case .threeMinuteBefore:
content.body = "notifications_event_start_three_minutes_body".loco()
case .fiveMinuteBefore:
content.body = "notifications_event_start_five_minutes_body".loco()
}
content.categoryIdentifier = "EVENT"
content.sound = UNNotificationSound.default
content.userInfo = ["eventID": event.ID]
content.threadIdentifier = "meetingbar"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
let request = UNNotificationRequest(identifier: notificationIDs.event_starts, content: content, trigger: trigger)
center.add(request) { error in
if let error = error {
NSLog("%@", "request \(request.identifier) could not be added because of error \(error)")
}
}
}
content.categoryIdentifier = "EVENT"
content.sound = UNNotificationSound.default
content.userInfo = ["eventID": event.ID]
content.threadIdentifier = "meetingbar"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
let request = UNNotificationRequest(identifier: notificationIDs.event_starts, content: content, trigger: trigger)
center.add(request) { error in
if let error = error {
NSLog("%@", "request \(request.identifier) could not be added because of error \(error)")
// Event end notification
if Defaults[.endOfEventNotification] {
let notificationTime = Double(Defaults[.endOfEventNotificationTime].rawValue)
let timeInterval = event.endDate.timeIntervalSince(now) - notificationTime

if timeInterval < 0.5 {
return
}

let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent()
if Defaults[.hideMeetingTitle] {
content.title = "general_meeting".loco()
} else {
content.title = event.title
}
if #available(macOS 12.0, *) {
content.interruptionLevel = .timeSensitive
}

switch Defaults[.endOfEventNotificationTime] {
// TODO: notification localization
case .atEnd:
content.body = "Event ends soon"
case .minuteBefore:
content.body = "Event ends in one minute"
case .threeMinuteBefore:
content.body = "Event ends in three minutes"
case .fiveMinuteBefore:
content.body = "Event ends in five minutes"
}
// content.categoryIdentifier = "EVENT"
content.sound = UNNotificationSound.default
content.userInfo = ["eventID": event.ID]
content.threadIdentifier = "meetingbar"

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
let request = UNNotificationRequest(identifier: notificationIDs.event_starts, content: content, trigger: trigger)
center.add(request) { error in
if let error = error {
NSLog("%@", "request \(request.identifier) could not be added because of error \(error)")
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MeetingBar/Views/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct endEventNotificationPicker: View {
Text("when event ends").tag(TimeBeforeEventEnd.atEnd)
Text("1 minute before").tag(TimeBeforeEventEnd.minuteBefore)
Text("3 minute before").tag(TimeBeforeEventEnd.threeMinuteBefore)
Text("5 minute before".loco()).tag(TimeBeforeEventEnd.fiveMinuteBefore)
Text("5 minute before").tag(TimeBeforeEventEnd.fiveMinuteBefore)
}.frame(width: 220, alignment: .leading).labelsHidden().disabled(!endOfEventNotification)
Text("βeta").font(.caption).foregroundColor(.orange)
}
Expand Down

0 comments on commit 13d27ac

Please sign in to comment.