Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CurrentWorkoutController.m
  3. //  Time Krunch!
  4. //
  5. //  Created by Michail Grebionkin on 05.06.11.
  6. //  Copyright 2011 smile2mobile. All rights reserved.
  7. //
  8.  
  9. #import "CurrentWorkoutController.h"
  10. #import "Model/Workout.h"
  11. #import "Model/AlertAdditions.h"
  12. #import "Time_Krunch_AppDelegate.h"
  13. #import "WorkoutSummaryController.h"
  14. #import <AudioToolbox/AudioToolbox.h>
  15.  
  16.  
  17. @implementation CurrentWorkoutController
  18.  
  19.  
  20. -(id)initWithAlert:(Alert *)anAlert currentWorkoutIndex:(NSInteger)index seconds:(NSInteger)secs {
  21.     self = [super init];
  22.     if (self) {
  23.         alert = [anAlert retain];
  24.         currentWorkoutIndex = index;
  25.         currentSet = 1;
  26.         seconds = secs;
  27.     }
  28.     return self;
  29. }
  30.  
  31.  
  32. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  33. {
  34.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  35.     if (self) {
  36.         // Custom initialization
  37.     }
  38.     return self;
  39. }
  40.  
  41. - (void)dealloc
  42. {
  43.     [alert release];
  44.     [_scrollView release];
  45.     [currentWorkout release], currentWorkout = nil;
  46.     [stateLabel release], stateLabel = nil;
  47.     [_webView release], _webView = nil;
  48.     [nextWorkoutButton release], nextWorkoutButton = nil;
  49.     [goButton release], goButton = nil;
  50.     [workoutTimer invalidate];
  51.     [workoutTimer release];
  52.     [timerLabel release];
  53.     [blueBgImage release], blueBgImage = nil;
  54.     [greenBgImage release], greenBgImage = nil;
  55.     [super dealloc];
  56. }
  57.  
  58. - (void)didReceiveMemoryWarning
  59. {
  60.     // Releases the view if it doesn't have a superview.
  61.     [super didReceiveMemoryWarning];
  62.    
  63.     // Release any cached data, images, etc that aren't in use.
  64. }
  65.  
  66.  
  67. - (NSManagedObjectContext*)managedObjectContext {
  68.     Time_Krunch_AppDelegate *appDelegate = (Time_Krunch_AppDelegate*)[[UIApplication sharedApplication] delegate];
  69.     return appDelegate.managedObjectContext;
  70. }
  71.  
  72.  
  73. - (void)updateStateLabel {
  74.     if ([currentWorkout.numberOfRepetitions intValue] != -1) {
  75.         stateLabel.text = [NSString stringWithFormat:@"SET %d: %d REPETITIONS", currentSet, [currentWorkout.numberOfRepetitions intValue]];
  76.     }
  77.     else {
  78.         stateLabel.text = [NSString stringWithFormat:@"SET %d: %d SECONDS", currentSet, [currentWorkout.numberOfSeconds intValue]];
  79.     }
  80. }
  81.  
  82.  
  83. #pragma mark - actions
  84.  
  85.  
  86. - (void)goTimerHandler:(NSTimer*)timer {
  87.     currentSet++;
  88.     if ([currentWorkout.numberOfSets intValue] < currentSet) {
  89.         nextWorkoutButton.enabled = YES;
  90.     }
  91.     else {
  92.         goButton.enabled = YES;
  93.     }
  94.     timerLabel.hidden = YES;
  95. }
  96.  
  97.  
  98. - (void)updateTimerLabelHandler:(NSTimer*)timer {
  99.     static NSInteger passSeconds = 0;
  100.    
  101. //    timerLabel.text = [NSString stringWithFormat:@"%02d", passSeconds];
  102.     NSInteger sn = [timerLabel.text intValue];
  103.     timerLabel.text = [NSString stringWithFormat:@"%02d", sn+1];
  104.    
  105.     passSeconds++;
  106. }
  107.  
  108.  
  109. - (void)goWorkoutAction:(id)sender {
  110.     goButton.enabled = NO;
  111.     NSTimer *goTimer = [NSTimer timerWithTimeInterval:[currentWorkout.numberOfSeconds intValue] target:self selector:@selector(goTimerHandler:) userInfo:nil repeats:NO];
  112.     [[NSRunLoop currentRunLoop] addTimer:goTimer forMode:NSDefaultRunLoopMode];
  113.    
  114.     if (isTimeBased) {
  115.         timerLabel.hidden = NO;
  116.         timerLabel.text = @"00";
  117.         NSTimer *updateTimerLableTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateTimerLabelHandler:) userInfo:nil repeats:YES];
  118.         [[NSRunLoop currentRunLoop] addTimer:updateTimerLableTimer forMode:NSDefaultRunLoopMode];
  119.     }
  120. }
  121.  
  122.  
  123. - (void)nextWorkoutAction:(id)sender {
  124.     if ([currentWorkout.numberOfSets intValue] > currentSet) { // begin next set
  125.         currentSet++;
  126.         setSeconds = 0;
  127.         [self updateStateLabel];
  128.         return;
  129.     }
  130.     else { // begin next workout
  131.         if (currentWorkoutIndex+1 < [[alert enabledSortedWorkouts:[self managedObjectContext]] count]) {
  132.             [workoutTimer invalidate];
  133.             CurrentWorkoutController *nextWorkoutController = [[CurrentWorkoutController alloc] initWithAlert:alert currentWorkoutIndex:currentWorkoutIndex+1 seconds:seconds];
  134.             [self.navigationController pushViewController:nextWorkoutController animated:YES];
  135.             [nextWorkoutController release];
  136.         }
  137.         else {
  138.             [workoutTimer invalidate];
  139.             WorkoutSummaryController *summary = [[WorkoutSummaryController alloc] initWithStyle:UITableViewStyleGrouped alert:alert totalWorkoutTime:seconds];
  140.             [self.navigationController pushViewController:summary animated:YES];
  141.             [summary release];
  142.         }
  143.     }
  144. }
  145.  
  146.  
  147. - (void)skipWorkoutAction:(id)sender {
  148.     currentSet = 999; // no next sets - force to begin next workout
  149.     [self nextWorkoutAction:sender];
  150. }
  151.  
  152.  
  153. - (void)exitWorkoutAction:(id)sender {
  154.     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];
  155.     [alertView show];
  156.     [alertView release];
  157. }
  158.  
  159.  
  160. - (void)workoutTimerHandler:(NSTimer*)timer {
  161.     seconds++;
  162.    
  163.     if (!isTimeBased) {
  164.         NSInteger numberOfReps = [currentWorkout.numberOfRepetitions intValue];
  165.         if (setSeconds >= (numberOfReps * 2 + 10)) {
  166.             if (flashCounter < 10) {
  167.                 if (setSeconds % 2) {
  168.                     [nextWorkoutButton setBackgroundImage:greenBgImage forState:UIControlStateNormal];
  169.                 }
  170.                 else {
  171.                     [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
  172.                 }
  173.                 if (setSeconds % 10) {
  174.                     AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
  175.                 }
  176.                 flashCounter++;
  177.                 if (flashCounter >= 10) {
  178.                     [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
  179.                 }
  180.             }
  181.         }
  182.         setSeconds++;
  183.     }
  184. }
  185.  
  186.  
  187. #pragma mark -
  188.  
  189.  
  190. - (NSString*)loadHTMLStringFromFile:(NSString*)fileName {
  191.     NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"html"];
  192.     NSError *error = nil;
  193.     NSString *contentString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
  194.     return contentString;
  195. }
  196.  
  197.  
  198. - (void)loadFileWithName:(NSString *)fileName {
  199.     NSString *path = [[NSBundle mainBundle] bundlePath];
  200.     NSURL *baseURL = [NSURL fileURLWithPath:path];
  201.     NSString *headerString = [self loadHTMLStringFromFile:@"header"];
  202.     NSString *contentString = [self loadHTMLStringFromFile:fileName];
  203.     [_webView loadHTMLString:[NSString stringWithFormat:@"%@%@</body></html>", headerString, contentString] baseURL:baseURL];
  204. }
  205.  
  206.  
  207. -(void)webViewDidFinishLoad:(UIWebView *)webView {
  208.     NSString *js = @"function getDocHeight() { \
  209.    var D = document; \
  210.    return '' + Math.max( \
  211.    Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), \
  212.    Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), \
  213.    Math.max(D.body.clientHeight, D.documentElement.clientHeight) \
  214.    ); \
  215.    } \
  216.    getDocHeight().toString(); \
  217.    ";
  218.     NSString *documentHeight = [_webView stringByEvaluatingJavaScriptFromString:js];
  219.    
  220.     CGRect newFrame = _webView.frame;
  221.     newFrame.size.height = [documentHeight floatValue];
  222.     _webView.frame = newFrame;
  223.  
  224.     [_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _webView.frame.origin.y + _webView.frame.size.height + 40.)];
  225. }
  226.  
  227.  
  228. #pragma mark - View lifecycle
  229.  
  230. /*
  231. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  232. - (void)loadView
  233. {
  234. }
  235. */
  236.  
  237.  
  238. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  239. - (void)viewDidLoad
  240. {
  241.     [super viewDidLoad];
  242.    
  243.     currentWorkout = [[[alert enabledSortedWorkouts:[self managedObjectContext]] objectAtIndex:currentWorkoutIndex] retain];
  244.    
  245.     self.navigationItem.title = currentWorkout.name;
  246.     self.navigationItem.hidesBackButton = YES;
  247.    
  248.     _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  249.     _scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  250.     [self.view addSubview:_scrollView];
  251.    
  252.     stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0., 15., _scrollView.frame.size.width, 21.)];
  253.     stateLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
  254.     stateLabel.font = [UIFont boldSystemFontOfSize:17.];
  255.     stateLabel.textAlignment = UITextAlignmentCenter;
  256.     [_scrollView addSubview:stateLabel];
  257.     [self updateStateLabel];
  258.    
  259.     blueBgImage = [[[UIImage imageNamed:@"blue-button-bg.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0] retain];
  260.    
  261.     nextWorkoutButton = [[UIButton alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width / 2. - 60., stateLabel.frame.origin.y + stateLabel.frame.size.height + 10., 120., 45.)];
  262.     [nextWorkoutButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
  263.     [nextWorkoutButton setTitle:@"Next" forState:UIControlStateNormal];
  264.     [nextWorkoutButton addTarget:self action:@selector(nextWorkoutAction:) forControlEvents:UIControlEventTouchUpInside];
  265.     nextWorkoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:17.];
  266.     nextWorkoutButton.titleLabel.textColor = [UIColor whiteColor];
  267.     [_scrollView addSubview:nextWorkoutButton];
  268.    
  269.     if ([currentWorkout.numberOfRepetitions intValue] == -1) {
  270.         goButton = [[UIButton alloc] initWithFrame:CGRectMake(45., stateLabel.frame.origin.y + stateLabel.frame.size.height + 10., 120., 45.)];
  271.         [goButton setBackgroundImage:blueBgImage forState:UIControlStateNormal];
  272.         [goButton setTitle:@"Go" forState:UIControlStateNormal];
  273.         [goButton addTarget:self action:@selector(goWorkoutAction:) forControlEvents:UIControlEventTouchUpInside];
  274.         goButton.titleLabel.font = [UIFont boldSystemFontOfSize:17.];
  275.         goButton.titleLabel.textColor = [UIColor whiteColor];
  276.         [_scrollView addSubview:goButton];
  277.        
  278.         nextWorkoutButton.center = CGPointMake(goButton.frame.origin.x + goButton.frame.size.width + 10. + nextWorkoutButton.frame.size.width / 2., nextWorkoutButton.center.y);
  279.         nextWorkoutButton.enabled = NO;
  280.     }
  281.    
  282.     _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10., nextWorkoutButton.frame.origin.y + nextWorkoutButton.frame.size.height + 10., _scrollView.frame.size.width - 20., 1.)];
  283.     _webView.opaque = NO;
  284.     _webView.backgroundColor = [UIColor clearColor];
  285.     for (UIView *v in [_webView subviews]) {
  286.         if ([v isKindOfClass:[UIScrollView class]]) {
  287.             [(UIScrollView*)v setScrollEnabled:NO];
  288.             [(UIScrollView*)v setBounces:NO];
  289.         }
  290.     }
  291.     _webView.delegate = self;
  292.     [self loadFileWithName:[currentWorkout.name lowercaseString]];
  293.     [_scrollView addSubview:_webView];
  294.  
  295.     workoutTimer = [[NSTimer timerWithTimeInterval:1 target:self selector:@selector(workoutTimerHandler:) userInfo:nil repeats:YES] retain];
  296.     [[NSRunLoop currentRunLoop] addTimer:workoutTimer forMode:NSDefaultRunLoopMode];
  297.    
  298.     UIBarButtonItem *skipButton = [[UIBarButtonItem alloc] initWithTitle:@"Skip" style:UIBarButtonItemStyleBordered target:self action:@selector(skipWorkoutAction:)];
  299.     self.navigationItem.rightBarButtonItem = skipButton;
  300.     [skipButton release];
  301.    
  302.     UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(exitWorkoutAction:)];
  303.     self.navigationItem.leftBarButtonItem = homeButton;
  304.     [homeButton release];
  305.  
  306.     isTimeBased = ([currentWorkout.numberOfSeconds intValue] != -1);
  307.     if (isTimeBased) {
  308.         timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0., 130., self.view.frame.size.width, 150.)];
  309.         timerLabel.font = [UIFont systemFontOfSize:70.];
  310.         timerLabel.backgroundColor = [UIColor clearColor];
  311.         timerLabel.textColor = [UIColor redColor];
  312.         timerLabel.shadowColor = [UIColor blackColor];
  313.         timerLabel.shadowOffset = CGSizeMake(2., 2.);
  314.         timerLabel.textAlignment = UITextAlignmentCenter;
  315.         timerLabel.text = @"00";
  316.         timerLabel.hidden = YES;
  317.         [self.view addSubview:timerLabel];
  318.         [self.view bringSubviewToFront:timerLabel];
  319.     }
  320.     else {
  321.         greenBgImage = [[[UIImage imageNamed:@"green-button-bg.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0] retain];
  322.     }
  323. }
  324.  
  325.  
  326. - (void)viewDidUnload
  327. {
  328.     [super viewDidUnload];
  329.     // Release any retained subviews of the main view.
  330.     // e.g. self.myOutlet = nil;
  331.     [_scrollView release], _scrollView = nil;
  332.     [currentWorkout release], currentWorkout = nil;
  333.     [stateLabel release], stateLabel = nil;
  334.     [_webView release], _webView = nil;
  335.     [nextWorkoutButton release], nextWorkoutButton = nil;
  336.     [goButton release], goButton = nil;
  337.     [timerLabel release], timerLabel = nil;
  338.     [blueBgImage release], blueBgImage = nil;
  339.     [greenBgImage release], greenBgImage = nil;
  340. }
  341.  
  342. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  343. {
  344.     // Return YES for supported orientations
  345.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  346. }
  347.  
  348.  
  349. #pragma mark - alert view delegate
  350.  
  351.  
  352. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  353.     if (buttonIndex == 0) { // quit workout
  354.         [self dismissModalViewControllerAnimated:YES];
  355.     }
  356.     if (buttonIndex == 1) { // continue workout
  357.         // nothig to do
  358.     }
  359. }
  360.  
  361.  
  362. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement