diff --git a/ProjectClock/AddAlarmViewController.swift b/ProjectClock/AddAlarmViewController.swift index 0ee31b9..570658b 100644 --- a/ProjectClock/AddAlarmViewController.swift +++ b/ProjectClock/AddAlarmViewController.swift @@ -12,7 +12,6 @@ class AddAlarmViewController: EndEditingOnReturn // Editing variables var alarmCell: AlarmTableCell? = nil var editMode: Bool { alarmCell != nil } - var originalTime: String = "" override func viewDidLoad() { @@ -20,57 +19,27 @@ class AddAlarmViewController: EndEditingOnReturn alarmNameTextField.delegate = self // Load alarm to edit if in edit mode - if let alarmCell = alarmCell + if let alarmCell = alarmCell, let alarm = alarmCell.alarm { // Toggle editing mode 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 - timePicker.date = date! - originalTime = String(dateFormatter.string(from: date!).dropLast(2)) + let (y,m,d) = Date().getYMD() + timePicker.date = Date.create(y, m, d, alarm.hour, alarm.minute) // 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 - } - } + repeatWeekdaysSwitch.isOn = alarm.repeats[1...5].allSatisfy { $0 } + repeatWeekendsSwitch.isOn = alarm.repeats[0] && alarm.repeats[6] - alarmNameTextField.text = String(alarmCell.descriptionText.text!.dropFirst(2)) + alarmNameTextField.text = alarm.text 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) - } - } - } + wvmPicker.selectRow(alarm.wakeMethod.index, inComponent: 0, animated: true) - //Sets alarm tone - if let toneName = alarmCell.toneLabel.text { - for index in 0...ringtones.count-1 { - if toneName == ringtones[index].name { - ringtonePicker.selectRow(index, inComponent: 0, animated: true) - } - } - } + // Sets alarm tone + ringtonePicker.selectRow(ringtones.firstIndex { $0.tone == alarm.alarmTone }!, inComponent: 0, animated: true) } } @@ -100,17 +69,14 @@ class AddAlarmViewController: EndEditingOnReturn Returns the removed Alarm object. */ @discardableResult - func removeCurrentAlarm() -> Alarm? { - 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 { ($0.hour == hours || $0.hour == (hours + 12)) && $0.minute == minutes } + func removeCurrentAlarm() -> Alarm? + { + guard let alarm = alarmCell?.alarm else { return nil } // Removes the alarm from stored alarms - let alarmsObj = Alarms.fromLocal() - alarmsObj.list = Alarms.fromLocal().list.filter { $0 != alarm } - alarmsObj.localSave() + let alarms = Alarms.fromLocal() + alarms.list = alarms.list.filter { $0 != alarmCell?.alarm } + alarms.localSave() return alarm } diff --git a/ProjectClock/AlarmActivator.swift b/ProjectClock/AlarmActivator.swift index 2f48d1b..8f757d4 100644 --- a/ProjectClock/AlarmActivator.swift +++ b/ProjectClock/AlarmActivator.swift @@ -25,9 +25,21 @@ class AlarmActivator: UITabBarController /// Timer for getting family alarm updates var familyTimer: Timer? + /** + Called when the app started + */ override func viewDidLoad() { start() + + // Get notification permissions from user + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in + if success { + print("All set!") + } else if let error = error { + print(error.localizedDescription) + } + } } /** diff --git a/ProjectClock/AlarmViewController.swift b/ProjectClock/AlarmViewController.swift index 64090eb..ecdbc0c 100644 --- a/ProjectClock/AlarmViewController.swift +++ b/ProjectClock/AlarmViewController.swift @@ -8,15 +8,6 @@ class AlarmViewController: UIViewController override func viewDidLoad() { super.viewDidLoad() - - //Get notification permissions from user - UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in - if success { - print("All set!") - } else if let error = error { - print(error.localizedDescription) - } - } // Assign table delegate and data source AlarmViewController.staticTable = table @@ -90,7 +81,7 @@ class AlarmTableCell: UITableViewCell descriptionText.text = "- " + alarm.text enable.isOn = alarm.enabled wvmText.text = alarm.wakeMethod.name - toneLabel.text = alarm.toneName + toneLabel.text = ringtones.first { $0.tone.description == alarm.alarmTone.description }?.name // Display Hour, Minute, and AM or PM ampm.text = alarm.hour < 12 || alarm.hour == 24 ? "AM" : "PM" diff --git a/ProjectClock/DebugViewController.swift b/ProjectClock/DebugViewController.swift index 19af623..fc788f3 100644 --- a/ProjectClock/DebugViewController.swift +++ b/ProjectClock/DebugViewController.swift @@ -21,15 +21,6 @@ class DebugViewController: EndEditingOnReturn super.viewDidLoad() wvmStepper.maximumValue = Double(wvms.count - 1) - // Request notification permission - UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in - if success { - print("All set!") - } else if let error = error { - print(error.localizedDescription) - } - } - // End editing on return wvmInput.delegate = self } diff --git a/ProjectClock/Models.swift b/ProjectClock/Models.swift index 10cfbf6..91466af 100644 --- a/ProjectClock/Models.swift +++ b/ProjectClock/Models.swift @@ -33,18 +33,19 @@ struct Family: Codable struct WVM: Codable { + let index: Int let name: String let desc: String } let wvms = [ - WVM(name: "Shake", desc: "Shake your phone... aggresively!"), - WVM(name: "Math 1", desc: "Easy math expression"), - WVM(name: "Math 2", desc: "Medium math expression"), - WVM(name: "Math 3", desc: "Hard math expression"), - WVM(name: "Factor", desc: "Factor a binomial"), - WVM(name: "RPS", desc: "Win a game of rock paper scissors"), + WVM(index: 0, name: "Shake", desc: "Shake your phone... aggresively!"), + WVM(index: 1, name: "Math 1", desc: "Easy math expression"), + WVM(index: 2, name: "Math 2", desc: "Medium math expression"), + WVM(index: 3, name: "Math 3", desc: "Hard math expression"), + WVM(index: 4, name: "Factor", desc: "Factor a binomial"), + WVM(index: 5, name: "RPS", desc: "Win a game of rock paper scissors"), //WVM(name: "Smash", desc: "It'll never turn off"), //WVM(name: "Walk", desc: "Walk a few steps"), //WVM(name: "Jump", desc: "Make a few jumps") @@ -84,7 +85,6 @@ class Alarm: Codable, Equatable var wakeMethod: WVM var alarmTone: SystemSoundID var notificationID: String - var toneName: String /// What days does it repeat (Sun, Mon, Tue, Wed, Thu, Fri, Sat) var repeats: [Bool] @@ -111,7 +111,6 @@ class Alarm: Codable, Equatable self.lastActivate = lastActivate self.alarmTone = alarmTone self.notificationID = "notification.id.\(Int.random(in: 1...Int.max))" - self.toneName = toneName } /// Does it automatically disable after activating once @@ -160,7 +159,7 @@ class Alarms: Codable } /// Read alarms from local storage - func localRead() { list = JSON.parse([Alarm].self, localStorage.string(forKey: "alarms")!) ?? []} + func localRead() { list = JSON.parse([Alarm].self, localStorage.string(forKey: "alarms")!)! } /// Read an alarm object from local storage static func fromLocal() -> Alarms { return Alarms().apply { $0.localRead() } }