Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CurrentWorkoutController.m
- // Time Krunch!
- //
- // Created by Michail Grebionkin on 05.06.11.
- // Copyright 2011 smile2mobile. All rights reserved.
- //
- #import "CurrentWorkoutController.h"
- #import "Model/Workout.h"
- #import "Model/AlertAdditions.h"
- #import "Time_Krunch_AppDelegate.h"
- #import "WorkoutSummaryController.h"
- #import <AudioToolbox/AudioToolbox.h>
- @implementation CurrentWorkoutController
- -(id)initWithAlert:(Alert *)anAlert currentWorkoutIndex:(NSInteger)index seconds:(NSInteger)secs {
- self = [super init];
- if (self) {
- alert = [anAlert retain];
- currentWorkoutIndex = index;
- currentSet = 1;
- seconds = secs;
- }
- return self;
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)dealloc
- {
- [alert release];
- [_scrollView release];
- [currentWorkout release], currentWorkout = nil;
- [stateLabel release], stateLabel = nil;
- [_webView release], _webView = nil;
- [nextWorkoutButton release], nextWorkoutButton = nil;
- [goButton release], goButton = nil;
- [workoutTimer invalidate];
- [workoutTimer release];
- [timerLabel release];
- [blueBgImage release], blueBgImage = nil;
- [greenBgImage release], greenBgImage = nil;
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (NSManagedObjectContext*)managedObjectContext {
- Time_Krunch_AppDelegate *appDelegate = (Time_Krunch_AppDelegate*)[[UIApplication sharedApplication] delegate];
- return appDelegate.managedObjectContext;
- }
- - (void)updateStateLabel {
- if ([currentWorkout.numberOfRepetitions intValue] != -1) {
- stateLabel.text = [NSString stringWithFormat:@"SET %d: %d REPETITIONS", currentSet, [currentWorkout.numberOfRepetitions intValue]];
- }
- else {
- stateLabel.text = [NSString stringWithFormat:@"SET %d: %d SECONDS", currentSet, [currentWorkout.numberOfSeconds intValue]];
- }
- }
- #pragma mark - actions
- - (void)goTimerHandler:(NSTimer*)timer {
- currentSet++;
- if ([currentWorkout.numberOfSets intValue] < currentSet) {
- nextWorkoutButton.enabled = YES;
- }
- else {
- goButton.enabled = YES;
- }
- timerLabel.hidden = YES;
- }
- - (void)updateTimerLabelHandler:(NSTimer*)timer {
- static NSInteger passSeconds = 0;
- // timerLabel.text = [NSString stringWithFormat:@"%02d", passSeconds];
- NSInteger sn = [timerLabel.text intValue];
- timerLabel.text = [NSString stringWithFormat:@"%02d", sn+1];
- passSeconds++;
- }
- - (void)goWorkoutAction:(id)sender {
- goButton.enabled = NO;
- NSTimer *goTimer = [NSTimer timerWithTimeInterval:[currentWorkout.numberOfSeconds intValue] target:self selector:@selector(goTimerHandler:) userInfo:nil repeats:NO];
- [[NSRunLoop currentRunLoop] addTimer:goTimer forMode:NSDefaultRunLoopMode];
- if (isTimeBased) {
- timerLabel.hidden = NO;
- timerLabel.text = @"00";
- NSTimer *updateTimerLableTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateTimerLabelHandler:) userInfo:nil repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:updateTimerLableTimer forMode:NSDefaultRunLoopMode];
- }
- }
- - (void)nextWorkoutAction:(id)sender {
- if ([currentWorkout.numberOfSets intValue] > currentSet) { // begin next set
- currentSet++;
- setSeconds = 0;
- [self updateStateLabel];
- return;
- }
- else { // begin next workout
- if (currentWorkoutIndex+1 < [[alert enabledSortedWorkouts:[self managedObjectContext]] count]) {
- [workoutTimer invalidate];
- CurrentWorkoutController *nextWorkoutController = [[CurrentWorkoutController alloc] initWithAlert:alert currentWorkoutIndex:currentWorkoutIndex+1 seconds:seconds];
- [self.navigationController pushViewController:nextWorkoutController animated:YES];
- [nextWorkoutController release];
- }
- else {
- [workoutTimer invalidate];
- WorkoutSummaryController *summary = [[WorkoutSummaryController alloc] initWithStyle:UITableViewStyleGrouped alert:alert totalWorkoutTime:seconds];
- [self.navigationController pushViewController:summary animated:YES];
- [summary release];
- }
- }
- }
- - (void)skipWorkoutAction:(id)sender {
- currentSet = 999; // no next sets - force to begin next workout
- [self nextWorkoutAction:sender];
- }
- - (void)exitWorkoutAction:(id)sender {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Time Krunch!" message:@"Today’s workout will be lost if you choose to quit your workout" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Quit Workout", @"Continue", nil];
- [alertView show];
- [alertView release];
- }
- - (void)workoutTimerHandler:(NSTimer*)timer {
- seconds++;
- if (!isTimeBased) {
- NSInteger numberOfReps = [currentWorkout.numberOfRepetitions intValue];
- if (setSeconds >= (numberOfReps * 2 + 10)) {
- if (flashCounter < 10) {
- if (setSeconds % 2) {
- [nextWorkoutButton setBackgroundImage:greenBgImage forState:UIControlStateNormal];
- }
- else {
- [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
- }
- if (setSeconds % 10) {
- AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
- }
- flashCounter++;
- if (flashCounter >= 10) {
- [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
- }
- }
- }
- setSeconds++;
- }
- }
- #pragma mark -
- - (NSString*)loadHTMLStringFromFile:(NSString*)fileName {
- NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];
- NSError *error = nil;
- NSString *contentString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
- return contentString;
- }
- - (void)loadFileWithName:(NSString *)fileName {
- NSString *path = [[NSBundle mainBundle] bundlePath];
- NSURL *baseURL = [NSURL fileURLWithPath:path];
- NSString *headerString = [self loadHTMLStringFromFile:@"header"];
- NSString *contentString = [self loadHTMLStringFromFile:fileName];
- [_webView loadHTMLString:[NSString stringWithFormat:@"%@%@</body></html>", headerString, contentString] baseURL:baseURL];
- }
- -(void)webViewDidFinishLoad:(UIWebView *)webView {
- NSString *js = @"function getDocHeight() { \
- var D = document; \
- return '' + Math.max( \
- Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), \
- Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), \
- Math.max(D.body.clientHeight, D.documentElement.clientHeight) \
- ); \
- } \
- getDocHeight().toString(); \
- ";
- NSString *documentHeight = [_webView stringByEvaluatingJavaScriptFromString:js];
- CGRect newFrame = _webView.frame;
- newFrame.size.height = [documentHeight floatValue];
- _webView.frame = newFrame;
- [_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _webView.frame.origin.y + _webView.frame.size.height + 40.)];
- }
- #pragma mark - View lifecycle
- /*
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView
- {
- }
- */
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- currentWorkout = [[[alert enabledSortedWorkouts:[self managedObjectContext]] objectAtIndex:currentWorkoutIndex] retain];
- self.navigationItem.title = currentWorkout.name;
- self.navigationItem.hidesBackButton = YES;
- _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
- _scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
- [self.view addSubview:_scrollView];
- stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0., 15., _scrollView.frame.size.width, 21.)];
- stateLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
- stateLabel.font = [UIFont boldSystemFontOfSize:17.];
- stateLabel.textAlignment = UITextAlignmentCenter;
- [_scrollView addSubview:stateLabel];
- [self updateStateLabel];
- blueBgImage = [[[UIImage imageNamed:@"blue-button-bg.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0] retain];
- nextWorkoutButton = [[UIButton alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width / 2. - 60., stateLabel.frame.origin.y + stateLabel.frame.size.height + 10., 120., 45.)];
- [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
- [nextWorkoutButton setTitle:@"Next" forState:UIControlStateNormal];
- [nextWorkoutButton addTarget:self action:@selector(nextWorkoutAction:) forControlEvents:UIControlEventTouchUpInside];
- nextWorkoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:17.];
- nextWorkoutButton.titleLabel.textColor = [UIColor whiteColor];
- [_scrollView addSubview:nextWorkoutButton];
- if ([currentWorkout.numberOfRepetitions intValue] == -1) {
- goButton = [[UIButton alloc] initWithFrame:CGRectMake(45., stateLabel.frame.origin.y + stateLabel.frame.size.height + 10., 120., 45.)];
- [goButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
- [goButton setTitle:@"Go" forState:UIControlStateNormal];
- [goButton addTarget:self action:@selector(goWorkoutAction:) forControlEvents:UIControlEventTouchUpInside];
- goButton.titleLabel.font = [UIFont boldSystemFontOfSize:17.];
- goButton.titleLabel.textColor = [UIColor whiteColor];
- [_scrollView addSubview:goButton];
- nextWorkoutButton.center = CGPointMake(goButton.frame.origin.x + goButton.frame.size.width + 10. + nextWorkoutButton.frame.size.width / 2., nextWorkoutButton.center.y);
- nextWorkoutButton.enabled = NO;
- }
- _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10., nextWorkoutButton.frame.origin.y + nextWorkoutButton.frame.size.height + 10., _scrollView.frame.size.width - 20., 1.)];
- _webView.opaque = NO;
- _webView.backgroundColor = [UIColor clearColor];
- for (UIView *v in [_webView subviews]) {
- if ([v isKindOfClass:[UIScrollView class]]) {
- [(UIScrollView*)v setScrollEnabled:NO];
- [(UIScrollView*)v setBounces:NO];
- }
- }
- _webView.delegate = self;
- [self loadFileWithName:[currentWorkout.name lowercaseString]];
- [_scrollView addSubview:_webView];
- workoutTimer = [[NSTimer timerWithTimeInterval:1 target:self selector:@selector(workoutTimerHandler:) userInfo:nil repeats:YES] retain];
- [[NSRunLoop currentRunLoop] addTimer:workoutTimer forMode:NSDefaultRunLoopMode];
- UIBarButtonItem *skipButton = [[UIBarButtonItem alloc] initWithTitle:@"Skip" style:UIBarButtonItemStyleBordered target:self action:@selector(skipWorkoutAction:)];
- self.navigationItem.rightBarButtonItem = skipButton;
- [skipButton release];
- UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(exitWorkoutAction:)];
- self.navigationItem.leftBarButtonItem = homeButton;
- [homeButton release];
- isTimeBased = ([currentWorkout.numberOfSeconds intValue] != -1);
- if (isTimeBased) {
- timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0., 130., self.view.frame.size.width, 150.)];
- timerLabel.font = [UIFont systemFontOfSize:70.];
- timerLabel.backgroundColor = [UIColor clearColor];
- timerLabel.textColor = [UIColor redColor];
- timerLabel.shadowColor = [UIColor blackColor];
- timerLabel.shadowOffset = CGSizeMake(2., 2.);
- timerLabel.textAlignment = UITextAlignmentCenter;
- timerLabel.text = @"00";
- timerLabel.hidden = YES;
- [self.view addSubview:timerLabel];
- [self.view bringSubviewToFront:timerLabel];
- }
- else {
- greenBgImage = [[[UIImage imageNamed:@"green-button-bg.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0] retain];
- }
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- [_scrollView release], _scrollView = nil;
- [currentWorkout release], currentWorkout = nil;
- [stateLabel release], stateLabel = nil;
- [_webView release], _webView = nil;
- [nextWorkoutButton release], nextWorkoutButton = nil;
- [goButton release], goButton = nil;
- [timerLabel release], timerLabel = nil;
- [blueBgImage release], blueBgImage = nil;
- [greenBgImage release], greenBgImage = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- #pragma mark - alert view delegate
- -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 0) { // quit workout
- [self dismissModalViewControllerAnimated:YES];
- }
- if (buttonIndex == 1) { // continue workout
- // nothig to do
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement