// // InstaBeanViewController.m // iBean // // Created by Eddie Ehlin on 2012-12-31. // Copyright (c) 2012 Eddie Ehlin. All rights reserved. // #import "InstaBeanViewController.h" #import "AppDelegate+Storage.h" @interface InstaBeanViewController () @end @implementation InstaBeanViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self initInstaBean]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /***************************************************** Utility methods *****************************************************/ #pragma mark - Utility methods - (void) initInstaBean { if (self.configuration == nil) { NSLog(@"Loading & setting stepper!"); self.configuration = [(AppDelegate*) [[UIApplication sharedApplication] delegate] getInstaBeanConfiguration]; if (self.configuration == nil) { //TODO: Display error! NSLog(@"initInstaBean received nil for configuration!"); #warning TODO: Display error!? return; } } [self.extractionSettingStepper setValue:[self.configuration.extractionTime doubleValue]]; [self updateExtractionSettingLabel]; } - (void) updateExtractionProgressLabel { self.extractionProgress += 0.1f; self.extractionProgressLabel.text = [NSString stringWithFormat:@"%1.1f", self.extractionProgress]; if (self.extractionProgress >= self.extractionSettingStepper.value) { NSLog(@"Timer reached its limit!"); [self haltExtractionTimer]; } } - (void) updateExtractionSettingLabel { self.extractionSettingLabel.text = [NSString stringWithFormat:@"%1.0f", self.extractionSettingStepper.value]; } - (void) haltExtractionTimer { if (self.timer != nil) { [self.timer invalidate]; self.timer = nil; //Return UI to "Start extraction" state. [self.extractionButton setTitle:@"Start extraction" forState:UIControlStateNormal]; [self.extractionSettingStepper setEnabled:YES]; } } /***************************************************** UI Actions *****************************************************/ #pragma mark - IBActions - (IBAction)startExtraction:(id)sender { //Extraction in progress? if (self.timer == nil) { self.extractionProgress = 0.0f; [self.extractionButton setTitle:@"Stop extraction" forState:UIControlStateNormal]; [self.extractionSettingStepper setEnabled:NO]; self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateExtractionProgressLabel) userInfo:nil repeats:YES]; } else { [self haltExtractionTimer]; } } - (IBAction)setExtractionTimer:(id)sender { if (self.configuration != nil) { self.configuration.extractionTime = [NSNumber numberWithDouble:self.extractionSettingStepper.value]; //Let's save our config. NSError* error = [(AppDelegate*) [[UIApplication sharedApplication] delegate] save]; if (error != nil) { NSLog(@"InstaBean unable to save configuration!"); #warning TODO: Handle this error? } } [self updateExtractionSettingLabel]; } @end