Merge pull request #7 from hykilpikonna/errorDebug

[+] Fix more things
This commit is contained in:
Hykilpikonna
2021-01-28 19:01:10 -05:00
committed by GitHub
5 changed files with 36 additions and 77 deletions
+15 -49
View File
@@ -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
}
+12
View File
@@ -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)
}
}
}
/**
+1 -10
View File
@@ -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"
-9
View File
@@ -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
}
+8 -9
View File
@@ -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() } }