Skip to content

Instantly share code, notes, and snippets.

@corybohon
Created April 28, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corybohon/22a144627938aba92e8142585878cb14 to your computer and use it in GitHub Desktop.
Save corybohon/22a144627938aba92e8142585878cb14 to your computer and use it in GitHub Desktop.
func addTodoItem(item: TodoItem) {
self.storedTodos.append(item)
self.createNotification(item.todoName, date: item.todoDueDate)
self.saveAndNotify()
}
func removeTodoItem(item: TodoItem) {
if self.storedTodos.contains(item) {
if let indexToRemove = self.storedTodos.indexOf(item) {
self.storedTodos.removeAtIndex(indexToRemove)
self.removeNotification(item.todoName, date: item.todoDueDate)
self.saveAndNotify()
}
}
}
func createNotification(name: String, date: NSDate) {
let notification: NSUserNotification = NSUserNotification()
notification.deliveryDate = date
notification.title = "Reminderz"
notification.subtitle = name
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(notification)
}
func removeNotification(name: String, date: NSDate) {
for notification in NSUserNotificationCenter.defaultUserNotificationCenter().scheduledNotifications {
if notification.subtitle! == name && (notification.deliveryDate?.isEqualToDate(date))! {
NSUserNotificationCenter.defaultUserNotificationCenter().removeScheduledNotification(notification)
}
}
}
func removeOldNotifications() {
NSUserNotificationCenter.defaultUserNotificationCenter().removeAllDeliveredNotifications()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment