// // BeanCollectionExtractionViewController.m // iBean // // Created by Eddie Ehlin on 2013-01-18. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // #import "BeanCollectionExtractionViewController.h" #import "BeanCollectionInfoViewController.h" #import "BeanCollection+Interface.h" #import "BeanCell.h" #import "Bean+Interface.h" #import "AppDelegate+Storage.h" @interface BeanCollectionExtractionViewController () @end @implementation BeanCollectionExtractionViewController - (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 initViewController]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"EditBeanCollectionSegue"]) { BeanCollectionInfoViewController *infoViewController = (BeanCollectionInfoViewController *)[[segue.destinationViewController viewControllers] objectAtIndex:0]; [infoViewController initWithModeAndBeanCollection:YES :self.beanCollection]; } } /***************************************************** Utility methods *****************************************************/ #pragma mark - Utility methods - (void) initViewController { NSLog(@"BeanCollectionExtraction - initViewController"); if (self.beanCollection != nil) { self.navigationItem.title = self.beanCollection.name; self.extractionProgressLabel.text = [self.beanCollection.extractionTime stringValue]; if (![self.beanCollection.note isEqualToString:@""]) { self.beanCollectionNoteTextView.text = self.beanCollection.note; } else { NSLog(@"Note is empty!"); [self.beanCollectionNoteTextView setHidden:YES]; } } else { #warning TODO - Add an error message? } } - (void) initWithBeanCollection:(BeanCollection *)bc { self.beanCollection = bc; } - (void) updateExtractionProgress { self.extractionProgress -= 0.1f; self.extractionProgressLabel.text = [NSString stringWithFormat:@"%1.1f", self.extractionProgress]; if (self.extractionProgress <= 0.0f) { NSLog(@"Timer reached its limit!"); [self haltExtractionTimer]; NSLog(@"Extraction count = %@", [(AppDelegate*) [[UIApplication sharedApplication] delegate] incrementExtractionCount]); } } - (void) haltExtractionTimer { if ([(AppDelegate*) [[UIApplication sharedApplication] delegate] getTimer] != nil) { [(AppDelegate*) [[UIApplication sharedApplication] delegate] haltTimer:YES]; //Return UI to "Start extraction" state. [self timerInterrupted]; } } - (void) timerInterrupted { NSLog(@"BeanCollectionExtractionViewController - timerInterrupted"); //Return UI to "Start extraction" state. [self.extractionButton setTitle:@"Start extraction" forState:UIControlStateNormal]; self.extractionProgressLabel.text = [self.beanCollection.extractionTime stringValue]; self.extractionInProgress = NO; } /***************************************************** UI Actions *****************************************************/ #pragma mark - UI action methods - (void) startExtraction:(id)sender { //Extraction in progress? if ([(AppDelegate*) [[UIApplication sharedApplication] delegate] getTimer] == nil || self.extractionInProgress == NO) { self.extractionProgress = [self.beanCollection.extractionTime doubleValue]; [self.extractionButton setTitle:@"Stop extraction" forState:UIControlStateNormal]; self.extractionInProgress = YES; [(AppDelegate*) [[UIApplication sharedApplication] delegate] createTimer:self :@selector(updateExtractionProgress) :nil :YES]; } else if ([(AppDelegate*) [[UIApplication sharedApplication] delegate] getTimer] != nil) { [self haltExtractionTimer]; } } /***************************************************** Delegates *****************************************************/ #pragma mark - UITableViewDelegate & datasource - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.beanCollection.beans.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"BeanCell"; BeanCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //Configure the cell and set its title Bean *currentBean = [self.beanCollection.beans objectAtIndex:indexPath.row]; [cell initWithNameGrindSettingAndAmount:currentBean.name :currentBean.grindSetting :currentBean.amount]; return cell; } @end