JGuizard
8/29/2015 - 3:56 PM

akara.swift

//
//  WorkoutController.swift
//  Akara
//
//  Created by Giuseppe Gullo on 28/08/15.
//  Copyright (c) 2015 Giuseppe Gullo. All rights reserved.
//

import UIKit
import Foundation

class WorkoutController: UIViewController
{
    
    @IBOutlet weak var percentageDoughnut: MCPercentageDoughnutView!

    @IBOutlet weak var tvExercise: UILabel!
    @IBOutlet weak var tvSet: UILabel!
    @IBOutlet weak var tvMessage: UILabel!
    
    var exercise:Exercise!
    var time=0;
    var serie=0
    var progress=0.0
    var timer:NSTimer!
    var excount=0
    var workout:Workout!
    var onrec=false
    var onstart=true
    var onpause=false
    var tts:TTS!
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        NSLog("VIEW DID LOAD")
        tts=TTS()
        initTimer()
        createWorkout()
        startWorkout()
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initTimer()
    {
        self.percentageDoughnut.textStyle = MCPercentageDoughnutViewTextStyleUserDefined;
        self.percentageDoughnut.percentage              = self.percentageDoughnut.percentage + 0
        self.percentageDoughnut.linePercentage          = 0.15;
        self.percentageDoughnut.animationDuration       = 1;
        self.percentageDoughnut.showTextLabel           = true;
        self.percentageDoughnut.animatesBegining        = true;
        self.percentageDoughnut.textLabel.textColor=UIColor.whiteColor()
        self.percentageDoughnut.textLabel.text="15"
        self.percentageDoughnut.textLabel.font=UIFont.boldSystemFontOfSize(14)
    }
    
    func createWorkout()
    {
        workout=Workout()
        var exercise:Exercise=Exercise(name:"Squat",series:5,time:20,rest:30)
        workout.addExercise(exercise)
        exercise=Exercise(name:"Pushup",series:5,time:20,rest:30)
        workout.addExercise(exercise)
        exercise=Exercise(name:"Pullups",series:10,time:10,rest:20)
        workout.addExercise(exercise)
        exercise=Exercise(name:"Dip",series:10,time:10,rest:20)
        workout.addExercise(exercise)
        exercise=Exercise(name:"Crunch",series:5,time:30,rest:10)
        workout.addExercise(exercise)
        exercise=Exercise(name:"Reverse crunch",series:5,time:30,rest:20)
        workout.addExercise(exercise)
    }
    
    func startWorkout()
    {
        exercise=workout.getExercise(0)
        tvExercise.text=exercise.getName()
        tvSet.text="Ready"
        onrec=true
        time=15
        serie=0
        excount=0
        progress=(100.0/Double(exercise.getTime())/100.0)
        NSLog("Progress "+String(format:"%f", progress))
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
        //btStart.setTitle("Stop", forState: UIControlState.Normal)

        NSLog("Exercises: "+String(workout.exerciseCount()))
        self.percentageDoughnut.percentage  = 0;

    }
    
    @IBAction func takeaMinute(sender: AnyObject)
    {
        tvMessage.text="Too hard, eh?"
        tts.speak("giving up is not an option")
        tts.speak("recovers a minute")
        onpause=true
    }
    
    func update()
    {
        if(onpause)
        {
            time=60
            progress=100/60/100
            tvSet.text="Recover"
            self.percentageDoughnut.percentage=0
            onpause=false
        }
        else if(time<=0)
        {
            tvMessage.text="Don't give up!"
            //ON RECOVER COMPLETED
            if(onrec)
            {
                tts.speak("start")
                tvExercise.text=exercise.getName()
                tvSet.text="set "+String(serie+1)+" of "+String(exercise.getSeries())
                onrec=false
                time=exercise.getTime()
                progress=(100.0/Double(exercise.getTime())/100.0)
                NSLog("Progress "+String(format:"%f", progress))
                self.percentageDoughnut.percentage  = 0;
            }
                //START RECOVER
            else
            {
                serie++
                //ON SET COMPLETED
                if(serie>=exercise.getSeries())
                {
                    excount++
                    //ON WORKOUT COMPLETED
                    NSLog("Current exercise "+String(excount))
                    if(excount>=workout.exerciseCount())
                    {
                        tts.speak("workout completed")
                        tvSet.text="Completed"
                        timer.invalidate()
                        timer=nil
                        return
                    }
                    else
                    {
                        exercise=workout.getExercise(excount)
                        serie=0
                    }
                }
                
                tts.speak("recover")
                exercise=workout.getExercise(excount)
                tvSet.text="Recover"
                onrec=true
                time=exercise.getRest()
                progress=(100.0/Double(exercise.getRest())/100.0)
                NSLog("Progress "+String(format:"%f", progress))
                self.percentageDoughnut.percentage  = 0;

                
            }
        }
            
            //NO EVENT, GO ON WITH EXERCISE
        else
        {
            if(time==10&&onrec)
            {
                if(excount==0)
                {
                    tts.speak("First exercise");
                    tts.speak(exercise.getName())
                }
                else if(exercise.getRest()>=15)
                {
                    tts.speak("ready")
                }
                
            }
            else if(time==(exercise.getRest()-5)&&onrec)
            {
                if(serie==0)
                {
                    tts.speak("next exercise "+exercise.getName())
                }
                    
                else if(serie==(exercise.getSeries()-1))
                {
                    tts.speak("one last set")
                }
            }
            else if(time<=6)
            {
                tts.speak(String(time-1))
            }
            
            time--
        }
        
        self.percentageDoughnut.textLabel.text=String(time)
        self.percentageDoughnut.percentage=self.percentageDoughnut.percentage+CGFloat(progress)
    
    }
}