Edit Alarm delete now works.

This commit is contained in:
Aaron
2021-01-26 21:49:39 -05:00
parent 7c626b4f82
commit 53a3616d5c
+21 -10
View File
@@ -81,6 +81,22 @@ class AddAlarmViewController: UIViewController
@IBOutlet weak var timeTillAlarmLabel: UILabel!
@IBOutlet weak var viewTitle: UILabel!
/**
Removes the currently selcted alarm.
*/
func removeCurrentAlarm() {
let hours = Int(String(originalTime[...originalTime.index(originalTime.endIndex, offsetBy: -4 )]))!
let minutes = Int(String(originalTime.suffix(2)))!
// TODO : REWRITE the am/pm check, pretty sure this could work on two alarms at once
let alarm = Alarms.fromLocal().list.first(where: {($0.hour == hours || $0.hour == (hours + 12)) && $0.minute == minutes})
// Removes the alarm from stored alarms
let alarmsObj = Alarms.fromLocal()
alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm }
alarmsObj.localSave()
}
/**
Called when the time for the alarm is changed.
Sets the time away at the top of the View.
@@ -94,6 +110,10 @@ class AddAlarmViewController: UIViewController
Called when the user clicks the remove button and brings them back to the home page
*/
@IBAction func cancelAlarmButton(_ sender: Any) {
if editFlag {
removeCurrentAlarm()
}
self.dismiss(animated: true, completion: nil)
//might need to reset all UI elements
}
@@ -108,16 +128,7 @@ class AddAlarmViewController: UIViewController
// Check if editing alarm
if (editFlag)
{
let hours = Int(String(originalTime[...originalTime.index(originalTime.endIndex, offsetBy: -4 )]))!
let minutes = Int(String(originalTime.suffix(2)))!
// TODO : REWRITE the am/pm check, pretty sure this could work on two alarms at once
let alarm = Alarms.fromLocal().list.first(where: {($0.hour == hours || $0.hour == (hours + 12)) && $0.minute == minutes})
// Removes the alarm from stored alarms
let alarmsObj = Alarms.fromLocal()
alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm }
alarmsObj.localSave()
removeCurrentAlarm()
} // Check for existing alarm