Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CopyDayViewController.m
- // ShapeUpClub
- //
- // Created by Martin Wählby on 2010-11-23.
- // Copyright 2010 Sillens AB. All rights reserved.
- //
- #import "CopyDayViewController.h"
- #import "ShapeUpClubAppDelegate.h"
- #import "NSDate-Utilities.h"
- #import "StandardCell.h"
- #import "DiaryDay.h"
- #import "DescriptionFooterView.h"
- #import "TitleHeaderView.h"
- #import "DiaryTitleCell.h"
- #import "FoodItem.h"
- #import "CopyItem.h"
- #import "DiaryListViewController.h"
- @implementation CopyDayViewController
- @synthesize keyboardToolbar;
- @synthesize addType, addTypeString, selectedRow;
- @synthesize breakfastSection,lunchSection,dinnerSection,snackSection,exerciseSection, earlySnackTitleRow, afternoonSnackTitleRow, otherSnackTitleRow;
- @synthesize changeMealTypeButton;
- @synthesize toolbar;
- @synthesize tableView;
- @synthesize day;
- @synthesize numberOfSnacksRows;
- @synthesize breakfastItems, lunchItems, dinnerItems, snackItems;
- @synthesize pickerView;
- @synthesize mealTypeButton;
- @synthesize infoLabel;
- bool changingMealType;
- #pragma mark -
- #pragma mark View lifecycle
- - (void)viewDidLoad {
- [super viewDidLoad];
- UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back",nil) style:UIBarButtonItemStylePlain target:nil action:nil];
- self.navigationItem.backBarButtonItem = backButton;
- [backButton release];
- UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Copy", nil) style:UIBarButtonItemStyleDone target:self action:@selector(saveClicked:)];
- self.navigationItem.rightBarButtonItem = saveButton;
- [saveButton release];
- ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate *)[[UIApplication sharedApplication] delegate];
- if (!delegate.profile.isGold) {
- [self.view addSubview:[delegate getGoldLabelView]];
- }
- pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 216)];
- pickerView.showsSelectionIndicator = YES;
- pickerView.dataSource = self;
- pickerView.delegate = self;
- [self changeMealTypeTitle:addType];
- [self initDay];
- [self loadData];
- }
- -(void)loadData{
- [self.day loadData];
- [self initArrays];
- [self.tableView reloadData];
- [self setSections];
- }
- -(void) initDay{
- ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate *)[[UIApplication sharedApplication] delegate];
- NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
- [components setDay:-1];
- NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
- NSDate *date = [gregorian dateByAddingComponents:components toDate:delegate.diaryViewController.day.date options:0];
- day = [[DiaryDay alloc] initWithDate:date];
- [day loadData];
- }
- -(void)initArrays{
- breakfastItems = [[NSMutableArray alloc]init];
- lunchItems = [[NSMutableArray alloc]init];
- dinnerItems = [[NSMutableArray alloc]init];
- snackItems = [[NSMutableArray alloc]init];
- NSArray *array;
- array = day.breakfast;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [breakfastItems addObject:item];
- }
- array = day.lunch;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [lunchItems addObject:item];
- }
- array = day.dinner;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [dinnerItems addObject:item];
- }
- array = day.earlysnack;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [snackItems addObject:item];
- }
- int indexStart = [snackItems count] +1;
- array = day.afternoonsnack;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [snackItems addObject:item];
- }
- indexStart = [snackItems count] +1;
- array = day.other;
- for(int i=0; i<[array count]; i++){
- CopyItem *item = [[CopyItem alloc]init];
- item.row = i;
- item.isChecked = NO;
- [item setItem:[array objectAtIndex:i]];
- [snackItems addObject:item];
- }
- }
- -(void)setSections {
- NSInteger firstSection = 0;
- NSInteger numberOfSections = 0;
- if (day.breakfast.count > 0) {
- breakfastSection = firstSection;
- numberOfSections++;
- } else {
- breakfastSection = -1;
- }
- if (day.lunch.count > 0) {
- lunchSection = firstSection+numberOfSections;
- numberOfSections++;
- } else {
- lunchSection = -1;
- }
- if (day.dinner.count > 0) {
- dinnerSection = firstSection+numberOfSections;
- numberOfSections++;
- } else {
- dinnerSection = -1;
- }
- if (day.earlysnack.count > 0 || day.afternoonsnack.count > 0 || day.other.count > 0) {
- NSInteger numberOfRows = 1;
- if (day.earlysnack.count > 0) {
- earlySnackTitleRow = numberOfRows;
- numberOfRows += [day.earlysnack count]+1;
- } else {
- earlySnackTitleRow = -1;
- }
- if (day.afternoonsnack.count > 0) {
- afternoonSnackTitleRow = numberOfRows;
- numberOfRows += [day.afternoonsnack count]+1;
- } else {
- afternoonSnackTitleRow = -1;
- }
- if (day.other.count > 0) {
- otherSnackTitleRow = numberOfRows;
- } else {
- otherSnackTitleRow = -1;
- }
- snackSection = firstSection+numberOfSections;
- numberOfSections++;
- } else {
- snackSection = -1;
- }
- }
- - (void)saveClicked:(id)sender{
- ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate*)[[UIApplication sharedApplication] delegate];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Copy %@?",@"Copy breakfast?"),self.addTypeString] message:[NSString stringWithFormat:NSLocalizedString(@"Do you want to copy %@ from %@ to %@?",@"Do you want to copy breakfast from Yesterday to Today?"),self.addTypeString, @"these items",[delegate.diaryViewController.day dateToString]]
- delegate:self cancelButtonTitle:NSLocalizedString(@"No",nil) otherButtonTitles:NSLocalizedString(@"Yes",nil), nil];
- [alert show];
- [alert release];
- }
- -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- if(buttonIndex == 1){
- ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate*)[[UIApplication sharedApplication] delegate];
- NSDate *date = delegate.diaryViewController.day.date;
- for(CopyItem *item in breakfastItems){
- if(item.isChecked){
- if (item.isFood){
- FoodItem *foodItem = (FoodItem*)[item getItem];
- [foodItem insertToDb:self.addType date:date];
- }else{
- MealItem *mealItem = (MealItem*)[item getItem];
- MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
- [newMealItem loadAll];
- newMealItem.amount = mealItem.amount;
- [newMealItem insertToDb:self.addType date:date];
- [newMealItem release];
- }
- }
- }
- for(CopyItem *item in lunchItems){
- if(item.isChecked){
- if (item.isFood){
- FoodItem *foodItem = (FoodItem*)[item getItem];
- [foodItem insertToDb:self.addType date:date];
- }else{
- MealItem *mealItem = (MealItem*)[item getItem];
- MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
- [newMealItem loadAll];
- newMealItem.amount = mealItem.amount;
- [newMealItem insertToDb:self.addType date:date];
- [newMealItem release];
- }
- }
- }
- for(CopyItem *item in dinnerItems){
- if(item.isChecked){
- if (item.isFood){
- FoodItem *foodItem = (FoodItem*)[item getItem];
- [foodItem insertToDb:self.addType date:date];
- }else{
- MealItem *mealItem = (MealItem*)[item getItem];
- MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
- [newMealItem loadAll];
- newMealItem.amount = mealItem.amount;
- [newMealItem insertToDb:self.addType date:date];
- [newMealItem release];
- }
- }
- }
- for(CopyItem *item in snackItems){
- if(item.isChecked){
- if (item.isFood){
- FoodItem *foodItem = (FoodItem*)[item getItem];
- [foodItem insertToDb:self.addType date:date];
- }else{
- MealItem *mealItem = (MealItem*)[item getItem];
- MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
- [newMealItem loadAll];
- newMealItem.amount = mealItem.amount;
- [newMealItem insertToDb:self.addType date:date];
- [newMealItem release];
- }
- }
- }
- [delegate syncPart:kSyncMealItem];
- [delegate syncPart:kSyncFoodItem];
- [delegate.diaryViewController setShowSection:self.addType];
- [delegate reloadDiaryAndSyncViewController];
- [self.navigationController popToViewController:delegate.diaryViewController animated:YES];
- }
- }
- #pragma mark -
- #pragma mark Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- //Om inte dagboken är satt ska inget visas i dagboken
- if (!day) {
- return 0;
- }
- // Return the number of sections.
- [self setSections];
- int sections = [day numberOfMealSections];
- if(day.exercise.count != 0){
- sections--;
- }
- return sections;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSInteger titleRow = 1;
- if (section == breakfastSection) {
- return day.breakfast.count+titleRow;
- } else if (section == lunchSection) {
- return day.lunch.count+titleRow;
- } else if (section == dinnerSection){
- return day.dinner.count+titleRow;
- } else {
- NSInteger titleRows = 0;
- if (earlySnackTitleRow != -1) {
- titleRows++;
- }
- if (afternoonSnackTitleRow != -1) {
- titleRows++;
- }
- if (otherSnackTitleRow != -1) {
- titleRows++;
- }
- numberOfSnacksRows = day.earlysnack.count + day.afternoonsnack.count + day.other.count + titleRow + titleRows;
- return numberOfSnacksRows;
- }
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- TitleHeaderView *headerView;
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TitleHeaderView" owner:self options:nil];
- headerView = [topLevelObjects objectAtIndex:0];
- if (section == 0) {
- headerView.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Copy from %@", nil), [self.day dateToString]];
- return headerView;
- }
- return nil;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- if (section == 0) {
- return 44;
- } else {
- return 0;
- }
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.row == 0) {
- return 60;
- } else if (indexPath.section == snackSection && (indexPath.row == earlySnackTitleRow || indexPath.row == afternoonSnackTitleRow || indexPath.row == otherSnackTitleRow)) {
- return 20;
- } else {
- return 44;
- }
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section == snackSection) {
- if (indexPath.row == 0) {
- DiaryTitleCell *cell;
- static NSString *firstCellId = @"FirstDiaryTitleCell";
- cell = (DiaryTitleCell*)[self.tableView dequeueReusableCellWithIdentifier:firstCellId];
- if (cell == nil) {
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:firstCellId owner:self options:nil];
- cell = [topLevelObjects objectAtIndex:0];
- }
- cell.accessoryView = nil;
- cell.titleLabel.text = NSLocalizedString(@"Snacks",nil);
- cell.descriptionLabel.text = [day snackCaloriesToString];
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"snack_big" ofType:@"png"]];
- cell.iconView.image = icon;
- return cell;
- } else if (indexPath.row == earlySnackTitleRow || indexPath.row == afternoonSnackTitleRow || indexPath.row == otherSnackTitleRow) {
- StandardCell *cell;
- static NSString *titleCellId = @"DiarySmallTitleCell";
- cell = (StandardCell*)[self.tableView dequeueReusableCellWithIdentifier:titleCellId];
- if (cell == nil) {
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:titleCellId owner:self options:nil];
- cell = [topLevelObjects objectAtIndex:0];
- }
- if (indexPath.row == earlySnackTitleRow) {
- cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Early Snack (%@)",@"Early Snack (200 calories)"),[day earlysnackCaloriesToString]];
- } else if (indexPath.row == afternoonSnackTitleRow) {
- cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Afternoon Snack (%@)",@"Afternoon Snack (200 calories)"),[day afternoonsnackCaloriesToString]];
- } else if (indexPath.row == otherSnackTitleRow) {
- cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Other (%@)",@"Other Snack (200 calories)"),[day otherCaloriesToString]];
- }
- return cell;
- } else {
- ImageDetailedCell *cell;
- NSString *cellId;
- if(indexPath.row == numberOfSnacksRows-1){
- cellId = @"LastImageDetailedCell";
- }else{
- cellId = @"ImageDetailedCell";
- }
- cell = (ImageDetailedCell*)[self.tableView dequeueReusableCellWithIdentifier:cellId];
- if (cell == nil) {
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
- cell = [topLevelObjects objectAtIndex:0];
- }
- NSArray *objects;
- NSInteger rowIndex = 0;
- if (otherSnackTitleRow != -1 && indexPath.row > otherSnackTitleRow) {
- rowIndex = indexPath.row - otherSnackTitleRow - 1;
- objects = day.other;
- } else if (afternoonSnackTitleRow != -1 && indexPath.row > afternoonSnackTitleRow) {
- rowIndex = indexPath.row - afternoonSnackTitleRow - 1;
- objects = day.afternoonsnack;
- } else {
- rowIndex = indexPath.row - earlySnackTitleRow - 1;
- objects = day.earlysnack;
- }
- NSObject *food = (NSObject*)[objects objectAtIndex:rowIndex];
- if ([food isMemberOfClass:[FoodItem class]]) {
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[((FoodItem*)food) image] ofType:@"png"]];
- cell.iconView.image = icon;
- cell.titleLabel.text = [(FoodItem *)food titleAndAmountToString];
- cell.descriptionLabel.text = [(FoodItem *)food totalCaloriesToString];
- } else {
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fodef" ofType:@"png"]];
- cell.iconView.image = icon;
- cell.titleLabel.text = [(MealItem *)food titleAndAmountToString];
- cell.descriptionLabel.text = [(MealItem *)food totalCaloriesToString];
- }
- int row = indexPath.row;
- if(indexPath.row > earlySnackTitleRow && earlySnackTitleRow != -1){
- row--;
- }
- if(indexPath.row > afternoonSnackTitleRow && afternoonSnackTitleRow != -1){
- row--;
- }
- if(indexPath.row > otherSnackTitleRow && otherSnackTitleRow != -1){
- row--;
- }
- CopyItem *item = [snackItems objectAtIndex:row-1];
- if(indexPath.row != 0 && indexPath.row != afternoonSnackTitleRow && indexPath.row != earlySnackTitleRow && indexPath.row != otherSnackTitleRow){
- if(!item.isChecked){
- cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone;
- }else{
- cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
- }
- }
- return cell;
- }
- } else {
- NSArray *objects = nil;
- if (indexPath.section == breakfastSection) {
- objects = day.breakfast;
- } else if (indexPath.section == lunchSection) {
- objects = day.lunch;
- } else if (indexPath.section == dinnerSection) {
- objects = day.dinner;
- }
- if (indexPath.row == 0) {
- DiaryTitleCell *cell;
- static NSString *firstCellId = @"FirstDiaryTitleCell";
- cell = (DiaryTitleCell*)[self.tableView dequeueReusableCellWithIdentifier:firstCellId];
- if (cell == nil) {
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:firstCellId owner:self options:nil];
- cell = [topLevelObjects objectAtIndex:0];
- }
- cell.accessoryView = nil;
- if (indexPath.section == breakfastSection) {
- cell.titleLabel.text = NSLocalizedString(@"Breakfast",nil);
- cell.descriptionLabel.text = [day breakfastCaloriesToString];
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"breakfast_big" ofType:@"png"]];
- cell.iconView.image = icon;
- } else if (indexPath.section == lunchSection) {
- cell.titleLabel.text = NSLocalizedString(@"Lunch",nil);
- cell.descriptionLabel.text = [day lunchCaloriesToString];
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lunch_big" ofType:@"png"]];
- cell.iconView.image = icon;
- } else if (indexPath.section == dinnerSection) {
- cell.titleLabel.text = NSLocalizedString(@"Dinner",nil);
- cell.descriptionLabel.text = [day dinnerCaloriesToString];
- UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dinner_big" ofType:@"png"]];
- cell.iconView.image = icon;
- }
- return cell;
- } else {
- NSString *cellId;
- if(indexPath.row == objects.count){
- cellId = @"LastImageDetailedCell";
- }else{
- cellId = @"ImageDetailedCell";
- }
- ImageDetailedCell *cell;
- cell = (ImageDetailedCell*)[self.tableView dequeueReusableCellWithIdentifier:cellId];
- if (cell == nil) {
- NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
- cell = [topLevelObjects objectAtIndex:0];
- }
- NSObject *food = (NSObject*)[objects objectAtIndex:indexPath.row-1];
- CopyItem *item;
- if (indexPath.section == breakfastSection) {
- item = [breakfastItems objectAtIndex:indexPath.row - 1];
- } else if (indexPath.section == lunchSection) {
- item = [lunchItems objectAtIndex:indexPath.row - 1];
- } else if (indexPath.section == dinnerSection) {
- item = [dinnerItems objectAtIndex:indexPath.row - 1];
- }
- if(indexPath.row != 0){
- if(!item.isChecked){
- cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone;
- }else{
- cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
- }
- }
- UIImage *icon = nil;
- if ([food isMemberOfClass:[FoodItem class]]) {
- NSString *imageName = [((FoodItem*)food) image];
- icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
- cell.titleLabel.text = [(FoodItem *)food titleAndAmountToString];
- cell.descriptionLabel.text = [(FoodItem *)food totalCaloriesToString];
- } else {
- icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fodef" ofType:@"png"]];
- cell.titleLabel.text = [(MealItem *)food titleAndAmountToString];
- cell.descriptionLabel.text = [(MealItem *)food totalCaloriesToString];
- }
- cell.iconView.image = icon;
- return cell;
- }
- }
- }
- #pragma mark -
- #pragma mark Table view delegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if(indexPath.row != 0){
- CopyItem *item;
- if (indexPath.section == breakfastSection) {
- item = [breakfastItems objectAtIndex:indexPath.row - 1];
- } else if (indexPath.section == lunchSection) {
- item = [lunchItems objectAtIndex:indexPath.row - 1];
- } else if (indexPath.section == dinnerSection) {
- item = [dinnerItems objectAtIndex:indexPath.row - 1];
- } else{
- int row = indexPath.row;
- if(indexPath.row > earlySnackTitleRow && earlySnackTitleRow != -1){
- row--;
- }
- if(indexPath.row > afternoonSnackTitleRow && afternoonSnackTitleRow != -1){
- row--;
- }
- if(indexPath.row > otherSnackTitleRow && otherSnackTitleRow != -1){
- row--;
- }
- item = [snackItems objectAtIndex:row-1];
- }
- item.isChecked = !item.isChecked;
- }else{
- if (indexPath.section == breakfastSection) {
- [self selectAllItemsInArray:breakfastItems];
- } else if (indexPath.section == lunchSection) {
- [self selectAllItemsInArray:lunchItems];
- } else if (indexPath.section == dinnerSection) {
- [self selectAllItemsInArray:dinnerItems];
- } else{
- [self selectAllItemsInArray:snackItems];
- }
- }
- [self.tableView reloadData];
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- -(void)selectAllItemsInArray: (NSMutableArray *)array{
- for(CopyItem *item in array){
- if(!item.isChecked){
- for(CopyItem *item2 in array){
- item2.isChecked = YES;
- }
- return;
- }
- }
- for(CopyItem *item in array){
- item.isChecked = NO;
- }
- }
- - (IBAction)calendarPressed:(id)sender {
- DiaryListViewController *diaryListViewController = [[DiaryListViewController alloc] initWithNibName:@"DiaryListViewController" bundle:nil];
- diaryListViewController.showFuture = NO;
- diaryListViewController.copyDayViewController = self;
- [diaryListViewController loadData];
- UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:diaryListViewController];
- navCon.navigationBar.barStyle = UIBarStyleBlack;
- [diaryListViewController release];
- [self presentModalViewController:navCon animated:YES];
- [navCon release];
- }
- - (IBAction)changeMealtypePressed:(id)sender {
- CGRect frame = CGRectMake(0, self.view.bounds.size.height - 216, 320, 216);
- if(!changingMealType){
- changingMealType = YES;
- [self.view addSubview:pickerView];
- [UIView animateWithDuration:0.3
- delay:0
- options: UIViewAnimationCurveEaseIn
- animations:^{
- pickerView.frame = frame;
- }
- completion:^(BOOL finished){
- }];
- //[self performSelector:@selector(highlightmealTypeButton) withObject:nil afterDelay:0.0];
- switch (addType) {
- case 1:
- [self.pickerView selectRow:0 inComponent:0 animated:YES];
- break;
- case 4:
- [self.pickerView selectRow:1 inComponent:0 animated:YES];
- break;
- case 2:
- [self.pickerView selectRow:2 inComponent:0 animated:YES];
- break;
- case 5:
- [self.pickerView selectRow:3 inComponent:0 animated:YES];
- break;
- case 3:
- [self.pickerView selectRow:4 inComponent:0 animated:YES];
- break;
- case 6:
- [self.pickerView selectRow:5 inComponent:0 animated:YES];
- break;
- default:
- break;
- }
- }else{
- changingMealType = NO;
- frame.origin.y += 216;
- [UIView animateWithDuration:0.3
- delay:0
- options: UIViewAnimationCurveEaseIn
- animations:^{
- pickerView.frame = frame;
- }
- completion:^(BOOL finished){
- [pickerView removeFromSuperview];
- }];
- //self.mealTypeButton.highlighted = NO;
- }
- }
- #pragma mark -
- #pragma mark Picker view
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
- return 1;
- }
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
- return 6;
- }
- -(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
- if(view){
- return view;
- }
- else{
- UIView *cell = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 40)];
- UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(65, 5, 200, 30)];
- title.backgroundColor = [UIColor clearColor];
- UIImageView *cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 3, 34, 34)];
- switch (row) {
- case 0:
- title.text = NSLocalizedString(@"Breakfast",nil);
- cellImage.image = [UIImage imageNamed:@"breakfast.png"];
- break;
- case 1:
- title.text = NSLocalizedString(@"Early Snack",nil);
- cellImage.image = [UIImage imageNamed:@"snack.png"];
- break;
- case 2:
- title.text = NSLocalizedString(@"Lunch",nil);
- cellImage.image = [UIImage imageNamed:@"lunch.png"];
- break;
- case 3:
- title.text = NSLocalizedString(@"Afternoon Snack",nil);
- cellImage.image = [UIImage imageNamed:@"snack.png"];
- break;
- case 4:
- title.text = NSLocalizedString(@"Dinner",nil);
- cellImage.image = [UIImage imageNamed:@"dinner.png"];
- break;
- case 5:
- title.text = NSLocalizedString(@"Other",nil);
- cellImage.image = [UIImage imageNamed:@"snack.png"];
- break;
- default:
- title.text = @"<Mealtype>";
- cellImage.image = [UIImage imageNamed:@"breakfast.png"];
- break;
- }
- [cell addSubview:title];
- [cell addSubview:cellImage];
- return cell;
- };
- }
- -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
- switch (row) {
- case 0:
- [self changeMealTypeTitle:1];
- break;
- case 1:
- [self changeMealTypeTitle:4];
- break;
- case 2:
- [self changeMealTypeTitle:2];
- break;
- case 3:
- [self changeMealTypeTitle:5];
- break;
- case 4:
- [self changeMealTypeTitle:3];
- break;
- case 5:
- [self changeMealTypeTitle:6];
- break;
- default:
- break;
- }
- if(changingMealType){
- changingMealType = false;
- [UIView animateWithDuration:0.3
- delay:0
- options: UIViewAnimationCurveEaseIn
- animations:^{
- self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height, 320, 216);
- }
- completion:^(BOOL finished){
- [self.pickerView removeFromSuperview];
- }];
- }
- }
- -(void)changeMealTypeTitle: (int) type{
- addType = type;
- [self createAddTypeString];
- self.title = [NSString stringWithFormat:NSLocalizedString(@"Copy %@", @"Copy breakfast"), addTypeString];
- }
- -(void)createAddTypeString {
- if (addType == 1) {
- self.addTypeString = NSLocalizedString(@"Breakfast",nil);
- }
- else if (addType == 2) {
- self.addTypeString = NSLocalizedString(@"Lunch",nil);
- }
- else if (addType == 3) {
- self.addTypeString = NSLocalizedString(@"Dinner",nil);
- }
- else if (addType == 4) {
- self.addTypeString = NSLocalizedString(@"Early Snack",nil);
- }
- else if (addType == 5) {
- self.addTypeString = NSLocalizedString(@"Afternoon Snack",nil);
- }
- else if (addType == 6) {
- self.addTypeString = NSLocalizedString(@"Other",nil);
- }
- else {
- self.addTypeString = @"";
- }
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Relinquish ownership any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- [self setMealTypeButton:nil];
- [self setChangeMealTypeButton:nil];
- [self setToolbar:nil];
- [self setTableView:nil];
- [self setKeyboardToolbar:nil];
- [self setPickerView:nil];
- // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
- // For example: self.myOutlet = nil;
- }
- - (void)dealloc {
- [addTypeString release];
- [keyboardToolbar release];
- [tableView release];
- [toolbar release];
- [changeMealTypeButton release];
- [pickerView release];
- [mealTypeButton release];
- [infoLabel release];
- [super dealloc];
- }
- @end
Add Comment
Please, Sign In to add comment