@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,21 @@ class AlarmActivator: UITabBarController
|
|||||||
/// Timer for getting family alarm updates
|
/// Timer for getting family alarm updates
|
||||||
var familyTimer: Timer?
|
var familyTimer: Timer?
|
||||||
|
|
||||||
|
/**
|
||||||
|
Called when the app started
|
||||||
|
*/
|
||||||
override func viewDidLoad()
|
override func viewDidLoad()
|
||||||
{
|
{
|
||||||
start()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,15 +9,6 @@ class AlarmViewController: UIViewController
|
|||||||
{
|
{
|
||||||
super.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
|
// Assign table delegate and data source
|
||||||
AlarmViewController.staticTable = table
|
AlarmViewController.staticTable = table
|
||||||
table.delegate = self
|
table.delegate = self
|
||||||
@@ -90,7 +81,7 @@ class AlarmTableCell: UITableViewCell
|
|||||||
descriptionText.text = "- " + alarm.text
|
descriptionText.text = "- " + alarm.text
|
||||||
enable.isOn = alarm.enabled
|
enable.isOn = alarm.enabled
|
||||||
wvmText.text = alarm.wakeMethod.name
|
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
|
// Display Hour, Minute, and AM or PM
|
||||||
ampm.text = alarm.hour < 12 || alarm.hour == 24 ? "AM" : "PM"
|
ampm.text = alarm.hour < 12 || alarm.hour == 24 ? "AM" : "PM"
|
||||||
|
|||||||
@@ -21,15 +21,6 @@ class DebugViewController: EndEditingOnReturn
|
|||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
wvmStepper.maximumValue = Double(wvms.count - 1)
|
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
|
// End editing on return
|
||||||
wvmInput.delegate = self
|
wvmInput.delegate = self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
@@ -84,7 +85,6 @@ class Alarm: Codable, Equatable
|
|||||||
var wakeMethod: WVM
|
var wakeMethod: WVM
|
||||||
var alarmTone: SystemSoundID
|
var alarmTone: SystemSoundID
|
||||||
var notificationID: String
|
var notificationID: String
|
||||||
var toneName: String
|
|
||||||
|
|
||||||
/// What days does it repeat (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
|
/// What days does it repeat (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
|
||||||
var repeats: [Bool]
|
var repeats: [Bool]
|
||||||
@@ -111,7 +111,6 @@ class Alarm: Codable, Equatable
|
|||||||
self.lastActivate = lastActivate
|
self.lastActivate = lastActivate
|
||||||
self.alarmTone = alarmTone
|
self.alarmTone = alarmTone
|
||||||
self.notificationID = "notification.id.\(Int.random(in: 1...Int.max))"
|
self.notificationID = "notification.id.\(Int.random(in: 1...Int.max))"
|
||||||
self.toneName = toneName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Does it automatically disable after activating once
|
/// Does it automatically disable after activating once
|
||||||
@@ -160,7 +159,7 @@ class Alarms: Codable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read alarms from local storage
|
/// 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
|
/// Read an alarm object from local storage
|
||||||
static func fromLocal() -> Alarms { return Alarms().apply { $0.localRead() } }
|
static func fromLocal() -> Alarms { return Alarms().apply { $0.localRead() } }
|
||||||
|
|||||||
Reference in New Issue
Block a user