Alarms are now editable.

This commit is contained in:
Aaron
2021-01-26 16:23:23 -05:00
parent 2437c52faf
commit 0de6aeef80
3 changed files with 98 additions and 7 deletions
+73 -4
View File
@@ -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
+11
View File
@@ -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"
+14 -3
View File
@@ -73,6 +73,16 @@
<color key="textColor" systemColor="secondaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Kaa-5k-xfL" userLabel="wvmLabel">
<rect key="frame" x="246" y="11" width="0.0" height="0.0"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<accessibility key="accessibilityConfiguration">
<accessibilityTraits key="traits" staticText="YES" notEnabled="YES"/>
</accessibility>
<fontDescription key="fontDescription" type="system" pointSize="1"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="dWq-jh-9Ue" firstAttribute="baseline" secondItem="4hj-fb-FF3" secondAttribute="baseline" id="4H2-o6-GfV"/>
@@ -99,7 +109,8 @@
<outlet property="goingOffText" destination="EPJ-eL-Kek" id="QNo-WD-AyS"/>
<outlet property="repeatText" destination="YhV-X3-y1v" id="Alj-Ko-6y6"/>
<outlet property="time" destination="mjO-SX-f31" id="OQC-eJ-Qjc"/>
<segue destination="Mki-dC-5Kc" kind="show" id="WYD-ju-WX0"/>
<outlet property="wvmText" destination="Kaa-5k-xfL" id="gsX-DJ-xmb"/>
<segue destination="Mki-dC-5Kc" kind="show" identifier="edit-alarm" id="WYD-ju-WX0"/>
</connections>
</tableViewCell>
</prototypes>
@@ -880,7 +891,7 @@
<rect key="frame" x="0.0" y="0.0" width="374" height="170"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="person.crop.circle.badge.checkmark" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="yVW-fL-SIQ">
<rect key="frame" x="130.5" y="0.0" width="111.5" height="109.5"/>
<rect key="frame" x="132" y="0.0" width="110" height="109.5"/>
<constraints>
<constraint firstAttribute="width" constant="110" id="So8-k5-nLS"/>
<constraint firstAttribute="height" constant="110" id="inA-vb-CrL"/>
@@ -1257,7 +1268,7 @@
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="Qim-4Q-43N"/>
<segue reference="WYD-ju-WX0"/>
</inferredMetricsTieBreakers>
<resources>
<image name="alarm.fill" catalog="system" width="124" height="128"/>