Editing an alarm now reschedules the notification
This commit is contained in:
@@ -83,8 +83,9 @@ class AddAlarmViewController: UIViewController
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Removes the currently selcted alarm.
|
Removes the currently selcted alarm.
|
||||||
|
Returns the removed Alarm object.
|
||||||
*/
|
*/
|
||||||
func removeCurrentAlarm() {
|
func removeCurrentAlarm() -> Alarm? {
|
||||||
let hours = Int(String(originalTime[...originalTime.index(originalTime.endIndex, offsetBy: -4 )]))!
|
let hours = Int(String(originalTime[...originalTime.index(originalTime.endIndex, offsetBy: -4 )]))!
|
||||||
let minutes = Int(String(originalTime.suffix(2)))!
|
let minutes = Int(String(originalTime.suffix(2)))!
|
||||||
|
|
||||||
@@ -95,6 +96,8 @@ class AddAlarmViewController: UIViewController
|
|||||||
let alarmsObj = Alarms.fromLocal()
|
let alarmsObj = Alarms.fromLocal()
|
||||||
alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm }
|
alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm }
|
||||||
alarmsObj.localSave()
|
alarmsObj.localSave()
|
||||||
|
|
||||||
|
return alarm
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,13 +127,12 @@ class AddAlarmViewController: UIViewController
|
|||||||
@IBAction func addAlarmButton(_ sender: Any)
|
@IBAction func addAlarmButton(_ sender: Any)
|
||||||
{
|
{
|
||||||
let (h, m, _) = timePicker.date.getHMS()
|
let (h, m, _) = timePicker.date.getHMS()
|
||||||
|
var oldAlarm: Alarm? = nil
|
||||||
|
|
||||||
// Check if editing alarm
|
// Check if editing alarm
|
||||||
if (editFlag)
|
if (editFlag)
|
||||||
{
|
{
|
||||||
removeCurrentAlarm()
|
oldAlarm = removeCurrentAlarm()
|
||||||
|
|
||||||
|
|
||||||
} // Check for existing alarm
|
} // Check for existing alarm
|
||||||
else if ((Alarms.fromLocal().list.contains { $0.hour == h && $0.minute == m }))
|
else if ((Alarms.fromLocal().list.contains { $0.hour == h && $0.minute == m }))
|
||||||
{
|
{
|
||||||
@@ -153,6 +155,9 @@ class AddAlarmViewController: UIViewController
|
|||||||
Alarms.fromLocal().apply { $0.list.append(alarm) }.localSave();
|
Alarms.fromLocal().apply { $0.list.append(alarm) }.localSave();
|
||||||
|
|
||||||
//Schedules notification for the alarm
|
//Schedules notification for the alarm
|
||||||
|
if editFlag{
|
||||||
|
Notification(alarm: oldAlarm!).removeNotification()
|
||||||
|
}
|
||||||
Notification(alarm: alarm).scheduleNotification()
|
Notification(alarm: alarm).scheduleNotification()
|
||||||
|
|
||||||
// Dismiss this view
|
// Dismiss this view
|
||||||
@@ -177,7 +182,6 @@ class AddAlarmViewController: UIViewController
|
|||||||
if repeatWeekendsSwitch.isOn { [0, 6].forEach { a.repeats[$0] = true } }
|
if repeatWeekendsSwitch.isOn { [0, 6].forEach { a.repeats[$0] = true } }
|
||||||
|
|
||||||
let timeTill = a.nextActivate!.timeIntervalSince(Date()).str()
|
let timeTill = a.nextActivate!.timeIntervalSince(Date()).str()
|
||||||
//print(timeTill)
|
|
||||||
timeTillAlarmLabel.text = "Going off in \(timeTill)"
|
timeTillAlarmLabel.text = "Going off in \(timeTill)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,18 @@ class Notification {
|
|||||||
|
|
||||||
var alarm: Alarm
|
var alarm: Alarm
|
||||||
|
|
||||||
|
func removeNotification() {
|
||||||
|
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
|
||||||
|
var identifiers: [String] = []
|
||||||
|
for notification:UNNotificationRequest in notificationRequests {
|
||||||
|
if notification.identifier == self.alarm.notificationID {
|
||||||
|
identifiers.append(notification.identifier)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func scheduleNotification() {
|
func scheduleNotification() {
|
||||||
let content = UNMutableNotificationContent()
|
let content = UNMutableNotificationContent()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user