Guest User

Untitled

a guest
Sep 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CopyDayViewController.m
  3. //  ShapeUpClub
  4. //
  5. //  Created by Martin Wählby on 2010-11-23.
  6. //  Copyright 2010 Sillens AB. All rights reserved.
  7. //
  8.  
  9. #import "CopyDayViewController.h"
  10. #import "ShapeUpClubAppDelegate.h"
  11. #import "NSDate-Utilities.h"
  12. #import "StandardCell.h"
  13. #import "DiaryDay.h"
  14. #import "DescriptionFooterView.h"
  15. #import "TitleHeaderView.h"
  16. #import "DiaryTitleCell.h"
  17. #import "FoodItem.h"
  18. #import "CopyItem.h"
  19. #import "DiaryListViewController.h"
  20.  
  21.  
  22. @implementation CopyDayViewController
  23. @synthesize keyboardToolbar;
  24.  
  25. @synthesize addType, addTypeString, selectedRow;
  26. @synthesize breakfastSection,lunchSection,dinnerSection,snackSection,exerciseSection, earlySnackTitleRow, afternoonSnackTitleRow, otherSnackTitleRow;
  27. @synthesize changeMealTypeButton;
  28. @synthesize toolbar;
  29. @synthesize tableView;
  30. @synthesize day;
  31. @synthesize numberOfSnacksRows;
  32. @synthesize breakfastItems, lunchItems, dinnerItems, snackItems;
  33. @synthesize pickerView;
  34. @synthesize mealTypeButton;
  35. @synthesize infoLabel;
  36.  
  37. bool changingMealType;
  38.  
  39. #pragma mark -
  40. #pragma mark View lifecycle
  41.  
  42.  
  43. - (void)viewDidLoad {
  44.     [super viewDidLoad];
  45.    
  46.     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back",nil) style:UIBarButtonItemStylePlain target:nil action:nil];
  47.     self.navigationItem.backBarButtonItem = backButton;
  48.     [backButton release];
  49.    
  50.    
  51.     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Copy", nil) style:UIBarButtonItemStyleDone target:self action:@selector(saveClicked:)];
  52.     self.navigationItem.rightBarButtonItem = saveButton;
  53.     [saveButton release];
  54.    
  55.     ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate *)[[UIApplication sharedApplication] delegate];
  56.     if (!delegate.profile.isGold) {
  57.         [self.view addSubview:[delegate getGoldLabelView]];
  58.     }
  59.    
  60.     pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 216)];
  61.     pickerView.showsSelectionIndicator = YES;
  62.     pickerView.dataSource = self;
  63.     pickerView.delegate = self;
  64.    
  65.     [self changeMealTypeTitle:addType];
  66.     [self initDay];
  67.     [self loadData];
  68.    
  69.  
  70.  
  71. }
  72.  
  73. -(void)loadData{
  74.     [self.day loadData];
  75.     [self initArrays];
  76.     [self.tableView reloadData];
  77.    
  78.     [self setSections];
  79. }
  80.  
  81. -(void) initDay{
  82.     ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate *)[[UIApplication sharedApplication] delegate];    
  83.     NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
  84.     [components setDay:-1];
  85.     NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
  86.     NSDate *date = [gregorian dateByAddingComponents:components toDate:delegate.diaryViewController.day.date options:0];
  87.    
  88.    
  89.     day = [[DiaryDay alloc] initWithDate:date];
  90.     [day loadData];
  91.    
  92.    
  93. }
  94.  
  95.  
  96. -(void)initArrays{
  97.     breakfastItems = [[NSMutableArray alloc]init];
  98.     lunchItems = [[NSMutableArray alloc]init];
  99.     dinnerItems = [[NSMutableArray alloc]init];
  100.     snackItems = [[NSMutableArray alloc]init];
  101.    
  102.     NSArray *array;
  103.     array = day.breakfast;
  104.     for(int i=0; i<[array count]; i++){
  105.         CopyItem *item = [[CopyItem alloc]init];
  106.         item.row = i;
  107.         item.isChecked = NO;
  108.         [item setItem:[array objectAtIndex:i]];
  109.         [breakfastItems addObject:item];
  110.     }
  111.    
  112.     array = day.lunch;
  113.     for(int i=0; i<[array count]; i++){
  114.         CopyItem *item = [[CopyItem alloc]init];
  115.         item.row = i;
  116.         item.isChecked = NO;
  117.         [item setItem:[array objectAtIndex:i]];
  118.         [lunchItems addObject:item];
  119.     }
  120.    
  121.     array = day.dinner;
  122.     for(int i=0; i<[array count]; i++){
  123.         CopyItem *item = [[CopyItem alloc]init];
  124.         item.row = i;
  125.         item.isChecked = NO;
  126.         [item setItem:[array objectAtIndex:i]];
  127.         [dinnerItems addObject:item];
  128.     }
  129.    
  130.     array = day.earlysnack;
  131.     for(int i=0; i<[array count]; i++){
  132.         CopyItem *item = [[CopyItem alloc]init];
  133.         item.row = i;
  134.         item.isChecked = NO;
  135.         [item setItem:[array objectAtIndex:i]];
  136.         [snackItems addObject:item];
  137.     }
  138.    
  139.     int indexStart = [snackItems count] +1;
  140.     array = day.afternoonsnack;
  141.     for(int i=0; i<[array count]; i++){
  142.         CopyItem *item = [[CopyItem alloc]init];
  143.         item.row = i;
  144.         item.isChecked = NO;
  145.         [item setItem:[array objectAtIndex:i]];
  146.         [snackItems addObject:item];
  147.     }
  148.  
  149.     indexStart = [snackItems count] +1;
  150.     array = day.other;
  151.     for(int i=0; i<[array count]; i++){
  152.         CopyItem *item = [[CopyItem alloc]init];
  153.         item.row = i;
  154.         item.isChecked = NO;
  155.         [item setItem:[array objectAtIndex:i]];
  156.         [snackItems addObject:item];
  157.     }
  158. }
  159.  
  160. -(void)setSections {
  161.     NSInteger firstSection = 0;
  162.     NSInteger numberOfSections = 0;
  163.    
  164.     if (day.breakfast.count > 0) {
  165.         breakfastSection = firstSection;
  166.         numberOfSections++;
  167.     } else {
  168.         breakfastSection = -1;
  169.     }
  170.    
  171.     if (day.lunch.count > 0) {
  172.         lunchSection = firstSection+numberOfSections;
  173.         numberOfSections++;
  174.     } else {
  175.         lunchSection = -1;
  176.     }
  177.    
  178.     if (day.dinner.count > 0) {
  179.         dinnerSection = firstSection+numberOfSections;
  180.         numberOfSections++;
  181.     } else {
  182.         dinnerSection = -1;
  183.     }
  184.    
  185.     if (day.earlysnack.count > 0 || day.afternoonsnack.count > 0 || day.other.count > 0) {
  186.         NSInteger numberOfRows = 1;
  187.         if (day.earlysnack.count > 0) {
  188.             earlySnackTitleRow = numberOfRows;
  189.             numberOfRows += [day.earlysnack count]+1;
  190.         } else {
  191.             earlySnackTitleRow = -1;
  192.         }
  193.        
  194.         if (day.afternoonsnack.count > 0) {
  195.             afternoonSnackTitleRow = numberOfRows;
  196.             numberOfRows += [day.afternoonsnack count]+1;
  197.            
  198.         } else {
  199.             afternoonSnackTitleRow = -1;
  200.         }
  201.        
  202.         if (day.other.count > 0) {
  203.             otherSnackTitleRow = numberOfRows;
  204.         } else {
  205.             otherSnackTitleRow = -1;
  206.         }
  207.        
  208.         snackSection = firstSection+numberOfSections;
  209.         numberOfSections++;
  210.     } else {
  211.         snackSection = -1;
  212.     }
  213.    
  214.  
  215. }
  216.  
  217. - (void)saveClicked:(id)sender{
  218.     ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate*)[[UIApplication sharedApplication] delegate];
  219.    
  220.     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]]
  221.                                                    delegate:self cancelButtonTitle:NSLocalizedString(@"No",nil) otherButtonTitles:NSLocalizedString(@"Yes",nil), nil];
  222.     [alert show];
  223.     [alert release];
  224. }
  225.  
  226.  
  227. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  228.     if(buttonIndex == 1){
  229.         ShapeUpClubAppDelegate *delegate = (ShapeUpClubAppDelegate*)[[UIApplication sharedApplication] delegate];
  230.         NSDate *date = delegate.diaryViewController.day.date;
  231.  
  232.  
  233.        
  234.         for(CopyItem *item in breakfastItems){
  235.             if(item.isChecked){
  236.                 if (item.isFood){
  237.                     FoodItem *foodItem = (FoodItem*)[item getItem];
  238.                     [foodItem insertToDb:self.addType date:date];
  239.                 }else{
  240.                     MealItem *mealItem = (MealItem*)[item getItem];
  241.                     MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
  242.                     [newMealItem loadAll];
  243.                     newMealItem.amount = mealItem.amount;
  244.                     [newMealItem insertToDb:self.addType date:date];
  245.                     [newMealItem release];
  246.                 }
  247.             }
  248.         }
  249.        
  250.        
  251.         for(CopyItem *item in lunchItems){
  252.             if(item.isChecked){
  253.                 if (item.isFood){
  254.                     FoodItem *foodItem = (FoodItem*)[item getItem];
  255.                     [foodItem insertToDb:self.addType date:date];
  256.                 }else{
  257.                     MealItem *mealItem = (MealItem*)[item getItem];
  258.                     MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
  259.                     [newMealItem loadAll];
  260.                     newMealItem.amount = mealItem.amount;
  261.                     [newMealItem insertToDb:self.addType date:date];
  262.                     [newMealItem release];
  263.                 }
  264.             }
  265.         }
  266.        
  267.        
  268.         for(CopyItem *item in dinnerItems){
  269.             if(item.isChecked){
  270.                 if (item.isFood){
  271.                     FoodItem *foodItem = (FoodItem*)[item getItem];
  272.                     [foodItem insertToDb:self.addType date:date];
  273.                 }else{
  274.                     MealItem *mealItem = (MealItem*)[item getItem];
  275.                     MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
  276.                     [newMealItem loadAll];
  277.                     newMealItem.amount = mealItem.amount;
  278.                     [newMealItem insertToDb:self.addType date:date];
  279.                     [newMealItem release];
  280.                 }
  281.             }
  282.         }
  283.        
  284.         for(CopyItem *item in snackItems){
  285.             if(item.isChecked){
  286.                 if (item.isFood){
  287.                     FoodItem *foodItem = (FoodItem*)[item getItem];
  288.                     [foodItem insertToDb:self.addType date:date];
  289.                 }else{
  290.                     MealItem *mealItem = (MealItem*)[item getItem];
  291.                     MealItem *newMealItem = [[MealItem alloc] initWithAddedId:mealItem.mealId];
  292.                     [newMealItem loadAll];
  293.                     newMealItem.amount = mealItem.amount;
  294.                     [newMealItem insertToDb:self.addType date:date];
  295.                     [newMealItem release];
  296.                 }
  297.             }
  298.         }
  299.        
  300.  
  301.         [delegate syncPart:kSyncMealItem];
  302.         [delegate syncPart:kSyncFoodItem];
  303.         [delegate.diaryViewController setShowSection:self.addType];
  304.         [delegate reloadDiaryAndSyncViewController];
  305.         [self.navigationController popToViewController:delegate.diaryViewController animated:YES];
  306.     }
  307. }
  308.  
  309. #pragma mark -
  310. #pragma mark Table view data source
  311.  
  312. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  313.     //Om inte dagboken är satt ska inget visas i dagboken
  314.     if (!day) {
  315.         return 0;
  316.     }
  317.     // Return the number of sections.
  318.     [self setSections];
  319.     int sections = [day numberOfMealSections];
  320.     if(day.exercise.count != 0){
  321.         sections--;
  322.     }
  323.     return sections;
  324. }
  325.  
  326.  
  327. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  328.     NSInteger titleRow = 1;
  329.    
  330.     if (section == breakfastSection) {
  331.             return day.breakfast.count+titleRow;
  332.     } else if (section == lunchSection) {
  333.             return day.lunch.count+titleRow;       
  334.     } else if (section == dinnerSection){
  335.             return day.dinner.count+titleRow;
  336.     } else {
  337.             NSInteger titleRows = 0;
  338.             if (earlySnackTitleRow != -1) {
  339.                 titleRows++;
  340.             }
  341.             if (afternoonSnackTitleRow != -1) {
  342.                 titleRows++;
  343.             }
  344.             if (otherSnackTitleRow != -1) {
  345.                 titleRows++;
  346.             }
  347.             numberOfSnacksRows = day.earlysnack.count + day.afternoonsnack.count + day.other.count + titleRow  + titleRows;
  348.             return numberOfSnacksRows;
  349.     }
  350. }
  351.  
  352. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  353.     TitleHeaderView *headerView;
  354.     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TitleHeaderView" owner:self options:nil];
  355.     headerView = [topLevelObjects objectAtIndex:0];
  356.    
  357.     if (section == 0) {
  358.         headerView.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Copy from %@", nil), [self.day dateToString]];
  359.         return headerView;
  360.     }
  361.     return nil;
  362. }
  363.  
  364. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  365.     if (section == 0) {
  366.         return 44;
  367.     } else {
  368.         return 0;
  369.     }
  370. }
  371.  
  372.  
  373. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  374.  
  375.         if (indexPath.row == 0) {
  376.             return 60;
  377.         } else if (indexPath.section == snackSection && (indexPath.row == earlySnackTitleRow || indexPath.row == afternoonSnackTitleRow || indexPath.row == otherSnackTitleRow)) {
  378.             return 20;
  379.         } else {
  380.             return 44;
  381.         }
  382. }
  383.  
  384.  
  385.  
  386. // Customize the appearance of table view cells.
  387. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  388.  
  389.         if (indexPath.section == snackSection) {
  390.         if (indexPath.row == 0) {
  391.             DiaryTitleCell *cell;
  392.             static NSString *firstCellId = @"FirstDiaryTitleCell";
  393.            
  394.                 cell = (DiaryTitleCell*)[self.tableView dequeueReusableCellWithIdentifier:firstCellId];
  395.                 if (cell == nil) {
  396.                     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:firstCellId owner:self options:nil];
  397.                     cell = [topLevelObjects objectAtIndex:0];
  398.                 }
  399.             cell.accessoryView = nil;
  400.             cell.titleLabel.text = NSLocalizedString(@"Snacks",nil);
  401.             cell.descriptionLabel.text = [day snackCaloriesToString];
  402.             UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"snack_big" ofType:@"png"]];
  403.             cell.iconView.image = icon;
  404.             return cell;
  405.         } else if (indexPath.row == earlySnackTitleRow || indexPath.row == afternoonSnackTitleRow || indexPath.row == otherSnackTitleRow) {
  406.             StandardCell *cell;
  407.             static NSString *titleCellId = @"DiarySmallTitleCell";
  408.             cell = (StandardCell*)[self.tableView dequeueReusableCellWithIdentifier:titleCellId];
  409.             if (cell == nil) {
  410.                 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:titleCellId owner:self options:nil];
  411.                 cell = [topLevelObjects objectAtIndex:0];
  412.             }
  413.            
  414.             if (indexPath.row == earlySnackTitleRow) {
  415.                 cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Early Snack (%@)",@"Early Snack (200 calories)"),[day earlysnackCaloriesToString]];
  416.             } else if (indexPath.row == afternoonSnackTitleRow) {      
  417.                 cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Afternoon Snack (%@)",@"Afternoon Snack (200 calories)"),[day afternoonsnackCaloriesToString]];
  418.             } else if (indexPath.row == otherSnackTitleRow) {
  419.                 cell.titleLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Other (%@)",@"Other Snack (200 calories)"),[day otherCaloriesToString]];
  420.             }
  421.  
  422.            
  423.  
  424.            
  425.             return cell;
  426.         } else {
  427.             ImageDetailedCell *cell;
  428.            
  429.             NSString *cellId;
  430.             if(indexPath.row == numberOfSnacksRows-1){
  431.                 cellId = @"LastImageDetailedCell";
  432.             }else{
  433.                 cellId = @"ImageDetailedCell";
  434.             }
  435.            
  436.             cell = (ImageDetailedCell*)[self.tableView dequeueReusableCellWithIdentifier:cellId];
  437.             if (cell == nil) {
  438.                 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
  439.                 cell = [topLevelObjects objectAtIndex:0];
  440.             }
  441.            
  442.             NSArray *objects;
  443.             NSInteger rowIndex = 0;
  444.             if (otherSnackTitleRow != -1 && indexPath.row > otherSnackTitleRow) {
  445.                 rowIndex = indexPath.row - otherSnackTitleRow - 1;
  446.                 objects = day.other;
  447.             } else if (afternoonSnackTitleRow != -1 && indexPath.row > afternoonSnackTitleRow) {
  448.                 rowIndex = indexPath.row - afternoonSnackTitleRow - 1;
  449.                 objects = day.afternoonsnack;
  450.             } else {
  451.                 rowIndex = indexPath.row - earlySnackTitleRow - 1;
  452.                 objects = day.earlysnack;
  453.             }
  454.            
  455.             NSObject *food = (NSObject*)[objects objectAtIndex:rowIndex];
  456.            
  457.             if ([food isMemberOfClass:[FoodItem class]]) {
  458.                 UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[((FoodItem*)food) image] ofType:@"png"]];
  459.                 cell.iconView.image = icon;
  460.                 cell.titleLabel.text = [(FoodItem *)food titleAndAmountToString];
  461.                 cell.descriptionLabel.text = [(FoodItem *)food totalCaloriesToString];
  462.             } else {
  463.                 UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fodef" ofType:@"png"]];
  464.                 cell.iconView.image = icon;
  465.                 cell.titleLabel.text = [(MealItem *)food titleAndAmountToString];
  466.                 cell.descriptionLabel.text = [(MealItem *)food totalCaloriesToString];
  467.             }
  468.  
  469.             int row = indexPath.row;
  470.             if(indexPath.row > earlySnackTitleRow && earlySnackTitleRow != -1){
  471.                 row--;
  472.             }
  473.             if(indexPath.row > afternoonSnackTitleRow && afternoonSnackTitleRow != -1){
  474.                 row--;
  475.             }
  476.             if(indexPath.row > otherSnackTitleRow && otherSnackTitleRow != -1){
  477.                 row--;
  478.             }
  479.             CopyItem *item = [snackItems objectAtIndex:row-1];
  480.             if(indexPath.row != 0 && indexPath.row != afternoonSnackTitleRow && indexPath.row != earlySnackTitleRow && indexPath.row != otherSnackTitleRow){
  481.                 if(!item.isChecked){
  482.                     cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone;
  483.                 }else{
  484.                     cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
  485.                 }
  486.             }
  487.            
  488.             return cell;   
  489.         }
  490.     } else {
  491.         NSArray *objects = nil;
  492.         if (indexPath.section == breakfastSection) {
  493.             objects = day.breakfast;
  494.         } else if (indexPath.section == lunchSection) {    
  495.             objects = day.lunch;
  496.         } else if (indexPath.section == dinnerSection) {
  497.             objects = day.dinner;
  498.         }
  499.  
  500.         if (indexPath.row == 0) {
  501.             DiaryTitleCell *cell;
  502.             static NSString *firstCellId = @"FirstDiaryTitleCell";
  503.            
  504.  
  505.                 cell = (DiaryTitleCell*)[self.tableView dequeueReusableCellWithIdentifier:firstCellId];
  506.                 if (cell == nil) {
  507.                     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:firstCellId owner:self options:nil];
  508.                     cell = [topLevelObjects objectAtIndex:0];
  509.                 }
  510.  
  511.             cell.accessoryView = nil;
  512.             if (indexPath.section == breakfastSection) {
  513.                 cell.titleLabel.text = NSLocalizedString(@"Breakfast",nil);
  514.                 cell.descriptionLabel.text = [day breakfastCaloriesToString];
  515.                 UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"breakfast_big" ofType:@"png"]];
  516.                 cell.iconView.image = icon;
  517.             } else if (indexPath.section == lunchSection) {    
  518.                 cell.titleLabel.text = NSLocalizedString(@"Lunch",nil);
  519.                 cell.descriptionLabel.text = [day lunchCaloriesToString];
  520.                 UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lunch_big" ofType:@"png"]];
  521.                 cell.iconView.image = icon;
  522.             } else if (indexPath.section == dinnerSection) {
  523.                 cell.titleLabel.text = NSLocalizedString(@"Dinner",nil);
  524.                 cell.descriptionLabel.text = [day dinnerCaloriesToString];
  525.                 UIImage *icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dinner_big" ofType:@"png"]];
  526.                 cell.iconView.image = icon;
  527.             }
  528.             return cell;
  529.         } else {
  530.             NSString *cellId;
  531.             if(indexPath.row == objects.count){
  532.                 cellId = @"LastImageDetailedCell";
  533.             }else{
  534.                 cellId = @"ImageDetailedCell";
  535.             }
  536.             ImageDetailedCell *cell;
  537.            
  538.            
  539.             cell = (ImageDetailedCell*)[self.tableView dequeueReusableCellWithIdentifier:cellId];
  540.             if (cell == nil) {
  541.                 NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil];
  542.                 cell = [topLevelObjects objectAtIndex:0];
  543.             }
  544.            
  545.             NSObject *food = (NSObject*)[objects objectAtIndex:indexPath.row-1];
  546.            
  547.            
  548.             CopyItem *item;
  549.             if (indexPath.section == breakfastSection) {
  550.                 item = [breakfastItems objectAtIndex:indexPath.row - 1];
  551.             } else if (indexPath.section == lunchSection) {
  552.                 item = [lunchItems objectAtIndex:indexPath.row - 1];
  553.             } else if (indexPath.section == dinnerSection) {
  554.                 item = [dinnerItems objectAtIndex:indexPath.row - 1];
  555.             }
  556.            
  557.             if(indexPath.row != 0){
  558.                 if(!item.isChecked){
  559.                     cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone;
  560.                 }else{
  561.                     cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
  562.                 }
  563.             }
  564.            
  565.             UIImage *icon = nil;
  566.            
  567.             if ([food isMemberOfClass:[FoodItem class]]) {
  568.                 NSString *imageName = [((FoodItem*)food) image];
  569.                 icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
  570.                 cell.titleLabel.text = [(FoodItem *)food titleAndAmountToString];
  571.                 cell.descriptionLabel.text = [(FoodItem *)food totalCaloriesToString];
  572.             } else {
  573.                 icon = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fodef" ofType:@"png"]];
  574.                 cell.titleLabel.text = [(MealItem *)food titleAndAmountToString];
  575.                 cell.descriptionLabel.text = [(MealItem *)food totalCaloriesToString];
  576.             }
  577.            
  578.             cell.iconView.image = icon;
  579.             return cell;
  580.         }
  581.     }
  582. }
  583.  
  584. #pragma mark -
  585. #pragma mark Table view delegate
  586.  
  587. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  588.    
  589.     if(indexPath.row != 0){
  590.         CopyItem *item;
  591.         if (indexPath.section == breakfastSection) {
  592.             item = [breakfastItems objectAtIndex:indexPath.row - 1];
  593.         } else if (indexPath.section == lunchSection) {    
  594.             item = [lunchItems objectAtIndex:indexPath.row - 1];
  595.         } else if (indexPath.section == dinnerSection) {
  596.             item = [dinnerItems objectAtIndex:indexPath.row - 1];
  597.         } else{
  598.             int row = indexPath.row;
  599.             if(indexPath.row > earlySnackTitleRow && earlySnackTitleRow != -1){
  600.                 row--;
  601.             }
  602.             if(indexPath.row > afternoonSnackTitleRow && afternoonSnackTitleRow != -1){
  603.                 row--;
  604.             }
  605.             if(indexPath.row > otherSnackTitleRow && otherSnackTitleRow != -1){
  606.                 row--;
  607.             }
  608.            
  609.             item = [snackItems objectAtIndex:row-1];
  610.         }
  611.        
  612.        
  613.         item.isChecked = !item.isChecked;
  614.  
  615.     }else{
  616.         if (indexPath.section == breakfastSection) {
  617.             [self selectAllItemsInArray:breakfastItems];
  618.         } else if (indexPath.section == lunchSection) {    
  619.             [self selectAllItemsInArray:lunchItems];
  620.         } else if (indexPath.section == dinnerSection) {
  621.             [self selectAllItemsInArray:dinnerItems];
  622.         } else{
  623.             [self selectAllItemsInArray:snackItems];            
  624.         }
  625.     }
  626.    
  627.     [self.tableView reloadData];
  628.    
  629.     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  630. }
  631.  
  632. -(void)selectAllItemsInArray: (NSMutableArray *)array{
  633.     for(CopyItem *item in array){
  634.         if(!item.isChecked){
  635.             for(CopyItem *item2 in array){
  636.                 item2.isChecked = YES;
  637.             }
  638.             return;
  639.         }
  640.     }
  641.    
  642.     for(CopyItem *item in array){
  643.         item.isChecked = NO;
  644.     }
  645. }
  646.  
  647. - (IBAction)calendarPressed:(id)sender {
  648.    
  649.     DiaryListViewController *diaryListViewController = [[DiaryListViewController alloc] initWithNibName:@"DiaryListViewController" bundle:nil];
  650.     diaryListViewController.showFuture = NO;
  651.     diaryListViewController.copyDayViewController = self;
  652.     [diaryListViewController loadData];
  653.    
  654.     UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:diaryListViewController];
  655.     navCon.navigationBar.barStyle = UIBarStyleBlack;
  656.     [diaryListViewController release];
  657.     [self presentModalViewController:navCon animated:YES];
  658.     [navCon release];
  659. }
  660.  
  661. - (IBAction)changeMealtypePressed:(id)sender {
  662.    
  663.     CGRect frame = CGRectMake(0, self.view.bounds.size.height - 216, 320, 216);
  664.    
  665.     if(!changingMealType){
  666.         changingMealType = YES;
  667.        
  668.         [self.view addSubview:pickerView];
  669.        
  670.         [UIView animateWithDuration:0.3
  671.                               delay:0
  672.                             options: UIViewAnimationCurveEaseIn
  673.                          animations:^{
  674.                              pickerView.frame = frame;
  675.                          }
  676.                          completion:^(BOOL finished){
  677.                              
  678.                          }];
  679.        
  680.        
  681.         //[self performSelector:@selector(highlightmealTypeButton) withObject:nil afterDelay:0.0];
  682.         switch (addType) {
  683.             case 1:
  684.                 [self.pickerView selectRow:0 inComponent:0 animated:YES];
  685.                 break;
  686.             case 4:
  687.                 [self.pickerView selectRow:1 inComponent:0 animated:YES];
  688.                 break;
  689.             case 2:
  690.                 [self.pickerView selectRow:2 inComponent:0 animated:YES];
  691.                 break;
  692.             case 5:
  693.                 [self.pickerView selectRow:3 inComponent:0 animated:YES];
  694.                 break;
  695.             case 3:
  696.                 [self.pickerView selectRow:4 inComponent:0 animated:YES];
  697.                 break;
  698.             case 6:
  699.                 [self.pickerView selectRow:5 inComponent:0 animated:YES];
  700.                 break;
  701.                
  702.             default:
  703.                 break;
  704.         }
  705.        
  706.     }else{
  707.         changingMealType = NO;
  708.         frame.origin.y += 216;
  709.  
  710.         [UIView animateWithDuration:0.3
  711.                               delay:0
  712.                             options: UIViewAnimationCurveEaseIn
  713.                          animations:^{
  714.                              pickerView.frame = frame;
  715.                          }
  716.                          completion:^(BOOL finished){
  717.                              [pickerView removeFromSuperview];
  718.                          }];
  719.        
  720.        
  721.         //self.mealTypeButton.highlighted = NO;
  722.     }
  723.    
  724. }
  725.  
  726.  
  727.  
  728. #pragma mark -
  729. #pragma mark Picker view
  730.  
  731. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  732.     return 1;
  733. }
  734. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  735.     return 6;
  736. }
  737. -(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  738.     if(view){
  739.         return view;
  740.     }
  741.     else{
  742.         UIView *cell = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 40)];
  743.         UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(65, 5, 200, 30)];
  744.         title.backgroundColor = [UIColor clearColor];
  745.         UIImageView *cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 3, 34, 34)];
  746.        
  747.         switch (row) {
  748.             case 0:
  749.                 title.text = NSLocalizedString(@"Breakfast",nil);
  750.                 cellImage.image = [UIImage imageNamed:@"breakfast.png"];
  751.                 break;
  752.             case 1:
  753.                 title.text = NSLocalizedString(@"Early Snack",nil);
  754.                 cellImage.image = [UIImage imageNamed:@"snack.png"];
  755.                 break;
  756.             case 2:
  757.                 title.text = NSLocalizedString(@"Lunch",nil);
  758.                 cellImage.image = [UIImage imageNamed:@"lunch.png"];
  759.                 break;
  760.             case 3:
  761.                 title.text = NSLocalizedString(@"Afternoon Snack",nil);
  762.                 cellImage.image = [UIImage imageNamed:@"snack.png"];
  763.                 break;
  764.             case 4:
  765.                 title.text = NSLocalizedString(@"Dinner",nil);
  766.                 cellImage.image = [UIImage imageNamed:@"dinner.png"];
  767.                 break;
  768.             case 5:
  769.                 title.text = NSLocalizedString(@"Other",nil);
  770.                 cellImage.image = [UIImage imageNamed:@"snack.png"];
  771.                 break;
  772.                
  773.             default:
  774.                 title.text = @"<Mealtype>";
  775.                 cellImage.image = [UIImage imageNamed:@"breakfast.png"];
  776.                 break;
  777.         }
  778.         [cell addSubview:title];
  779.         [cell addSubview:cellImage];
  780.         return cell;
  781.     };
  782.    
  783.    
  784.    
  785. }
  786.  
  787.  
  788. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  789.     switch (row) {
  790.         case 0:
  791.             [self changeMealTypeTitle:1];
  792.             break;
  793.         case 1:
  794.             [self changeMealTypeTitle:4];
  795.             break;
  796.         case 2:
  797.             [self changeMealTypeTitle:2];
  798.             break;
  799.         case 3:
  800.             [self changeMealTypeTitle:5];
  801.             break;
  802.         case 4:
  803.             [self changeMealTypeTitle:3];
  804.             break;
  805.         case 5:
  806.             [self changeMealTypeTitle:6];
  807.             break;
  808.            
  809.         default:
  810.             break;
  811.     }
  812.     if(changingMealType){
  813.         changingMealType = false;
  814.        
  815.         [UIView animateWithDuration:0.3
  816.                               delay:0
  817.                             options: UIViewAnimationCurveEaseIn
  818.                          animations:^{
  819.                              self.pickerView.frame = CGRectMake(0, self.view.bounds.size.height, 320, 216);
  820.                          }
  821.                          completion:^(BOOL finished){
  822.                              [self.pickerView removeFromSuperview];
  823.                          }];
  824.     }
  825. }
  826.  
  827. -(void)changeMealTypeTitle: (int) type{
  828.     addType = type;
  829.     [self createAddTypeString];
  830.     self.title = [NSString stringWithFormat:NSLocalizedString(@"Copy %@", @"Copy breakfast"), addTypeString];
  831. }
  832.  
  833. -(void)createAddTypeString {
  834.     if (addType == 1) {
  835.         self.addTypeString = NSLocalizedString(@"Breakfast",nil);
  836.     }
  837.     else if (addType == 2) {
  838.         self.addTypeString = NSLocalizedString(@"Lunch",nil);
  839.     }
  840.     else if (addType == 3) {
  841.         self.addTypeString = NSLocalizedString(@"Dinner",nil);
  842.     }
  843.     else if (addType == 4) {
  844.         self.addTypeString = NSLocalizedString(@"Early Snack",nil);
  845.     }
  846.     else if (addType == 5) {
  847.         self.addTypeString = NSLocalizedString(@"Afternoon Snack",nil);
  848.     }
  849.     else if (addType == 6) {
  850.         self.addTypeString = NSLocalizedString(@"Other",nil);
  851.     }
  852.     else {
  853.         self.addTypeString = @"";
  854.     }
  855.    
  856. }
  857.  
  858.  
  859.  
  860. #pragma mark -
  861. #pragma mark Memory management
  862.  
  863. - (void)didReceiveMemoryWarning {
  864.     // Releases the view if it doesn't have a superview.
  865.     [super didReceiveMemoryWarning];
  866.    
  867.     // Relinquish ownership any cached data, images, etc that aren't in use.
  868. }
  869.  
  870. - (void)viewDidUnload {
  871.     [self setMealTypeButton:nil];
  872.     [self setChangeMealTypeButton:nil];
  873.     [self setToolbar:nil];
  874.     [self setTableView:nil];
  875.     [self setKeyboardToolbar:nil];
  876.     [self setPickerView:nil];
  877.     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
  878.     // For example: self.myOutlet = nil;
  879. }
  880.  
  881.  
  882. - (void)dealloc {
  883.     [addTypeString release];
  884.     [keyboardToolbar release];
  885.     [tableView release];
  886.     [toolbar release];
  887.     [changeMealTypeButton release];
  888.     [pickerView release];
  889.     [mealTypeButton release];
  890.     [infoLabel release];
  891.     [super dealloc];
  892. }
  893.  
  894.  
  895. @end
Add Comment
Please, Sign In to add comment