[O] Optimize Alarm edit
This commit is contained in:
@@ -12,7 +12,6 @@ class AddAlarmViewController: EndEditingOnReturn
|
|||||||
// Editing variables
|
// Editing variables
|
||||||
var alarmCell: AlarmTableCell? = nil
|
var alarmCell: AlarmTableCell? = nil
|
||||||
var editMode: Bool { alarmCell != nil }
|
var editMode: Bool { alarmCell != nil }
|
||||||
var originalTime: String = ""
|
|
||||||
|
|
||||||
override func viewDidLoad()
|
override func viewDidLoad()
|
||||||
{
|
{
|
||||||
@@ -20,57 +19,27 @@ class AddAlarmViewController: EndEditingOnReturn
|
|||||||
alarmNameTextField.delegate = self
|
alarmNameTextField.delegate = self
|
||||||
|
|
||||||
// Load alarm to edit if in edit mode
|
// Load alarm to edit if in edit mode
|
||||||
if let alarmCell = alarmCell
|
if let alarmCell = alarmCell, let alarm = alarmCell.alarm
|
||||||
{
|
{
|
||||||
// Toggle editing mode
|
// Toggle editing mode
|
||||||
viewTitle.text = "Edit Alarm"
|
viewTitle.text = "Edit Alarm"
|
||||||
|
|
||||||
// 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
|
// Set all the original values to be edited
|
||||||
timePicker.date = date!
|
let (y,m,d) = Date().getYMD()
|
||||||
originalTime = String(dateFormatter.string(from: date!).dropLast(2))
|
timePicker.date = Date.create(y, m, d, alarm.hour, alarm.minute)
|
||||||
|
|
||||||
// Toggle proper repeats
|
// Toggle proper repeats
|
||||||
if let repeats = alarmCell.repeatText.text {
|
repeatWeekdaysSwitch.isOn = alarm.repeats[1...5].allSatisfy { $0 }
|
||||||
if repeats == "Repeats: Weekdays" {
|
repeatWeekendsSwitch.isOn = alarm.repeats[0] && alarm.repeats[6]
|
||||||
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))
|
alarmNameTextField.text = alarm.text
|
||||||
updateETA()
|
updateETA()
|
||||||
|
|
||||||
// Sets the WVM
|
// Sets the WVM
|
||||||
if let wvm = alarmCell.wvmText.text {
|
wvmPicker.selectRow(alarm.wakeMethod.index, inComponent: 0, animated: true)
|
||||||
for index in 0...wvms.count-1 {
|
|
||||||
if wvm == wvms[index].name {
|
|
||||||
wvmPicker.selectRow(index, inComponent: 0, animated: true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Sets alarm tone
|
// Sets alarm tone
|
||||||
if let toneName = alarmCell.toneLabel.text {
|
ringtonePicker.selectRow(ringtones.firstIndex { $0.tone == alarm.alarmTone }!, inComponent: 0, animated: true)
|
||||||
for index in 0...ringtones.count-1 {
|
|
||||||
if toneName == ringtones[index].name {
|
|
||||||
ringtonePicker.selectRow(index, inComponent: 0, animated: true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,17 +69,14 @@ class AddAlarmViewController: EndEditingOnReturn
|
|||||||
Returns the removed Alarm object.
|
Returns the removed Alarm object.
|
||||||
*/
|
*/
|
||||||
@discardableResult
|
@discardableResult
|
||||||
func removeCurrentAlarm() -> Alarm? {
|
func removeCurrentAlarm() -> Alarm?
|
||||||
let hours = Int(String(originalTime[...originalTime.index(originalTime.endIndex, offsetBy: -4 )]))!
|
{
|
||||||
let minutes = Int(String(originalTime.suffix(2)))!
|
guard let alarm = alarmCell?.alarm else { return nil }
|
||||||
|
|
||||||
// TODO : REWRITE the am/pm check, pretty sure this could work on two alarms at once
|
|
||||||
let alarm = Alarms.fromLocal().list.first { ($0.hour == hours || $0.hour == (hours + 12)) && $0.minute == minutes }
|
|
||||||
|
|
||||||
// Removes the alarm from stored alarms
|
// Removes the alarm from stored alarms
|
||||||
let alarmsObj = Alarms.fromLocal()
|
let alarms = Alarms.fromLocal()
|
||||||
alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm }
|
alarms.list = alarms.list.filter { $0 != alarmCell?.alarm }
|
||||||
alarmsObj.localSave()
|
alarms.localSave()
|
||||||
|
|
||||||
return alarm
|
return alarm
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,18 +33,19 @@ struct Family: Codable
|
|||||||
|
|
||||||
struct WVM: Codable
|
struct WVM: Codable
|
||||||
{
|
{
|
||||||
|
let index: Int
|
||||||
let name: String
|
let name: String
|
||||||
let desc: String
|
let desc: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let wvms = [
|
let wvms = [
|
||||||
WVM(name: "Shake", desc: "Shake your phone... aggresively!"),
|
WVM(index: 0, name: "Shake", desc: "Shake your phone... aggresively!"),
|
||||||
WVM(name: "Math 1", desc: "Easy math expression"),
|
WVM(index: 1, name: "Math 1", desc: "Easy math expression"),
|
||||||
WVM(name: "Math 2", desc: "Medium math expression"),
|
WVM(index: 2, name: "Math 2", desc: "Medium math expression"),
|
||||||
WVM(name: "Math 3", desc: "Hard math expression"),
|
WVM(index: 3, name: "Math 3", desc: "Hard math expression"),
|
||||||
WVM(name: "Factor", desc: "Factor a binomial"),
|
WVM(index: 4, name: "Factor", desc: "Factor a binomial"),
|
||||||
WVM(name: "RPS", desc: "Win a game of rock paper scissors"),
|
WVM(index: 5, name: "RPS", desc: "Win a game of rock paper scissors"),
|
||||||
//WVM(name: "Smash", desc: "It'll never turn off"),
|
//WVM(name: "Smash", desc: "It'll never turn off"),
|
||||||
//WVM(name: "Walk", desc: "Walk a few steps"),
|
//WVM(name: "Walk", desc: "Walk a few steps"),
|
||||||
//WVM(name: "Jump", desc: "Make a few jumps")
|
//WVM(name: "Jump", desc: "Make a few jumps")
|
||||||
|
|||||||
Reference in New Issue
Block a user