diff --git a/ProjectClock.xcodeproj/project.pbxproj b/ProjectClock.xcodeproj/project.pbxproj index f9fa29e..95840b7 100644 --- a/ProjectClock.xcodeproj/project.pbxproj +++ b/ProjectClock.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 7C83963625AF375B0027A94C /* NotificationLogic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963525AF375B0027A94C /* NotificationLogic.swift */; }; 7C83963925AF68980027A94C /* TestingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C83963825AF68980027A94C /* TestingViewController.swift */; }; C7E638E825B88F8B00799512 /* MathExpressions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E638E725B88F8B00799512 /* MathExpressions.swift */; }; + F0DF7C0725BCD9FC0064A26B /* StopwatchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0DF7C0625BCD9FC0064A26B /* StopwatchViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -46,6 +47,7 @@ 7C83963525AF375B0027A94C /* NotificationLogic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationLogic.swift; sourceTree = ""; }; 7C83963825AF68980027A94C /* TestingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestingViewController.swift; sourceTree = ""; }; C7E638E725B88F8B00799512 /* MathExpressions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathExpressions.swift; sourceTree = ""; }; + F0DF7C0625BCD9FC0064A26B /* StopwatchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopwatchViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -92,6 +94,7 @@ 4FD642DF25B4D5F30069171E /* AlarmActivationViewController.swift */, 4FD642D225B48C380069171E /* AlarmActivator.swift */, 4FF0684425A5F18700304E6B /* Main.storyboard */, + F0DF7C0625BCD9FC0064A26B /* StopwatchViewController.swift */, 7C83963525AF375B0027A94C /* NotificationLogic.swift */, 4F98955125A9260400F51321 /* Net.swift */, 4F509BD125AE22D100726227 /* Models.swift */, @@ -187,6 +190,7 @@ 4F98955225A9260400F51321 /* Net.swift in Sources */, 7C83963925AF68980027A94C /* TestingViewController.swift in Sources */, 4FF0684325A5F18700304E6B /* AccountViewController.swift in Sources */, + F0DF7C0725BCD9FC0064A26B /* StopwatchViewController.swift in Sources */, 4FF0683F25A5F18700304E6B /* AppDelegate.swift in Sources */, 4FD642D325B48C380069171E /* AlarmActivator.swift in Sources */, 4FD642DB25B4B7F60069171E /* Utils.swift in Sources */, diff --git a/ProjectClock/AlarmActivationViewController.swift b/ProjectClock/AlarmActivationViewController.swift index 092de54..d976b31 100644 --- a/ProjectClock/AlarmActivationViewController.swift +++ b/ProjectClock/AlarmActivationViewController.swift @@ -13,12 +13,17 @@ class AlarmActivationViewController: UIViewController var timer: Timer? var currentAlarm: Alarm? - //Outlets + //Puzzle outlets @IBOutlet weak var puzzleView: UIView! @IBOutlet weak var puzzleQuestionLabel: UILabel! @IBOutlet weak var puzzleAnswerInput: UITextField! var puzzleAnswers: [Int] = [] + //RPS Outlets + @IBOutlet weak var rpsView: UIView! + @IBOutlet weak var rpsResult: UILabel! + + init?(coder: NSCoder, currentAlarm: Alarm) { self.currentAlarm = currentAlarm @@ -35,6 +40,7 @@ class AlarmActivationViewController: UIViewController super.viewDidLoad() //Hide all inactive wakemethods puzzleView.isHidden = true + rpsView.isHidden = true timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AlarmActivationViewController.playSound), userInfo: nil, repeats: true) setAlarmType() @@ -61,6 +67,10 @@ class AlarmActivationViewController: UIViewController puzzleView.isHidden = false case "Smash": print("") + case "RPS": + rpsView.isHidden = false + //Get Choice here + //rpsAction(choice: choice) default: print("Invalid alarm type") } @@ -78,6 +88,31 @@ class AlarmActivationViewController: UIViewController } } + //Gets RPS choice + @IBAction func rockChoice(_ sender: Any) { + if rpsAction(choice: .rock)! { + endAlarm() + } else { + rpsResult.text = "Paper: You lost, try again" + } + } + @IBAction func paperChoice(_ sender: Any) { + if rpsAction(choice: .paper)! { + endAlarm() + } else { + rpsResult.text = "Scissors: You lost, try again" + } + } + @IBAction func scissorChoice(_ sender: Any) { + if rpsAction(choice: .scissors)! { + endAlarm() + } else { + rpsResult.text = "Rock: You lost, try again" + } + } + + + //Standard way to turn off and close the alarm func endAlarm() { timer?.invalidate() diff --git a/ProjectClock/Base.lproj/Main.storyboard b/ProjectClock/Base.lproj/Main.storyboard index d87ddd9..cd44af6 100644 --- a/ProjectClock/Base.lproj/Main.storyboard +++ b/ProjectClock/Base.lproj/Main.storyboard @@ -4,6 +4,7 @@ + @@ -198,7 +199,7 @@ - + @@ -268,7 +269,144 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -507,19 +645,13 @@ - - - + + + + + + + + + + + + + + + + + + + @@ -566,11 +747,13 @@ + + - + @@ -587,6 +770,7 @@ + @@ -679,7 +863,7 @@ - + @@ -696,7 +880,7 @@ - + @@ -764,22 +948,29 @@ - + + + + + + + + diff --git a/ProjectClock/MathExpressions.swift b/ProjectClock/MathExpressions.swift index 9fbc095..5a8171a 100644 --- a/ProjectClock/MathExpressions.swift +++ b/ProjectClock/MathExpressions.swift @@ -163,10 +163,19 @@ class RPS { case rock = "ROCK" case paper = "PAPER" case scissors = "SCISSORS" - - static func randomComputerChoice() -> Choice { - let choices: [Choice] = [.rock, .paper, .scissors] - return choices[Int.random(in: 0...2)] + } + + static func randomComputerChoice() -> Choice { + let choices: [Choice] = [.rock, .paper, .scissors] + return choices[Int.random(in: 0...2)] + } + + func playRPS(you: Choice, computer: Choice) -> Bool? { + if you == .rock && computer == .scissors { return true } + else if you == .paper && computer == .rock { return true} + else if you == .scissors && computer == .paper { return true } + else { + return false } } @@ -186,18 +195,4 @@ class RPS { resultsLabel.text = playRPS(you: .scissors, computer: computerChoice) } */ - - func playRPS(you: Choice, computer: Choice) -> Bool? { - if you == computer { return nil } - else if you == .rock && computer == .scissors { return true } - else if you == .paper && computer == .rock { return false} - else if you == .scissors && computer == .paper { return true } - else { - let randomNum = Int.random(in: 0...2) - if randomNum == 0 { return false } - // DO THESE RETURN THE RIGHT THING? - else if randomNum == 1 { return false} - else { return false } - } - } } diff --git a/ProjectClock/Models.swift b/ProjectClock/Models.swift index cf1d8bc..7792d6f 100644 --- a/ProjectClock/Models.swift +++ b/ProjectClock/Models.swift @@ -32,7 +32,7 @@ struct WVM: Codable let wvms = [ WVM(name: "Factor", desc: "Factor a binomial"), WVM(name: "RPS", desc: "Win a game of rock paper scissors"), - WVM(name: "Smash", desc: "It'll never truns off"), + 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") ] diff --git a/ProjectClock/NotificationLogic.swift b/ProjectClock/NotificationLogic.swift index 640a349..29a620c 100644 --- a/ProjectClock/NotificationLogic.swift +++ b/ProjectClock/NotificationLogic.swift @@ -15,10 +15,12 @@ func walkAction() { } func jumpAction() { - let rps = RPS() + } -func rpsAction() { +func rpsAction(choice: RPS.Choice) -> Bool? { + let rps = RPS() + return rps.playRPS(you: choice, computer: RPS.randomComputerChoice()) } diff --git a/ProjectClock/StopwatchViewController.swift b/ProjectClock/StopwatchViewController.swift new file mode 100644 index 0000000..cfe64ec --- /dev/null +++ b/ProjectClock/StopwatchViewController.swift @@ -0,0 +1,129 @@ +// +// StopwatchViewController.swift +// ProjectClock +// +// Created by Dallon Archibald on 1/23/21. +// Reference: https://youtu.be/H691qFRpaWA + +import UIKit + +class StopwatchViewController: UIViewController { + + @IBOutlet weak var hourLabel: UILabel! + @IBOutlet weak var minuteLabel: UILabel! + @IBOutlet weak var secondLabel: UILabel! + + @IBOutlet weak var startButton: UIButton! + @IBOutlet weak var stopButton: UIButton! + @IBOutlet weak var resetButton: UIButton! + @IBOutlet weak var lapButton: UIButton! + + @IBOutlet weak var tableView: UITableView! + + var hours = 0 + var minutes = 0 + var seconds = 0 + + var lappedTimes: [String] = [] + var timer = Timer() + + override func viewDidLoad() { + super.viewDidLoad() + + //lapButton.isHidden = true + } + + @IBAction func start(_ sender: UIButton) { + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(count), userInfo: nil, repeats: true) + //startButton.isHidden = true + //lapButton.isHidden = false + } + + @objc fileprivate func count() { + seconds += 1 + + if seconds == 60 { + minutes += 1 + seconds = 0 + } + if minutes == 60 { + hours += 1 + minutes = 0 + } + if hours == 24 { + resetTimes() + } + + if seconds >= 10 { secondLabel.text = "\(seconds)" } + else { secondLabel.text = "0\(seconds)" } + if minutes >= 10 { minuteLabel.text = "\(minutes)" } + else { minuteLabel.text = "0\(minutes)" } + if hours >= 10 { hourLabel.text = "\(hours)" } + else { hourLabel.text = "0\(hours)" } + } + + @IBAction func stop(_ sender: UIButton) { + timer.invalidate() + //startButton.isHidden = false + } + + @IBAction func reset(_ sender: UIButton) { + resetTimes() + } + + func resetTimes() { + seconds = 0 + minutes = 0 + seconds = 0 + lappedTimes = [] + timer.invalidate() + secondLabel.text = "00" + minuteLabel.text = "00" + hourLabel.text = "00" + tableView.reloadData() + //startButton.isHidden = false + //lapButton.isHidden = true + } + + @IBAction func lap(_ sender: UIButton) { + var currentSec = "" + if seconds >= 10 { currentSec = "\(seconds)" } + else { currentSec = "0\(seconds)" } + + var currentMin = "" + if minutes >= 10 { currentMin = "\(minutes)" } + else { currentMin = "0\(minutes)" } + + var currentHour = "" + if hours >= 10 { currentHour = "\(hours)" } + else { currentHour = "0\(hours)" } + + let currentTime = "\(currentHour):\(currentMin):\(currentSec)" //CHECK THIS + lappedTimes.append(currentTime) + + let indexPath = IndexPath(row: lappedTimes.count - 1, section: 0) + tableView.insertRows(at: [indexPath], with: .automatic) + } +} + +extension StopwatchViewController: UITableViewDelegate, UITableViewDataSource { + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return lappedTimes.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "lapCell", for: indexPath) + cell.textLabel?.text = lappedTimes[indexPath.row] + cell.selectionStyle = .none + return cell + } + + func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { + if editingStyle == .delete { + lappedTimes.remove(at: indexPath.row) + + tableView.deleteRows(at: [indexPath], with: .automatic) + } + } +} diff --git a/ProjectClock/TestingViewController.swift b/ProjectClock/TestingViewController.swift index 7d8d169..a6251b3 100644 --- a/ProjectClock/TestingViewController.swift +++ b/ProjectClock/TestingViewController.swift @@ -58,7 +58,7 @@ class TestingViewController: UIViewController @IBAction func addAlarm(_ sender: Any) { let (h, m, _) = Date().getHMS() - Alarms.fromLocal().apply { $0.list.append(Alarm(hour: h, minute: m, text: "Test alarm - \(h * m)", wakeMethod: wvms[0], repeats: [true, true, true, true, true, true, true], oneTime: true, lastActivate: Date().added(.minute, -1))) }.localSave() + Alarms.fromLocal().apply { $0.list.append(Alarm(hour: h, minute: m, text: "Test alarm - \(h * m)", wakeMethod: wvms[1], repeats: [true, true, true, true, true, true, true], oneTime: true, lastActivate: Date().added(.minute, -1))) }.localSave() } @IBAction func deleteAlarm(_ sender: Any)