Today's Menu  Portugal
journal and plan nutrition
Z6MenuPanoramaView.m
Go to the documentation of this file.
1 //
2 // Z6MenuPanoramaView.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 3/13/17.
6 // Copyright © 2017 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import "Z6MenuPanoramaView.h"
10 #import "Z5DataController.h"
13 
14 @interface Z6MenuPanoramaView ()
15 @property (nonatomic, strong) NSDateFormatter *mDateFormatter;
16 @property (nonatomic, strong) UIButton *dateButton;
17 @end
18 
19 @implementation Z6MenuPanoramaView
20 
21 id<Z6MenuPanoramaDelegate> menuPanoramaDelegate;
22 
23 UICollectionViewFlowLayout *flowLayout = nil;
24 UICollectionView *collectionView;
27 NSInteger selectedRow;
28 UIColor *backgroundShade;
29 static NSString *cellIdentifier = @"y4Cell";
30 static NSString *selectedCellIdentifier = @"y4SelectedCell";
31 
32 - (instancetype) initWithFrame:(CGRect)frame andDelegate:(id<Z6MenuPanoramaDelegate>)dele
33 {
34  self = [super initWithFrame:frame];
35  menuPanoramaDelegate = dele;
36  backgroundShade = [UIColor colorWithRed:226.0/256 green:201.0/256 blue:114.0/256 alpha:1.0];
37  // backgroundShade = [[Z5DataController alloc] wheatColor];
38 
39  [self initialize];
40  return self;
41 }
42 
43 - (void) initialize
44 {
45  flowLayout = [[UICollectionViewFlowLayout alloc] init];
46  [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
47 
48  _mDateFormatter = [[NSDateFormatter alloc] init];
49  [_mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
50  [_mDateFormatter setTimeStyle:NSDateFormatterShortStyle];
51 
52  // start definition of collection view layout - class var, used many places
53  float defaultCellwidth = 174;
54  float defaultCellheight = 218;
55  defaultCellSize = CGSizeMake(defaultCellwidth, defaultCellheight);
56  float selectCellWidth = 256;
57  float selectCellHeight = 280;
58  selectCellSize = CGSizeMake(selectCellWidth, selectCellHeight);
59  // end, for now - start definition of collection view layout
60 
61  // float initialCollectViewHgt = selectCellHeight + 18;
62  float initialCollectViewHgt = selectCellHeight + 6;
63  CGRect collectionViewFrame = CGRectMake(10, 36, self.bounds.size.width-12, initialCollectViewHgt);
64  collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout];
65  [collectionView setDelegate:self];
66  [collectionView setDataSource:self];
67  // [collectionView setBackgroundColor:[UIColor lightGrayColor]];
68  [collectionView setBackgroundColor:[[Z5DataController alloc] wheatColor]];
69  [self addSubview:collectionView];
70 
71  // add a button annotated with today's date, And added it to the view
72  CGRect dateFrame = CGRectMake(60.0, 4.0, 240.0, 28.0);
73  self.dateButton = [[UIButton alloc] initWithFrame:dateFrame];
74  [self.dateButton addTarget:self action:@selector(clickClearDay) forControlEvents:UIControlEventTouchUpInside];
75  [self.dateButton setOpaque:YES];
76  [self.dateButton setBackgroundColor:[UIColor cyanColor]];
77  [self addSubview:self.dateButton];
78  // end - add a button annotated with today's date, And added it to the view
79 
80  // setup how cells are built
81  [collectionView registerClass:[Y4NutritionCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
82  [collectionView registerClass:[Y4NutritionCollectSelectViewCell class] forCellWithReuseIdentifier:selectedCellIdentifier];
83  collectionView.delegate = self;
84  // end - setup how cells are built
85 
86  // initialize class var with all menu items for (for display by the gallery?)
87  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
88  NSNumber *galleryChoice = [NSNumber numberWithInt:1];
89  NSArray *tmp = [localData retrieveAllMenuItems:galleryChoice];
90  NSLog(@"Z6MenuPanoramaView.initialize: count of all menu items in d/b: %ld", (unsigned long)[tmp count]);
91  [self setMenuItems:[[NSMutableArray alloc] initWithArray:tmp]];
92  // end - initialize class var with all menu items for (for display by the gallery?)
93 
94  if (0 < [_menuItems count]) {
95  NSUInteger selectindex;
96  if ([menuPanoramaDelegate getSelectedGalleryRow: (NSUInteger *) &selectindex]) {
97  MenuItem *selectedItem = [_menuItems objectAtIndex:selectindex];
98  [self setFocusMenuItem:selectedItem];
99  NSLog(@"Z6MenuPanoramaView.initialize: selectedRow: %ld", (long)selectedRow);
100  [self centerGalleryItem:selectedRow];
101  } else {
102  selectedRow = -1;
103  }
104  }
105 
106  [self setCurrentDate];
107 
108  [self setBackgroundColor:backgroundShade];
109 }
110 
111 - (void) appear:(CGRect)bound
112 {
113  NSNumber *galleryChoice = [NSNumber numberWithInt:1];
114  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
115  NSArray *tmp = [localData retrieveAllMenuItems:galleryChoice];
116  // NSLog(@"Z6MenuPanoramaView.appear: count of all menu items in d/b: %ld", (unsigned long)[tmp count]);
117  [self setMenuItems:[[NSMutableArray alloc] initWithArray:tmp]];
118  [self reload];
119 
120  CGRect collectionViewFrame = CGRectMake(10, 36, self.bounds.size.width-20, [self collectionViewHeight]);
121  [collectionView setFrame:collectionViewFrame];
122  if (0 <= selectedRow) {
123  [self centerGalleryItem:selectedRow];
124  }
125 }
126 
127 - (void) reload
128 {
129  NSLog(@"MenuPanorama.reload: selected row: %ld", (long)selectedRow);
130  [collectionView reloadData];
131  [collectionView setNeedsDisplay];
132  [self setCurrentDate];
133 }
134 
136 {
137  NSString *todayDate = [_mDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
138  [Z5DataController labelizeButton:self.dateButton withText:todayDate];
139 }
141 {
142  float portalHeight = selectCellSize.height
143  + [collectionView layoutMargins].top
144  + [collectionView layoutMargins].bottom
145  + [collectionView contentInset].top
146  + [collectionView contentInset].bottom + 8;
147 
148 
149  NSLog(@"Z6MenuPanoramaView.collectionViewHeight: portalHeight: %3.2f", portalHeight);
150  return portalHeight;
151 }
152 
153 #pragma mark - <Z6ContainedView>
155 {
156  // dateButton does not show up in Menu-Record
157  float dateNoticeVert = self.dateButton.bounds.size.height;
158 
159  float portalHeight = [self collectionViewHeight] + dateNoticeVert + 6;
160 
161  // portalHeight += 100.0; // this stretches gray portion (headerView.collectionView) inside brown area (headerView)
162 
163  if (selectedRow > 0) {
164  portalHeight += 16;
165  }
166 
167  // NSLog(@"MenuPanorama.heightRequirement: dateNoticeVert: %3.2f portalHeight: %3.2f",
168  // dateNoticeVert, portalHeight);
169 
170  return portalHeight;
171 }
172 
173 #pragma mark - <UICollectionViewDelegateFlowLayout>
174 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
175  layout:(UICollectionViewLayout*)collectionViewLayout
176  insetForSectionAtIndex:(NSInteger)section {
177 
178  // NSLog(@"Z6MenuPanoramaView.insetForSectionAtIndex: %ld", (long)section);
179 
180  UIEdgeInsets ret = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
181  return ret;
182 }
183 
184 #pragma mark - <UICollectionViewDataSource>
185 
186 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
187 
188  NSInteger ret = 1;
189 
190  return ret;
191 }
192 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
193 
194  NSInteger ret = [[self menuItems] count];
195  // NSLog(@"Z6MenuPanoramaView.numberOfItemsInSection: menuItem count: %ld", ret);
196 
197  return ret;
198 }
199 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
200 
201  int imageHeight = 120; //
202  int selectedImageHeight = 160;
203  int outputImageHeight = 0;
204 
205  // UICollectionViewFlowLayout *viewLayout = collectionView.collectionViewLayout;
206  // UICollectionView *colView = collectionView;
207 
208 
210  if (selectedRow == indexPath.row) {
211  cell = (Y4NutritionCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:selectedCellIdentifier forIndexPath:indexPath];
212  outputImageHeight = selectedImageHeight;
213  } else {
214  cell = (Y4NutritionCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
215  outputImageHeight = imageHeight;
216  }
217  // NSLog(@"NutritionCollectController.cellForItemAtIndexPath: tag: %ld", [[cell textLabel] tag]);
218 
219  MenuItem *cellItem = [[self menuItems] objectAtIndex:indexPath.row];
220  UIImage *photoImage = [UIImage imageWithData:cellItem.photo];
221  NSString *itemName = [cellItem name];
222  if (!itemName) {
223  itemName = @"NameLess";
224  }
225 
226  CGSize photoSize = [photoImage size];
227  float resizeRatio = photoSize.height / (float)outputImageHeight;
228  UIImage *keptImage = [UIImage imageWithCGImage:[photoImage CGImage]
229  scale:photoImage.scale * resizeRatio
230  orientation:(photoImage.imageOrientation) ];
231 
232  // NSString *rkey = [Z5DataController produceRandomkey];
233  [cell.imageView setImage:keptImage];
234  [Z5DataController labelize:cell.textLabel withText:itemName];
235 
236  return cell;
237 }
238 
239 
240 #pragma mark - <UICollectionViewDelegate>
241 
246 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
247 {
248  if (selectedRow == indexPath.row) {
249  [self clickClearDay];
250  } else {
251  selectedRow = indexPath.row;
252 
253  NSLog(@"Z6MenuPanoramaView.didSelectItem: selectedRow: %ld", (long)selectedRow);
254 
255  NSUInteger tCount = [[self menuItems] count];
256  if (tCount > [indexPath row]) {
257 
258  MenuItem *selectedItem = [[self menuItems] objectAtIndex:[indexPath row]];
259  [self setFocusMenuItem:selectedItem];
260  [menuPanoramaDelegate clickedMenuItem:selectedItem atRow:selectedRow];
261  }
262  [self reload];
263  }
264  [self setNeedsDisplay];
265  [self centerGalleryItem:[indexPath row]];
266 }
267 - (void) centerGalleryItem:(NSUInteger)indexRow
268 {
269  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexRow inSection:0];
270  [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
271 }
275 - (CGSize)collectionView:(UICollectionView *)collectionView
276  layout:(UICollectionViewLayout *)collectionViewLayout
277  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
278 {
279  CGSize ret;
280 
281  if (indexPath.row == selectedRow) {
282  ret = selectCellSize;
283  } else {
284  ret = defaultCellSize;
285  }
286  return ret;
287 }
288 
297 - (NSMutableArray *)redetermineMenuItems
298 {
299  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
300 
301  NSNumber *galleryChoice = [NSNumber numberWithInt:1];
302  NSArray *menuItemsNow = [localData retrieveAllMenuItems:galleryChoice];
303  Boolean dbMenuItemFoundInGalllery = NO;
304  for (MenuItem *dbMenuItem in menuItemsNow) {
305  NSString *miNowRandomkey = dbMenuItem.randomkey;
306  for (MenuItem *galleryMenuItem in [self menuItems]) {
307  // NSLog(@"NutritionCollectionView.determineMenuItems: scan-in key: %@ alreadykey: %@", miNowRandomkey, galleryMenuItem.randomkey);
308 
309  if ([miNowRandomkey isEqualToString:galleryMenuItem.randomkey]) {
310  /*
311  * this block is encountered when a database menu item is found in the gallery menu items.
312  * a flag will be set indicating item is not to be added to gallery display,
313  *
314  */
315  dbMenuItemFoundInGalllery = YES;
316  break;
317  }
318  }
319  if (!dbMenuItemFoundInGalllery) {
320  /*
321  * this block is encountered when a database menu item is not found in the gallery menu items.
322  * Here it is added at the beginning of the gallery.
323  */
324  dbMenuItem.galleryPositionNo = [NSNumber numberWithLong:[localData getPrevGalleryPositionIdx]];
325  [[self menuItems] addObject:dbMenuItem];
326  }
327  }
328 
329  for (MenuItem *mi in [self menuItems]) {
330  NSLog(@"NutritionCollectionView.redetermineMenuItems: positionInGallery: %@", [mi galleryPositionNo]);
331  }
332  return [self menuItems];
333 }
334 
335 #pragma mark - JSON
336 
340 -(NSArray *)menuParameterArray: (NSString *)jsonString
341 {
342  NSLog(@"Z6MenuPanoramaView.menuParameterArray: jsonString: %@", jsonString);
343  NSData *jsonReturnData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
344  NSError *err;
345  if (jsonReturnData) {
346  NSObject *jsonSerialized = [NSJSONSerialization JSONObjectWithData:jsonReturnData options:NSJSONReadingAllowFragments error:&err];
347  if (jsonSerialized) {
348  NSString *jsonSerializedClass = NSStringFromClass([jsonSerialized class]);
349  NSLog(@"Z6MenuPanoramaView.menuParameterArray: valid data, top class: %@", jsonSerializedClass);
350  if (([jsonSerializedClass isEqualToString:@"__NSCFDictionary"])
351  || ([jsonSerializedClass isEqualToString:@"__NSSingleEntryDictionaryI"])) {
352  NSDictionary *jsonSerializedDict = (NSDictionary *)jsonSerialized;
353  NSObject *uNums = [jsonSerializedDict objectForKey:@"usdaNumbers"];
354  NSString *uNumsClass = NSStringFromClass([uNums class]);
355  NSLog(@"Z6MenuPanoramaView.menuParameterArray: uNumsClass: %@", uNumsClass);
356  NSArray *jsonSerializedArray = (NSArray *)uNums;
357  return jsonSerializedArray;
358  } else {
359  return nil;
360  }
361  } else {
362  return nil;
363  }
364  } else {
365  return nil;
366  }
367 }
368 
369 #pragma mark button action
370 
372 {
373  NSLog(@"Z6MenuPanoramaView.clickClearDay");
374  self.focusMenuItem = nil;
375  selectedRow = -1;
376 
377  [menuPanoramaDelegate clickClearDay];
378  [self reload];
379 }
380 
381 /*
382 // Only override drawRect: if you perform custom drawing.
383 // An empty implementation adversely affects performance during animation.
384 - (void)drawRect:(CGRect)rect {
385  // Drawing code
386 }
387 */
388 
389 @end
NSInteger selectedRow
NSArray< MenuItem * > * retrieveAllMenuItems:(NSNumber *galleryNo)
CGSize selectCellSize
instancetype sharedInstance()
void centerGalleryItem:(NSUInteger indexRow)
UIColor * backgroundShade
UICollectionViewFlowLayout * flowLayout
NSDateFormatter * mDateFormatter
float initialCollectViewHgt
UIButton * dateButton
static NSString * cellIdentifier
NSMutableArray< MenuItem * > * menuItems
id< Z6MenuPanoramaDelegate > menuPanoramaDelegate
Singleton interface to both core and remote data sources.
NSMutableArray * redetermineMenuItems()
static NSString * selectedCellIdentifier
void labelizeButton:withText:(UIButton *targButton, [withText] NSString *txt)
CGSize defaultCellSize
void labelize:withText:(UILabel *targLabel, [withText] NSString *txt)
UICollectionView * collectionView