-
Notifications
You must be signed in to change notification settings - Fork 34
Validate notification fire date before scheduling #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,19 @@ final class LocalNotificationsService { | |
| throw Error.badContentProvider | ||
| } | ||
|
|
||
| let nextTriggerDate: Date? | ||
| if let timeIntervalTrigger = notificationTrigger as? UNTimeIntervalNotificationTrigger { | ||
| nextTriggerDate = timeIntervalTrigger.nextTriggerDate() | ||
| } else if let calendarTrigger = notificationTrigger as? UNCalendarNotificationTrigger { | ||
| nextTriggerDate = calendarTrigger.nextTriggerDate() | ||
| } else { | ||
| nextTriggerDate = nil | ||
| } | ||
|
|
||
| if !self.isFireDateValid(nextTriggerDate) { | ||
|
||
| throw Error.badFireDate | ||
| } | ||
|
|
||
| let request = UNNotificationRequest( | ||
| identifier: contentProvider.identifier, | ||
| content: self.makeNotificationContent(for: contentProvider), | ||
|
|
@@ -186,6 +199,10 @@ final class LocalNotificationsService { | |
| private func localNotificationScheduleNotification( | ||
| contentProvider: LocalNotificationContentProvider | ||
| ) -> Promise<Void> { | ||
| if !self.isFireDateValid(contentProvider.fireDate) { | ||
|
||
| return Promise(error: Error.badFireDate) | ||
| } | ||
|
|
||
| let notification = UILocalNotification() | ||
| notification.alertTitle = contentProvider.title | ||
| notification.alertBody = contentProvider.body | ||
|
|
@@ -202,6 +219,19 @@ final class LocalNotificationsService { | |
| return .value(()) | ||
| } | ||
|
|
||
| /// Checks that `fireDate` is valid. | ||
| /// | ||
| /// - Parameters: | ||
| /// - fireDate: The Date object to be checked. | ||
| /// - Returns: `true` if the `fireDate` exists and it in the future, otherwise false. | ||
| private func isFireDateValid(_ fireDate: Date?) -> Bool { | ||
| if let fireDate = fireDate { | ||
| return fireDate.compare(Date()) == .orderedDescending | ||
| } else { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Types - | ||
|
|
||
| enum PayloadKey: String { | ||
|
|
@@ -212,5 +242,6 @@ final class LocalNotificationsService { | |
|
|
||
| enum Error: Swift.Error { | ||
| case badContentProvider | ||
| case badFireDate | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Возможно, так понятнее