diff --git a/ProjectClock/AddAlarmViewController.swift b/ProjectClock/AddAlarmViewController.swift
index bcab035..d69c902 100644
--- a/ProjectClock/AddAlarmViewController.swift
+++ b/ProjectClock/AddAlarmViewController.swift
@@ -9,6 +9,56 @@ import UIKit
class AddAlarmViewController: UIViewController
{
+ // Editing variables
+ var alarmCell: AlarmTableCell? = nil
+ var editFlag: Bool = false
+ var originalTime: String = ""
+
+ override func viewDidLoad() {
+ if let alarmCell = alarmCell {
+
+ //Toggle editing mode
+ editFlag = true
+
+ //Convert string to Date
+ let dateFormatter = DateFormatter()
+ dateFormatter.dateFormat = "h:mma"
+ let date = dateFormatter.date(from: "\(alarmCell.time.text!)\(alarmCell.ampm.text!)")
+
+ // Set all the original values to be edited
+ timePicker.date = date!
+ originalTime = String(dateFormatter.string(from: date!).dropLast(2))
+
+ //Toggle proper repeats
+ if let repeats = alarmCell.repeatText.text {
+ if repeats == "Repeats: Weekdays" {
+ repeatWeekdaysSwitch.isOn = true
+ repeatWeekendsSwitch.isOn = false
+ } else if repeats == "Repeats: Weekends" {
+ repeatWeekendsSwitch.isOn = true
+ repeatWeekdaysSwitch.isOn = false
+ } else if repeats == "Repeats: Daily" {
+ repeatWeekdaysSwitch.isOn = true
+ repeatWeekendsSwitch.isOn = true
+ } else {
+ repeatWeekendsSwitch.isOn = false
+ repeatWeekdaysSwitch.isOn = false
+ }
+ }
+
+ alarmNameTextField.text = String(alarmCell.descriptionText.text!.dropFirst(2))
+ updateETA()
+
+ // Sets the WVM
+ if let wvm = alarmCell.wvmText.text {
+ for index in 0...wvms.count-1 {
+ if wvm == wvms[index].name {
+ wvmPicker.selectRow(index, inComponent: 0, animated: true)
+ }
+ }
+ }
+ }
+ }
// UI: Make scroll view scrollable
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var scrollViewInner: UIView!
@@ -61,9 +111,24 @@ class AddAlarmViewController: UIViewController
@IBAction func addAlarmButton(_ sender: Any)
{
let (h, m, _) = timePicker.date.getHMS()
-
- // Check for existing alarm
- if (Alarms.fromLocal().list.contains { $0.hour == h && $0.minute == m })
+
+ // 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()
+
+
+ } // Check for existing alarm
+ else if ((Alarms.fromLocal().list.contains { $0.hour == h && $0.minute == m }))
{
msg("Nope", "You already have an alarm at " + String(format: "%i:%02i", h, m))
return
@@ -90,7 +155,9 @@ class AddAlarmViewController: UIViewController
self.dismiss(animated: true, completion: nil)
}
- // Dynamically the ETA label for the alarm
+ /**
+ Dynamically the ETA label for the alarm
+ */
func updateETA() {
//Create alarm without adding it to the queue.
let (h, m, _) = timePicker.date.getHMS()
@@ -109,6 +176,8 @@ class AddAlarmViewController: UIViewController
//print(timeTill)
timeTillAlarmLabel.text = "Going off in \(timeTill)"
}
+
+
}
class WVMDataSource: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource
diff --git a/ProjectClock/AlarmViewController.swift b/ProjectClock/AlarmViewController.swift
index 04800f6..4c85ed4 100644
--- a/ProjectClock/AlarmViewController.swift
+++ b/ProjectClock/AlarmViewController.swift
@@ -55,6 +55,14 @@ extension AlarmViewController: UITableViewDelegate, UITableViewDataSource
/// IDK what this does (TODO: Find out what this does)
func tableView(_ v: UITableView, didSelectRowAt i: IndexPath) { v.deselectRow(at: i, animated: true) }
+
+ /// Sends the selected alarm to be edited
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
+ if segue.identifier == "edit-alarm" {
+ let vc = segue.destination as! AddAlarmViewController
+ vc.alarmCell = (sender as! AlarmTableCell)
+ }
+ }
}
/**
@@ -68,6 +76,8 @@ class AlarmTableCell: UITableViewCell
@IBOutlet weak var enable: UISwitch!
@IBOutlet weak var repeatText: UILabel!
@IBOutlet weak var goingOffText: UILabel!
+ @IBOutlet weak var wvmText: UILabel!
+
var alarm: Alarm!
@@ -79,6 +89,7 @@ class AlarmTableCell: UITableViewCell
self.alarm = alarm
descriptionText.text = "- " + alarm.text
enable.isOn = alarm.enabled
+ wvmText.text = alarm.wakeMethod.name
// Display Hour, Minute, and AM or PM
ampm.text = alarm.hour < 12 || alarm.hour == 24 ? "AM" : "PM"
diff --git a/ProjectClock/Base.lproj/Main.storyboard b/ProjectClock/Base.lproj/Main.storyboard
index e47a0aa..d97b155 100644
--- a/ProjectClock/Base.lproj/Main.storyboard
+++ b/ProjectClock/Base.lproj/Main.storyboard
@@ -73,6 +73,16 @@
+
@@ -99,7 +109,8 @@
-
+
+
@@ -880,7 +891,7 @@
-
+
@@ -1257,7 +1268,7 @@
-
+