Today's Menu  Portugal
journal and plan nutrition
U5MealPanoramaView.m
Go to the documentation of this file.
1 //
2 // U5MealPanoramaView.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 1/1/19.
6 // Copyright © 2019 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import "U5MealPanoramaView.h"
10 #import "Z5DataController.h"
12 
13 @implementation U5MealPanoramaView
14 
15 id<Z6MealPanoramaDelegate> mealPanoramaDelegate;
16 UICollectionView *mealCollectionView;
17 
18 UICollectionViewFlowLayout *mealFlowLayout = nil;
19 
23 NSInteger selectedMealRow;
25 static NSString *cellIdentifier = @"y5Cell";
26 static NSString *selectedCellIdentifier = @"y5SelectedCell";
27 NSDateFormatter *mDateFormatter;
28 
29 + (NSValue *) mealCellDefaultSize
30 {
31  NSValue *ret = [NSValue valueWithCGSize:mealDefaultCellSize];
32  return ret;
33 }
34 
35 - (instancetype) initWithFrame:(CGRect)frame andDelegate:(id<Z6MealPanoramaDelegate>)dele
36 {
37  self = [super initWithFrame:frame];
38  mealPanoramaDelegate = dele;
39  mealBackgroundShade = [UIColor colorWithRed:226.0/256 green:201.0/256 blue:114.0/256 alpha:1.0];
40  selectedMealRow = 1001;
41  [self initialize];
42  return self;
43 }
44 
45 
46 - (void) initialize
47 {
48  // start definition of collection view layout - class var, used many places
49  float defaultCellwidth = 224;
50  float defaultCellheight = 268;
51  mealDefaultCellSize = CGSizeMake(defaultCellwidth, defaultCellheight);
52  float selectCellWidth = 156;
53  float selectCellHeight = 180;
54  mealSelectCellSize = CGSizeMake(selectCellWidth, selectCellHeight);
55  // end, for now - start definition of collection view layout
56 
57  mealFlowLayout = [[UICollectionViewFlowLayout alloc] init];
58  [mealFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
59  [mealFlowLayout setMinimumInteritemSpacing:1.0];
60  [mealFlowLayout setItemSize:mealDefaultCellSize];
61 
62  initialCollectViewHgt = defaultCellheight + 6;
63  CGRect collectionViewFrame = CGRectMake(4.0, 4.0, self.bounds.size.width-8, initialCollectViewHgt);
64  mealCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:mealFlowLayout];
65  [self addSubview:mealCollectionView];
66  [self registerCells];
67  [mealCollectionView setDelegate:self];
68  [mealCollectionView setDataSource:self];
69  // [collectionView setBackgroundColor:[UIColor lightGrayColor]];
70  [mealCollectionView setBackgroundColor:[[Z5DataController alloc] wheatColor]];
71 
72  mDateFormatter = [[NSDateFormatter alloc] init];
73  [mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
74  [mDateFormatter setTimeStyle:NSDateFormatterShortStyle];
75 }
76 
77 - (void) reloadMeals
78 {
79  [mealCollectionView reloadData];
80 }
81 
82 - (void) registerCells
83 {
84 // setup how cells are built
85 [mealCollectionView registerClass:[U5MealCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
86 [mealCollectionView registerClass:[U5MealCollectionViewCell class] forCellWithReuseIdentifier:selectedCellIdentifier];
87 mealCollectionView.delegate = self;
88 // end - setup how cells are built
89 }
90 
91 #pragma mark - Z6ContainedView
92 
94 {
95  float ret = initialCollectViewHgt;
96  return ret;
97 }
98 - (void) appear:(CGRect)bound
99 {
100  NSNumber *galleryChoice = [NSNumber numberWithInt:1];
101  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
102  // NSArray *tmp = [localData retrieveAllMenuItems:galleryChoice];
103  NSArray *tmp = [localData retrieveAllMeals:galleryChoice];
104  NSLog(@"U5MenuPanoramaView.appear: width: %3.2f self-width: %3.2f", bound.size.width, self.bounds.size.width);
105  [self setSnapMeals:[[NSMutableArray alloc] initWithArray:tmp]];
106 
107 // CGRect collectionViewFrame = CGRectMake(10, 36, self.bounds.size.width-20, mealDefaultCellSize.height+12.0);
108 // [mealCollectionView setFrame:collectionViewFrame];
109  [mealCollectionView setBackgroundColor:[UIColor yellowColor]];
110  [mealCollectionView reloadData];
111 // if (0 <= selectedRow) {
112 // [self centerGalleryItem:selectedRow];
113 // }
114 }
115 
116 #pragma mark - UICollectionViewDataSource
117 - (NSInteger)collectionView:(UICollectionView *)collectionView
118  numberOfItemsInSection:(NSInteger)section
119 {
120  NSInteger ret = [_snapMeals count];
121  NSLog(@"U5MealPanoramaView.numberOfItemsInSection: %ld", (long)ret);
122  return ret;
123 }
124 - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
125  cellForItemAtIndexPath:(NSIndexPath *)indexPath
126 {
127  int imageHeight = 120; //
128  int selectedImageHeight = 160;
129  int outputImageHeight = 0;
130 
132  if (selectedMealRow == indexPath.row) {
133  cell = (U5MealCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:selectedCellIdentifier forIndexPath:indexPath];
134  outputImageHeight = selectedImageHeight;
135  } else {
136  cell = (U5MealCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
137  outputImageHeight = imageHeight;
138  }
139  // NSLog(@"U5MealPanoramaView.cellForItemAtIndexPath: cell: %@", cell);
140 
141  Meal *cellItem = [_snapMeals objectAtIndex:indexPath.row];
142  UIImage *photoImage = [UIImage imageWithData:cellItem.photo];
143  NSDate *photoDate = [cellItem consumeDate];
144  NSString *itemName = [mDateFormatter stringFromDate:photoDate];
145  // NSString *itemName = @"Meal"; // [cellItem name];
146 
147  CGSize photoSize = [photoImage size];
148  float resizeRatio = photoSize.height / (float)outputImageHeight;
149  UIImage *keptImage = [UIImage imageWithCGImage:[photoImage CGImage]
150  scale:photoImage.scale * resizeRatio
151  orientation:(photoImage.imageOrientation) ];
152 
153  [cell.imageView setImage:keptImage];
154  [Z5DataController labelize:cell.textLabel withText:itemName];
155 
156  return cell;
157 }
158 /*
159 // Only override drawRect: if you perform custom drawing.
160 // An empty implementation adversely affects performance during animation.
161 - (void)drawRect:(CGRect)rect {
162  // Drawing code
163 }
164 */
165 
166 @end
NSInteger selectedMealRow
UICollectionView * mealCollectionView
id< Z6MealPanoramaDelegate > mealPanoramaDelegate
NSValue * mealCellDefaultSize()
UICollectionViewFlowLayout * mealFlowLayout
CGSize mealDefaultCellSize
instancetype sharedInstance()
NSArray< Meal * > * retrieveAllMeals:(NSNumber *galleryNumber)
CGSize mealSelectCellSize
UIColor * mealBackgroundShade
float initialCollectViewHgt
NSDateFormatter * mDateFormatter
NSDate * consumeDate
static NSString * selectedCellIdentifier
Singleton interface to both core and remote data sources.
void labelize:withText:(UILabel *targLabel, [withText] NSString *txt)
static NSString * cellIdentifier
UICollectionView * collectionView