From 53a3616d5c79ea5b33035f18621f875f550e1a91 Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 26 Jan 2021 21:49:39 -0500 Subject: [PATCH] Edit Alarm delete now works. --- ProjectClock/AddAlarmViewController.swift | 31 +++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/ProjectClock/AddAlarmViewController.swift b/ProjectClock/AddAlarmViewController.swift index 9ab9d31..f310c3b 100644 --- a/ProjectClock/AddAlarmViewController.swift +++ b/ProjectClock/AddAlarmViewController.swift @@ -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