Skip to content

Commit

Permalink
Add Rest Timer (#22)
Browse files Browse the repository at this point in the history
* Add Rest Timer

* Align view

* Add Settings

* Enable/Disable cells

* Add RestTime to settings

* Use Rest Time from Settings

* Add logic for settings

* YouTube videos are disabled if the video is not available

* Versioning
  • Loading branch information
mazurio authored Dec 31, 2016
1 parent 8ace239 commit 1b5e22f
Show file tree
Hide file tree
Showing 12 changed files with 742 additions and 31 deletions.
8 changes: 8 additions & 0 deletions BodyweightFitness.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
732CDDF11D84485D004C6607 /* bodyweight_fitness_one_leg_foot_supported_l_sit.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 732CDDEC1D84485D004C6607 /* bodyweight_fitness_one_leg_foot_supported_l_sit.mp4 */; };
732CDDF21D84485D004C6607 /* bodyweight_fitness_tuck_l_sit.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 732CDDED1D84485D004C6607 /* bodyweight_fitness_tuck_l_sit.mp4 */; };
732CDDF31D84485D004C6607 /* bodyweight_fitness_wrist_mobility.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 732CDDEE1D84485D004C6607 /* bodyweight_fitness_wrist_mobility.mp4 */; };
73374A8E1E167A6300EBC47B /* RestTimerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73374A8D1E167A6300EBC47B /* RestTimerViewController.swift */; };
73374A901E167AEF00EBC47B /* RestTimerView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 73374A8F1E167AEF00EBC47B /* RestTimerView.xib */; };
734132E51E130FAE00BAEFD2 /* starting_stretching_flexibility_routine.json in Resources */ = {isa = PBXBuildFile; fileRef = 734132E41E130FAE00BAEFD2 /* starting_stretching_flexibility_routine.json */; };
734132FA1E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_intermediate.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 734132E71E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_intermediate.mp4 */; };
734132FB1E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_beginner.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 734132E81E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_beginner.mp4 */; };
Expand Down Expand Up @@ -259,6 +261,8 @@
732CDDEC1D84485D004C6607 /* bodyweight_fitness_one_leg_foot_supported_l_sit.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = bodyweight_fitness_one_leg_foot_supported_l_sit.mp4; sourceTree = "<group>"; };
732CDDED1D84485D004C6607 /* bodyweight_fitness_tuck_l_sit.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = bodyweight_fitness_tuck_l_sit.mp4; sourceTree = "<group>"; };
732CDDEE1D84485D004C6607 /* bodyweight_fitness_wrist_mobility.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = bodyweight_fitness_wrist_mobility.mp4; sourceTree = "<group>"; };
73374A8D1E167A6300EBC47B /* RestTimerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestTimerViewController.swift; sourceTree = "<group>"; };
73374A8F1E167AEF00EBC47B /* RestTimerView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RestTimerView.xib; sourceTree = "<group>"; };
734132E41E130FAE00BAEFD2 /* starting_stretching_flexibility_routine.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = starting_stretching_flexibility_routine.json; sourceTree = "<group>"; };
734132E71E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_intermediate.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = starting_stretching_underarm_shoulder_stretch_intermediate.mp4; sourceTree = "<group>"; };
734132E81E130FE900BAEFD2 /* starting_stretching_underarm_shoulder_stretch_beginner.mp4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = starting_stretching_underarm_shoulder_stretch_beginner.mp4; sourceTree = "<group>"; };
Expand Down Expand Up @@ -638,6 +642,7 @@
732BA0231C5AE75900C1CDB5 /* ProgressPageViewController.xib */,
7329B68D1D0D84E4005F5F74 /* HomeView.xib */,
738B96CC1D763319009A8625 /* HomeBarView.xib */,
73374A8F1E167AEF00EBC47B /* RestTimerView.xib */,
73BADE161CC3E2F9001ACEDC /* TimedView.xib */,
73BADE1C1CC3F98D001ACEDC /* WeightedView.xib */,
73BF3BA81CBE643200E0258D /* SettingsView.xib */,
Expand All @@ -659,6 +664,7 @@
7366D8DC1C4EEFD500819790 /* ProgressPageViewController.swift */,
7366D8D01C4EE9CC00819790 /* ProgressViewController.swift */,
7329B68B1D0D8476005F5F74 /* HomeViewController.swift */,
73374A8D1E167A6300EBC47B /* RestTimerViewController.swift */,
73BADE181CC3E347001ACEDC /* TimedViewController.swift */,
73BADE1A1CC3F956001ACEDC /* WeightedViewController.swift */,
E0F587381B52730000540104 /* SettingsViewController.swift */,
Expand Down Expand Up @@ -960,6 +966,7 @@
734132E51E130FAE00BAEFD2 /* starting_stretching_flexibility_routine.json in Resources */,
732BA0241C5AE75900C1CDB5 /* ProgressPageViewController.xib in Resources */,
738B96821D760F66009A8625 /* bodyweight_fitness_l_sit_pullup.mp4 in Resources */,
73374A901E167AEF00EBC47B /* RestTimerView.xib in Resources */,
735BEA0A1CBD1F38003616C5 /* SideViewHeaderCell.xib in Resources */,
738B969D1D760F66009A8625 /* bodyweight_fitness_wall_pushup.mp4 in Resources */,
738B96A51D760F66009A8625 /* molding_mobility_circular_shrugs.mp4 in Resources */,
Expand Down Expand Up @@ -1063,6 +1070,7 @@
E0F5872E1B5272C400540104 /* AppDelegate.swift in Sources */,
73AA28FB1CB7ACEB00DEED79 /* DashboardSingleItemCell.swift in Sources */,
73AA28EA1CABE69200DEED79 /* DashboardViewController.swift in Sources */,
73374A8E1E167A6300EBC47B /* RestTimerViewController.swift in Sources */,
734B700B1C4BF7E900CFB6BD /* RepositorySection.swift in Sources */,
73AA28F11CB7ABC300DEED79 /* DashboardSectionCell.swift in Sources */,
7329B68C1D0D8476005F5F74 /* HomeViewController.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions BodyweightFitness/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if(defaults.objectForKey("playAudioWhenTimerStops") == nil) {
defaults.setBool(true, forKey: "playAudioWhenTimerStops")
}

if(defaults.objectForKey("showRestTimer") == nil) {
defaults.setBool(true, forKey: "showRestTimer")
}

if(defaults.objectForKey("showRestTimerAfterWarmup") == nil) {
defaults.setBool(false, forKey: "showRestTimerAfterWarmup")
}

if(defaults.objectForKey("showRestTimerAfterBodylineDrills") == nil) {
defaults.setBool(true, forKey: "showRestTimerAfterBodylineDrills")
}

if(defaults.objectForKey("showRestTimerAfterFlexibilityExercises") == nil) {
defaults.setBool(false, forKey: "showRestTimerAfterFlexibilityExercises")
}
}

func migrateSchemaIfNeeded() {
Expand Down
2 changes: 1 addition & 1 deletion BodyweightFitness/Bodyweight Fitness.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>149</string>
<string>150</string>
<key>Fabric</key>
<dict>
<key>APIKey</key>
Expand Down
156 changes: 156 additions & 0 deletions BodyweightFitness/Controller/RestTimerViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import UIKit
import AVFoundation

class RestTimerViewController: UIViewController, AVAudioPlayerDelegate {
@IBOutlet var timerMinutesButton: UIButton!
@IBOutlet var timerSecondsButton: UIButton!

@IBOutlet var timerPlayButton: UIButton!

@IBOutlet var previousButton: UIButton!
@IBOutlet var nextButton: UIButton!

var rootViewController: WorkoutViewController? = nil
var current: Exercise = RoutineStream.sharedInstance.routine.getFirstExercise()

var audioPlayer: AVAudioPlayer?
var timer = NSTimer()
var isPlaying = false

var seconds = PersistenceManager.getRestTime()
var defaultSeconds = PersistenceManager.getRestTime()

init() {
super.init(nibName: "RestTimerView", bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
}

func changeExercise(currentExercise: Exercise) {
self.current = currentExercise
self.defaultSeconds = PersistenceManager.getRestTime()

if let _ = self.current.previous {
self.previousButton.hidden = false
} else {
self.previousButton.hidden = true
}

if let _ = self.current.next {
self.nextButton.hidden = false
} else {
self.nextButton.hidden = true
}
}

func updateLabel() {
let (_, m, s) = secondsToHoursMinutesSeconds(seconds)

timerMinutesButton.setTitle(printTimerValue(m), forState: UIControlState.Normal)
timerSecondsButton.setTitle(printTimerValue(s), forState: UIControlState.Normal)
}

func printTimerValue(value: Int) -> String {
if(value > 9) {
return String(value)
} else {
return "0" + String(value)
}
}

func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

func stopTimer() {
isPlaying = false

timer.invalidate()
}

func startTimer() {
restartTimer(defaultSeconds)

isPlaying = true

timer = NSTimer.scheduledTimerWithTimeInterval(
1,
target: self,
selector: #selector(updateTimer),
userInfo: nil,
repeats: true
)
}

func restartTimer(seconds: Int) {
stopTimer()

self.seconds = seconds

updateLabel()
}

func updateTimer() {
seconds -= 1

if(seconds <= 0) {
self.rootViewController?.restTimerStopped()

let defaults = NSUserDefaults.standardUserDefaults()
if(defaults.objectForKey("playAudioWhenTimerStops") != nil) {
let playAudioWhenTimerStops = defaults.boolForKey("playAudioWhenTimerStops")
if(playAudioWhenTimerStops) {
audioPlayerStart()
}
} else {
audioPlayerStart()
}
}

updateLabel()
}

func audioPlayerStart() {
let alertSound = NSURL(fileURLWithPath: NSBundle
.mainBundle()
.pathForResource("finished", ofType: "mp3")!)

do {
try AVAudioSession.sharedInstance().setActive(true)

audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound, fileTypeHint: nil)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()
} catch {
print("AVAudioSession errors.")
}
}

func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
do {
try AVAudioSession.sharedInstance().setActive(false, withOptions:
AVAudioSessionSetActiveOptions.NotifyOthersOnDeactivation)
} catch {
print("AVAudioSession errors.")
}
}

@IBAction func stopButtonClicked(sender: AnyObject) {
self.rootViewController?.restTimerStopped()
}

@IBAction func previousButtonClicked(sender: AnyObject) {
self.rootViewController?.previousButtonClicked(sender)
}

@IBAction func nextButtonClicked(sender: AnyObject) {
self.rootViewController?.nextButtonClicked(sender)
}
}
Loading

0 comments on commit 1b5e22f

Please sign in to comment.