Today's Menu  Portugal
journal and plan nutrition
Y4NutritionViewController.m
Go to the documentation of this file.
1 //
2 // Y4NutritionViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 4/19/16.
6 // Copyright © 2016 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import <HealthKit/HealthKit.h>
12 #import "Z5DataController.h"
18 #import "Z6MenuPanoramaView.h"
19 #import "Z6MenuItemControlbar.h"
20 #import "Z3DateTimeFocusView.h"
21 #import "Z3MenuApprovalView.h"
22 #import "Z6ContainedView.h"
23 #import "Z1ContainedLabel.h"
25 
26 @interface Y4NutritionViewController ()
27 @end
28 
260 @implementation Y4NutritionViewController
261 {
262  NSMutableArray<NSString *> *rows; // an array of randomkeys
265  NSIndexPath *swipedMenuIndexPath;
266 
268  NSUInteger galleryFocusRow;
271 
272  UIView<Z6ContainedView> *menuPanoramaView;
274  Z3DateTimeFocusView *dateTimeView; // need .labelDate
275  UIView<Z6ContainedView> *menuApproveView;
277 
279 
280 
282 UICollectionViewCell *currentlySelectedCell;
283 NSMutableArray *focusUSDAparameterArray;
284 
287  UIImageView *backgrdImageView;
288 }
289 
293 - (void)viewDidLoad {
294  [super viewDidLoad];
295  // Do any additional setup after loading the view from its nib.
296  self.title = @"Menu Choices";
297  nutritionMenuPortraitBackgroundName = @"cheeses2port.png";
298  nutritionMenuLandscapeBackgroundName = @"cheeses2land.png";
299 
300  // create sub-units that do not require frame
301  ;
302 
303  // create sub-units that require frame spec
304  [self orientLoadUI];
305 
306  // sub-unit connects
307  [self.view addSubview:parameterTableView];
308 
309  [self orientAppearUI];
310 
311  galleryFocusSet = NO;
312  [self initHK];
313 
314  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
315  [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
316 
317  }
318 
319 -(void) viewDidAppear:(BOOL)animated
320 {
321  [self clearGallery];
322 
323  [self updateParameterList];
324 
325  NSArray<NSDictionary *> *nutritionComponents = focusUSDAparameterArray;
326  [parameterTableView update:nutritionComponents];
327 
328  [self focusEditControls];
329 
330  // NSString *todayDate = [mDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
331 
332  [menuPanoramaView appear:self.view.bounds];
333  [dateTimeView setLabelCaption:@"the menu today"];
334  [dateTimeView appear:self.view.bounds];
335  [menuApproveView appear:self.view.bounds];
336  [menuApprovalInstructions appear:self.view.bounds];
337 
338  [self reposition];
339 
340  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
341  NSArray *todays = [localData todaysMenuItems];
342  NSLog(@"Y4NutritionViewController.viewDidAppear: todays count: %ld", (unsigned long)[todays count]);
343  [parameterTableView updateMenu:todays];
344 }
345 
346 - (void) reposition
347 {
348  // Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
349 
350  CGRect totalFrame = self.view.frame;
351 
352  float verticalPos = navbarBottomEdge + 2.0;
353 
354  // NSLog(@"MutritionViewController.reposition begin");
355  float verticalHeight = [parameterTableView getViewHeightReqmt];
356  float maxVertical = totalFrame.size.height - verticalPos;
357  if (verticalHeight > maxVertical) {
358  verticalHeight = maxVertical - 4.0;
359  }
360 
361  CGRect tableViewFrame = CGRectMake(2.0, verticalPos, totalFrame.size.width - 4.0, verticalHeight);
362  // NSLog(@"MutritionViewController.reposition end: verticalHeight: %3.2f", verticalHeight);
363 
364  [parameterTableView setFrame:tableViewFrame];
365 
366 }
367 
368 - (void) viewWillDisappear:(BOOL)animated
369 {
370  // NSLog(@"Y4NutritionViewController.viewWillDisappear");
371  [[NSNotificationCenter defaultCenter] removeObserver: self];
372  [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
373 }
377 - (void) initHK
378 {
379  Boolean ret = [HKHealthStore isHealthDataAvailable];
380  if (ret) {
381  NSLog(@"Y4NutritionViewController.initHK: HKHealthStore is available");
382  [[[Z5DataController sharedInstance] localDataController] setupHealthKit];
383  }
384 }
385 // -- was in viewDidLoad
386 // [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
387 // [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
391 - (void)deviceOrientationDidChange:(NSNotification *)notification {
392 
393  [self orientAppearUI];
394 }
395 
396 - (CGRect) screenBound
397 {
398  return self.view.bounds;
399 }
400 #pragma mark - ZOrientProtocol
401 
402 - (void) orientLoadUI
403 {
404 
405  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
406  switch (orientation)
407  {
408  case UIDeviceOrientationPortrait:
409  case UIDeviceOrientationFaceUp:
410  {
411  [self positionFramesPortrait];
412  [self viewDidLoadPortrait];
413  }
414  break;
415  case UIDeviceOrientationLandscapeLeft:
416  case UIDeviceOrientationLandscapeRight:
417  {
419  [self viewDidLoadLandscape];
420  }
421  break;
422  default:
423  {
424  ;
425  }
426  break;
427  }
428 }
430 {
431  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
432  switch (orientation)
433  {
434  case UIDeviceOrientationPortrait:
435  case UIDeviceOrientationFaceUp:
436  {
437  [self positionFramesPortrait];
438  [self viewDidAppearPortrait];
439  }
440  break;
441  case UIDeviceOrientationLandscapeLeft:
442  case UIDeviceOrientationLandscapeRight:
443  {
445  [self viewDidAppearLandscape];
446  }
447  break;
448  default:
449  {
450  ;
451  }
452  break;
453  }
454 }
455 
459 //- (void) orientUI {
460 //
461 // CGRect totalFrame = self.view.bounds;
462 // // NSLog(@"nutritionCollectionView.orientUI: totalFrame width: %3.2f", totalFrame);
463 // float portalHeight = 0.0;
464 // float calculatedPortalHeight = 0.0;
465 //
466 // // NSLog(@"nutritionCollectionView.orientUI: content-size height: %3.2f", self.collectionView.contentSize.height);
467 // float cHeight = 0; // headerView.bounds.size.height;
468 // float yNext = 0.0;
469 //
470 // UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
471 // switch (orientation)
472 // {
473 // case UIDeviceOrientationPortrait:
474 // case UIDeviceOrientationFaceUp:
475 // {
476 // // NSLog(@"nutritionCollectionView.orientUI: UIDeviceOrientationPortrait/FaceUp");
477 // float controlWidth = totalFrame.size.width;
478 // NSLog(@"Y4NutritionViewController.orientUI: totalFrame width: %3.2f", controlWidth);
479 // float topMargin = 48;
480 // portalHeight = 0; // headerView.bounds.size.height;
481 // if (0.0 == cHeight) {
482 // calculatedPortalHeight = portalHeight;
483 // portalHeight = 290.0;
484 // }
485 //
486 // yNext = topMargin + portalHeight;
487 // }
488 // break;
489 // case UIDeviceOrientationLandscapeLeft:
490 // case UIDeviceOrientationLandscapeRight:
491 // {
492 // // float leftMargin = 0.04*totalFrame.size.width;
493 // // float topMargin = 18;
494 //
495 // portalHeight = 180.0;
496 // if (0.0 == cHeight) {
497 // calculatedPortalHeight = portalHeight;
498 // portalHeight = 290.0;
499 // }
500 //
501 // // float controlWidth = 0.92*totalFrame.size.width;
502 // }
503 // break;
504 // default:
505 // {
506 // NSLog(@"nutritionCollectionView.orientUI: other: %ld", (long)orientation );
507 //
508 // }
509 // } // END - switch
510 //}
511 
512 
513 
515 {
516  CGSize viewSize = self.view.frame.size;
517  UIImage *resizedImage = [Z5DataController imageNamed:nutritionMenuPortraitBackgroundName sizedToPortrait:viewSize];
518  backgrdImageView = [[UIImageView alloc] initWithImage:resizedImage];
519  [backgrdImageView setContentMode:UIViewContentModeScaleAspectFill];
520  [self.view addSubview:backgrdImageView];
521 
522  navbarBottomEdge = self.navigationController.navigationBar.frame.origin.y
523  + self.navigationController.navigationBar.frame.size.height;
524 
525 
526  CGRect totalFrame = self.view.frame;
527 
528  float tableWidth = self.view.bounds.size.width;
529  CGRect headFrame = CGRectMake(4.0, 0.0, tableWidth - 12.0, 90.0); // @TODO ??
530 
531  /* first section header/tailer views */
532  NSNumber *galleryOne = [NSNumber numberWithInt:1];
533  NSArray *dataMenuItems = [self fetchMenuChoicesGalleryNum:galleryOne];
534  NSInteger menuItemCount = [dataMenuItems count];
535  NSLog(@"Y4NutritionViewController.viewDidLoad: count of all menu items in d/b: %ld", (unsigned long)menuItemCount);
536  if (0 < menuItemCount) {
537  Z6MenuPanoramaView *tableSpecial = [[Z6MenuPanoramaView alloc] initWithFrame:headFrame andDelegate:self];
538  float headerVert = [tableSpecial heightRequirement];
539  NSLog(@"Y4NutritionViewController.viewDidLoad: headerVert: %3.2f", headerVert);
541  } else {
542 
544  [menuPanoramaView setBackgroundColor:[UIColor cyanColor]];
545 
546  [self.view addSubview:[self produceBigJumpButton]];
547 
548  }
549 
550 
551  CGRect tailFrame = CGRectMake(4.0, 0.0, tableWidth - 8.0, 46.0);
552  menuItemControlBar = [[Z6MenuItemControlbar alloc] initWithFrame:tailFrame andDelegate:self];
553  [menuItemControlBar showBar:YES];
554  /* end - first section header/tailer views */
555 
556  /* second section header/tailer views */
557 // NSArray *todays = [localData todaysMenuItems];
558 // NSInteger todaysMenuItemCount = [todays count];
559 // NSLog(@"Y4NutritionViewController.viewDidLoad: todays count: %ld", (unsigned long)todaysMenuItemCount);
560 
561 
562  float dateTimeViewFrameHeight = [[Z3DateTimeFocusView alloc] heightRequirement];
563 
564  CGRect dateTimeViewFrame = CGRectMake(0.0, 0.0, tableWidth - 8.0, dateTimeViewFrameHeight);
565  dateTimeView = [[Z3DateTimeFocusView alloc] initWithFrame:dateTimeViewFrame andDelegate:self];
566  menuApproveView = [[Z3MenuApprovalView alloc] initWithDelegate:self];
567 
568  menuApprovalInstructions = [[Z1ContainedLabel alloc] initWithFrame:dateTimeViewFrame];
569  [Z5DataController extraLabelize:menuApprovalInstructions.messageLabel withText:@"tap Bravo when complete\nswipe right to remove items"];
570  NSNumber *labelHeight = [NSNumber numberWithFloat:64.0];
571  [menuApprovalInstructions setContainedHeight:labelHeight];
572 
573  /* end - second section header/tailer views */
574 
575  float verticalPos = navbarBottomEdge + 26;
576 
577  CGRect tableViewFrame = CGRectMake(6.0, verticalPos, totalFrame.size.width - 12, 220.0); // TRIAL
578 
579  // parameterTableView = [[U3ParameterTableView alloc]
580  // initWithFrame: tableViewFrame
581  // andComponents:focusUSDAparameterArray
582  // inContainer:self];
583 
585  initWithFrame: tableViewFrame
586  andComponents:focusUSDAparameterArray
587  inContainer:self];
588 
589  [parameterTableView setViewForHead:menuPanoramaView];
590  [parameterTableView setViewForTail:menuItemControlBar];
591  [parameterTableView setViewForMenuHead:menuApproveView];
592  [parameterTableView setViewForMenuTail:menuApprovalInstructions];
593  [parameterTableView setNeedsDisplay];
594 
595  float parameterTableHgt = [parameterTableView getViewHeightReqmt];
596  float tableVertical = parameterTableHgt;
597  if (parameterTableHgt > (overallNutritionSize.height - navbarBottomEdge)) {
598  tableVertical = overallNutritionSize.height - navbarBottomEdge;
599  }
600  tableViewFrame = CGRectMake(2.0, verticalPos, totalFrame.size.width - 4, tableVertical + 2.0);
601  [parameterTableView setFrame:tableViewFrame];
602  // end - add a parameter table, it will be shown for item selected
603 
604  galleryFocusMenuItem = nil;
605 
606  verticalPos += (parameterTableHgt+2);
607 
608 
609  verticalPos += 44;
610 
611 }
613 {
614  CGSize viewSize = self.view.frame.size;
615  UIImage *resizedImage = [Z5DataController imageNamed:nutritionMenuLandscapeBackgroundName sizedToLandscape:viewSize];
616  backgrdImageView = [[UIImageView alloc] initWithImage:resizedImage];
617  [backgrdImageView setContentMode:UIViewContentModeScaleAspectFill];
618  [self.view addSubview:backgrdImageView];
619 
620 }
622 {
623  CGSize viewSize = self.view.frame.size;
624  UIImage *resizedImage = [Z5DataController imageNamed:nutritionMenuPortraitBackgroundName sizedToPortrait:viewSize];
625  [backgrdImageView setImage:resizedImage];
626 }
628 {
629  CGSize viewSize = self.view.frame.size;
630  UIImage *resizedImage = [Z5DataController imageNamed:nutritionMenuLandscapeBackgroundName sizedToLandscape:viewSize];
631  [backgrdImageView setImage:resizedImage];
632 }
634 {
635 }
637 {
638 }
639 
640 #pragma mark - factoring
642 {
643  float tableWidth = self.view.bounds.size.width;
644 
645  CGRect noChoiceLabelFrame = CGRectMake(2.0, 2.0, tableWidth-40.0, 42.0);
646  Z1ContainedLabel *tableNoChoice = [[Z1ContainedLabel alloc] initWithFrame:noChoiceLabelFrame];
647  // UILabel *noChoiceLabel = [[UILabel alloc] init];
648  UILabel *noChoiceLabel = [tableNoChoice messageLabel];
649  [noChoiceLabel postAttributedText:@"Empty database, refresh through messages"];
650  [noChoiceLabel setTextColor:[UIColor blackColor]];
651  [noChoiceLabel setOpaque:YES];
652  [noChoiceLabel setBackgroundColor:[UIColor greenColor]];
653 
654  [noChoiceLabel.layer setBorderColor: [[UIColor blackColor] CGColor]];
655  [noChoiceLabel.layer setBorderWidth: 2.0];
656  noChoiceLabel.layer.cornerRadius = 8.0f;
657  noChoiceLabel.layer.masksToBounds = YES;
658 
659  [tableNoChoice setMessageLabel:noChoiceLabel];
660  NSNumber *labelHeight = [NSNumber numberWithFloat:50.0];
661  [tableNoChoice setContainedHeight:labelHeight];
662  return tableNoChoice;
663 }
664 - (UIButton *) produceBigJumpButton {
665  float tableWidth = self.view.bounds.size.width;
666 
667  CGRect jumpToMsgsInstructionFrame = CGRectMake(30.0, 160.0, tableWidth - 60.0, 180.0);
668  UIButton *jumpToMsgsInstruction = [[UIButton alloc] initWithFrame:jumpToMsgsInstructionFrame];
669  [jumpToMsgsInstruction setBackgroundColor:[UIColor greenColor]];
670  [jumpToMsgsInstruction setTitle:@"Scan the mail\nfor messages\nor jump to the web or drive\nor even take a snapshot" forState:UIControlStateNormal];
671  [jumpToMsgsInstruction setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
672  [jumpToMsgsInstruction addGradientFace:[UIColor blueColor]];
673  [jumpToMsgsInstruction addTarget:self action:@selector(jumpToMenuItemCreate) forControlEvents:UIControlEventTouchUpInside];
674  return jumpToMsgsInstruction;
675 }
676 // initial data fetch
677 - (NSArray *) fetchMenuChoicesGalleryNum:(NSNumber *)galleryNum
678 {
679  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
680  // @TODO -- due for revision at multi-gallery (home kitchen, Mondellos, the-truck)
681  // NSNumber *galleryChoice = [NSNumber numberWithInt:1];
682  NSArray *localDataMenuItems = [localData retrieveAllMenuItems:galleryNum];
683  return localDataMenuItems;
684 }
685 
686 #pragma mark - JSON
687 
691 -(NSArray *)usdaArrayFromJSON: (NSString *)jsonString
692 {
693  NSLog(@"Y4NutritionViewController.menuParameterArray: jsonString: %@", jsonString);
694  NSData *jsonReturnData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
695  NSError *err;
696  if (jsonReturnData) {
697  NSObject *jsonSerialized = [NSJSONSerialization JSONObjectWithData:jsonReturnData options:NSJSONReadingAllowFragments error:&err];
698  if (jsonSerialized) {
699  NSString *jsonSerializedClass = NSStringFromClass([jsonSerialized class]);
700  NSLog(@"Y4NutritionViewController.menuParameterArray: valid data, top class: %@", jsonSerializedClass);
701  if (([jsonSerializedClass isEqualToString:@"__NSCFDictionary"])
702  || ([jsonSerializedClass isEqualToString:@"__NSSingleEntryDictionaryI"])) {
703  NSDictionary *jsonSerializedDict = (NSDictionary *)jsonSerialized;
704  NSObject *uNums = [jsonSerializedDict objectForKey:@"usdaNumbers"];
705  NSString *uNumsClass = NSStringFromClass([uNums class]);
706  NSLog(@"Y4NutritionViewController.menuParameterArray: uNumsClass: %@", uNumsClass);
707  NSArray *jsonSerializedArray = (NSArray *)uNums;
708  return jsonSerializedArray;
709  } else {
710  return nil;
711  }
712  } else {
713  return nil;
714  }
715  } else {
716  return nil;
717  }
718 }
719 
720 #pragma mark manage screen
721 
722 -(void) hideSelectedItemInfo:(Boolean)hideFlag
723 {
724  self.addItemOnToday.hidden = hideFlag;
725 }
726 
735 //- (NSMutableArray *)redetermineMenuItems { }
736 
737 #pragma mark -
739 {
740  NSString *focusUsdaNumbers = nil;
741 
742  NSString *focusRandomkey = [galleryFocusMenuItem randomkey];
743 
744  if (focusRandomkey) {
745  dbFocusMenuItem = [[[Z5DataController sharedInstance] localDataController] findOrCreateMenuItemWithRandomkey: focusRandomkey];
746  focusUsdaNumbers = dbFocusMenuItem.usdaNumbers;
747  if (focusUsdaNumbers) {
748  focusUSDAparameterArray = [[NSMutableArray alloc] initWithArray:[self usdaArrayFromJSON:focusUsdaNumbers]];
749  } else {
750  focusUsdaNumbers = @"";
751  focusUSDAparameterArray = [[NSMutableArray alloc] init];
752  }
753  NSLog(@"Y4NutritionViewController.updateParameterList: usda: %@",
754  focusUsdaNumbers);
755  } else {
756  NSLog(@"Y4NutritionViewController.updateParameterList: no focus item");
757  }
758 }
768 - (void) focusGallery
769 {
770  // NSLog(@"Y4NutritionViewController.focusGallery: focusMenuItem: %@", galleryFocusMenuItem);
771  if (galleryFocusMenuItem) {
772  NSLog(@"Y4NutritionViewController.focusGallery: focusMenuItem randomkey: %@", [galleryFocusMenuItem randomkey]);
773 
774  [self updateParameterList];
775 
776  [self hideSelectedItemInfo:NO];
777  // [self hideTodaysItemsInfo:YES];
778  } else {
779  // focusUsdaNumbers = @"";
780  [self hideSelectedItemInfo:YES]; // removes "add item" button
781  NSMutableArray<MenuItem *> *todaysMenuItems = [[[Z5DataController sharedInstance] localDataController] todaysMenuItems];
782  if ([todaysMenuItems count]) {
783  //[self hideTodaysItemsInfo:NO];
784  } else {
785  //[self hideTodaysItemsInfo:YES]; // hides proposed menu items table
786  }
788  }
789  // [self.focusItemInfoTextView setText:focusUsdaNumbers];
791  [parameterTableView update:focusUSDAparameterArray];
792  }
793  //NSString *todayDate = [_mDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
794  //[Z5DataController labelizeButton:self.dateButton withText:todayDate];
795 
796  // [self redetermineMenuItems];
797 
798  // [self orientUI];
799  if (0 < [focusUSDAparameterArray count]) {
800  NSIndexPath *addIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
802  [parameterTableView scrollToRowAtIndexPath:addIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
803  }
804  }
805 }
806 
808 {
809  if (galleryFocusSet) {
810  [menuItemControlBar setHidden:NO];
811  [menuItemControlBar showBar:YES];
812  } else {
813  [menuItemControlBar setHidden:YES];
814  [menuItemControlBar showBar:NO];
815  }
816 
817 }
818 
823 {
824  CGRect totalFrame = self.view.frame;
825  float paramLineHgt = 42.0 / totalFrame.size.height;
826 
827  NSLog(@"nutritionCollectionView.parameterViewHeight: nutrComp count: %ld",(unsigned long)[focusUSDAparameterArray count]);
828 
829  float suggestedParamHeight = paramLineHgt * [focusUSDAparameterArray count]; // = 42;
830 
831  if (0.28 < suggestedParamHeight) {
832  suggestedParamHeight = 0.28;
833  }
834  // float parameterViewHeight = totalFrame.size.height * 0.30;
835  float parameterViewHeight = totalFrame.size.height * suggestedParamHeight;
836  NSLog(@"nutritionCollectionView.parameterViewHeight: OVERALL parameterViewHeight: %3.2f", parameterViewHeight);
837  return parameterViewHeight;
838 }
839 
840 
841 - (void) placeButtons
842 {
843  NSLog(@"USDAparameterEntry.placeButtons: parameterViewHeight: %3.2f", [self parameterViewHeight]);
844 }
845 
847 {
849  [[self navigationController] pushViewController:vc3 animated:YES];
850 }
851 
852 #pragma mark - Z6MenuPanoramaDelegate
853 - (void) clickedMenuItem: (MenuItem *) menuItem atRow: (NSUInteger) row
854 {
855  // first problem - correlate row number to same array element
857  NSLog(@"Y4NutritionViewController.clickedMenuItem: galleryFocusMenuItem: %@", galleryFocusMenuItem);
858 
859  galleryFocusRow = row;
860  galleryFocusSet = YES;
861  [self focusEditControls];
862  NSString *info = menuItem.usdaNumbers;
863 
864  NSLog(@"NutritionViewController.clickedMenuItem: info: %@", info);
865  if (info) {
866  // NSLog(@"NutritionCollectionView.selectedMenuItem: usda: %@", info);
867  NSDictionary *currentParameters = [[[Z5DataController sharedInstance] localDataController] jsonForUsdaNumbersInMenuItem:menuItem];
868  focusUSDAparameterArray = [currentParameters objectForKey:@"usdaNumbers"];
869 
870  NSArray *annotatedArray = [[[Z5DataController sharedInstance] localDataController] annotateAuthority:focusUSDAparameterArray];
871 
873  focusUSDAparameterArray = [[NSMutableArray alloc] initWithArray:annotatedArray];
874 
875  [parameterTableView update:focusUSDAparameterArray];
876  if ([focusUSDAparameterArray count] > 0) {
877  NSInteger nRowsSect0 = [parameterTableView numberOfRowsInSection:0];
878  NSIndexPath *firstIndex = [NSIndexPath indexPathForRow:0 inSection:0];
879  if (0 < nRowsSect0) {
880  [parameterTableView scrollToRowAtIndexPath:firstIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
881  } else {
882  NSLog(@"NutritionViewController.clickedMenuItem: Huh?");
883  }
884  } else {
885  NSLog(@"NutritionViewController.clickedMenuItem: no parameters for selected item");
886  }
887 
888  [self reposition];
889  } else {
890  // [self.focusItemInfoTextView setText:@"no USDA info"];
892  focusUSDAparameterArray = [[NSMutableArray alloc] init];
893  [parameterTableView update:nil];
894 
895  // [self orientUI];
896  }
897 }
898 
899 - (Boolean) getSelectedGalleryRow:(NSUInteger *)inforow
900 {
901  Boolean ret = galleryFocusSet;
902  if (galleryFocusSet) {
903  *inforow = galleryFocusRow;
904  NSLog(@"Y4NutritionViewController.getSelectedGalleryRow: %lu", (unsigned long)galleryFocusRow);
905  } else {
906  NSLog(@"Y4NutritionViewController.getSelectedGalleryRow: no Selection");
907  }
908  return ret;
909 }
910 #pragma mark - <Z5HealthConnectionProtocol>
911 - (void) healthQuantityTypeData: (HKQuantityType *)quantityType success: (Boolean) flag
912 {
913  NSLog(@"Y4NutritionViewController.healthQuantityTypeData: ?");
914 }
916 {
917  [parameterTableView reloadData];
918 
919  // [proposedMenuTableView reloadData];
920 }
921 #pragma mark <U3ParameterTableDelegate>
922 // this method called from ParameterTableView.update, after it
923 // updates its vars.
924 - (void) updatedParameterArray:(NSArray *)menuItemArray
925 {
926  NSLog(@"Y4NutritionViewController.updatedParameterArray, called after table-view update");
927  [focusUSDAparameterArray removeAllObjects];
928  [focusUSDAparameterArray addObjectsFromArray:menuItemArray];
929 }
931 {
932  // float parameterPanelHeight = [self parameterViewHeight];
933  float parameterPanelHeight = [parameterTableView getViewHeightReqmt];
934 
935  NSLog(@"Y4NutritionViewController.resizeParameterFrame: parameterPanelHeight: %3.2f", parameterPanelHeight);
936 
937  CGRect totalFrame = self.view.frame;
938  float verticalPos = navbarBottomEdge + 2.0;
939  float verticalHeight = [parameterTableView getViewHeightReqmt];
940  float maxVertical = totalFrame.size.height - verticalPos;
941  if (verticalHeight > maxVertical) {
942  verticalHeight = maxVertical - 4.0;
943  }
944 
945  CGRect tableViewFrame = CGRectMake(2.0, verticalPos, totalFrame.size.width - 4, verticalHeight);
946  [parameterTableView setFrame:tableViewFrame];
947 
948  return parameterPanelHeight;
949 }
950 
951 - (void) didSelectParameterRow: (NSIndexPath *)iPath
952 {
953  editMenuItemIndexRow = [iPath row];
954 }
955 
956 #pragma mark - Z6MenuItemControlbarDelegate
957 
959 {
960  if (nil != galleryFocusMenuItem) {
961  // NSString *textusda = [self.focusItemInfoTextView text];
962  // self.focusMenuItem.usdaNumbers = textusda;
963  // NSLog(@"NutritionCollectionView.fireRecipeCheck: usda: %@", dbFocusMenuItem.usdaNumbers);
964 
965  U7UsdaParamEntryViewController *recipeView =
966  [[U7UsdaParamEntryViewController alloc] initWithMenuItem:galleryFocusMenuItem];
967  [self.navigationController pushViewController:recipeView animated:YES];
968 
969  } else {
970  NSLog(@"CollectionView.fireRecipeCheck: nil focusMenuItem");
971  }
972 }
973 
974 - (void) clearGallery
975 {
976  dbFocusMenuItem = nil;
977  currentlySelectedCell = nil;
978  [self focusGallery];
979 }
980 - (void )addMenuItemOnToday:(UIButton *)sender {
981 
982  NSLog(@"Y4NutritionViewController.addMenuItemOnToday ==========%@===============", [galleryFocusMenuItem name]);
983 
984  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
985  [localData putMenuItemOnToday:galleryFocusMenuItem];
986 
987  NSArray<MenuItem *> *q = [localData todaysMenuItems];
988  [parameterTableView updateMenu:q];
989 
990  [self clearGallery];
991 
992  // put entry into healthkit, for today
993  NSDictionary *currentParameters = [[[Z5DataController sharedInstance] localDataController] jsonForUsdaNumbersInMenuItem:dbFocusMenuItem];
994  NSLog(@"Y4NutritionViewController.addMenuItemOnToday: currentParameters: %@", currentParameters);
995  // NSArray *parametersForHK = [currentParameters mutableArrayValueForKey:@"usdaNumbers"];
996  if (!currentParameters) {
997  // NSLog(@"Y4NutritionViewController.addMenuItemOnToday: NO PARAMETERS FOR MENU ITEM");
998  }
999 
1000  [self reposition];
1001 
1002  [parameterTableView scrollForInstructionPanel];
1003 }
1004 
1005 #pragma mark <U3MenuTableDelegate>
1006 // This method is called from the menu table. If the method returns nil, there will be no overall cable header. Otherwise it is to be the beginning of the day, for the calendar menu – which could contain a number of meals.
1007 //
1008 - (NSDate *) dateForHeader
1009 {
1010  NSDate *dayBegin = nil;
1011  return dayBegin;
1012 }
1013 
1014 - (Boolean) hasFooter
1015 {
1016  return YES;
1017 }
1018 
1019 - (void) updatedMenuItemArray:(NSArray *)menuItemArray
1020 {
1021  NSLog(@"Y4NutritionViewController.updatedMenuItemArray: ??");
1022 }
1023 
1028 {
1029  CGRect totalFrame = self.view.frame;
1030  float leftMargin = 0.02*totalFrame.size.width;
1031  float topMargin = navbarBottomEdge;
1032  float controlWidth = 0.96*totalFrame.size.width;
1033 
1034  float menuItemListPanelHeight = [parameterTableView getViewHeightReqmt];
1035  CGRect menuItemParametersFrame = CGRectMake(leftMargin, topMargin + 4, controlWidth, menuItemListPanelHeight);
1036  [parameterTableView setFrame:menuItemParametersFrame];
1037 
1038  NSLog(@"Y4NutritionViewController.resizeMenuItemFrame: height: %3.2f", menuItemParametersFrame.size.height);
1039 
1040  return menuItemListPanelHeight;
1041 }
1042 
1043 - (void) didSelectMenuItemRow: (NSIndexPath *)iPath
1044 {
1045  NSLog(@"Y4NutritionViewController.didSelectMenuItemRow: NO-OP NO-OP");
1046  editMenuItemIndexRow = [iPath row];
1047 }
1048 
1049 // -(void)didSwipeMenu:(UIGestureRecognizer *)gestureRecognizer {
1050 //
1051 // if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
1052 // CGPoint swipeLocation = [gestureRecognizer locationInView:proposedMenuTableView];
1053 // swipedMenuIndexPath = [proposedMenuTableView indexPathForRowAtPoint:swipeLocation];
1054 // UITableViewCell* swipedCell = [proposedMenuTableView cellForRowAtIndexPath:swipedMenuIndexPath];
1055 // NSLog(@"NutritionView.didSwipeMenu: cell row: %ld", (long)[swipedMenuIndexPath row]);
1056 // [swipedCell setBackgroundColor:[UIColor orangeColor]];
1057 //
1058 // Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
1059 // NSInteger nRow = [swipedMenuIndexPath row];
1060 // if ((0<=nRow) && ([[localData todaysMenuItems] count] > nRow)) {
1061 // [[localData todaysMenuItems] removeObjectAtIndex:nRow];
1062 // }
1063 //
1064 // [proposedMenuTableView updateSingle:[localData todaysMenuItems]];
1065 // [proposedMenuTableView reloadData];
1066 // }
1067 // }
1068 
1073 {
1074  NSLog(@"NutritionViewController.didTapMenuFooter");
1075  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
1076  [localData clearTodaysMenuItems];
1077 
1078  [parameterTableView setNeedsDisplay];
1079 }
1081 {
1082  NSLog(@"NutritionViewController.didTapClearFooter");
1083  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
1084  [localData clearTodaysMenuItems];
1085  NSArray *todays = [localData todaysMenuItems];
1086  [parameterTableView updateMenu:todays];
1087 }
1094 {
1095  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
1096  if (0 < [[localData todaysMenuItems] count]) {
1097 
1098  [localData logTodaysMenu:@"didTapBravoFooter"];
1099 
1100  NSDate *rightNow = [NSDate dateWithTimeIntervalSinceNow:0];
1101  // NSDate *rightNowHere = [localData toLocalTime:rightNow];
1102  [localData todaysMealRequested:rightNow];
1103 
1104  Y4MenuPostViewController *mealView = [[Y4MenuPostViewController alloc] init];
1105  [self.navigationController pushViewController:mealView animated:YES];
1106 
1107  } else {
1108  NSLog(@"NutritionCollectionView.didTapBravoFooter: no items in todays menu");
1109  }
1110 }
1112 {
1113  NSLog(@"NutritionCollectionView.didTapNetworkFooter");
1114 }
1115 
1117 {
1118  CGFloat yOffset = 0;
1119 
1120  if (parameterTableView.contentSize.height > parameterTableView.bounds.size.height) {
1121  yOffset = parameterTableView.contentSize.height - parameterTableView.bounds.size.height;
1122  }
1123 
1124  [parameterTableView setContentOffset:CGPointMake(0, yOffset) animated:NO];
1125 }
1126 
1127 #pragma mark button action
1129 {
1130  NSLog(@"NutritionView.clickClearDay");
1131  dbFocusMenuItem = nil;
1132  galleryFocusMenuItem = nil;
1133  // selectedRow = -1;
1134  galleryFocusSet = NO;
1135  [self focusEditControls];
1136  currentlySelectedCell = nil;
1137  [self focusGallery];
1138  [parameterTableView update:nil];
1139  [parameterTableView setNeedsDisplay];
1140 
1141  // REDO - package method
1142  float parameterTableHgt = [parameterTableView getViewHeightReqmt];
1143  float verticalPos = navbarBottomEdge + 2;
1144  CGRect totalFrame = self.view.bounds;
1145  CGRect tableViewFrame = CGRectMake(2.0, verticalPos, totalFrame.size.width - 4, parameterTableHgt);
1146  [parameterTableView setFrame:tableViewFrame];
1147  // end - package method
1148 }
1149 
1150 
1152 {
1153  NSLog(@"NutritionCollectionView.clickMenuItemTodayButton");
1154 }
1155 
1159 - (void) clickAction
1160 {
1161  NSLog(@"NutritionCollectionView.clickAction");
1162 
1163  // [self scrollToBottom];
1164 
1165  [parameterTableView setInitialSectionCollapsed:YES];
1166 
1167  [self.view setNeedsDisplay];
1168  // NSIndexPath *menuTopPath = [NSIndexPath indexPathForRow:0 inSection:1];
1169 
1170  //warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.
1171  // [parameterTableView scrollToRowAtIndexPath:menuTopPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
1172 }
1173 
1174 /*
1175 - (void) healthQuantityTypeData: (HKQuantityType *)quantityType success: (Boolean) flag
1176 {
1177  NSString *rowQuanTypeString = [quantityType description];
1178  NSLog(@"Y4NutritionViewController.healthQuantityTypeData: rowQuantityType: %@ flag: %@", rowQuanTypeString, (flag?@"true":@"false"));
1179  NSString *protoCaption = [rowQuanTypeString substringFromIndex:31]; // length 'HKQuantityTypeIdentifierDietary' = 31
1180  [parameterTableView markAuthorization:protoCaption as:flag];
1181  [parameterTableView reloadData];
1182 
1183 }
1184 */
1185 - (void) attributed
1186 {
1187  NSString *redText = @"red text";
1188  NSString *greenText = @"green text";
1189  NSString *purpleBoldText = @"purple bold text";
1190 
1191  NSString *text = [NSString stringWithFormat:@"Here are %@, %@ and %@",
1192  redText,
1193  greenText,
1194  purpleBoldText];
1195 
1196  // If attributed text is supported (iOS6+)
1197  if ([self.label respondsToSelector:@selector(setAttributedText:)]) {
1198 
1199  // Define general attributes for the entire text
1200  NSDictionary *attribs = @{
1201  NSForegroundColorAttributeName: self.label.textColor,
1202  NSFontAttributeName: self.label.font
1203  };
1204  NSMutableAttributedString *attributedText =
1205  [[NSMutableAttributedString alloc] initWithString:text
1206  attributes:attribs];
1207 
1208  // Red text attributes
1209  UIColor *redColor = [UIColor redColor];
1210  NSRange redTextRange = [text rangeOfString:redText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
1211  [attributedText setAttributes:@{NSForegroundColorAttributeName:redColor}
1212  range:redTextRange];
1213 
1214  // Green text attributes
1215  UIColor *greenColor = [UIColor greenColor];
1216  NSRange greenTextRange = [text rangeOfString:greenText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
1217  [attributedText setAttributes:@{NSForegroundColorAttributeName:greenColor}
1218  range:greenTextRange];
1219 
1220  // Purple and bold text attributes
1221  UIColor *purpleColor = [UIColor purpleColor];
1222  UIFont *boldFont = [UIFont boldSystemFontOfSize:self.label.font.pointSize];
1223  NSRange purpleBoldTextRange = [text rangeOfString:purpleBoldText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
1224  [attributedText setAttributes:@{NSForegroundColorAttributeName:purpleColor,
1225  NSFontAttributeName:boldFont}
1226  range:purpleBoldTextRange];
1227 
1228  self.label.attributedText = attributedText;
1229  }
1230  // If attributed text is NOT supported (iOS5-)
1231  else {
1232  self.label.text = text;
1233  }
1234 }
1235 //- (MenuItem *) menuItemForIndex: (NSInteger) choice
1236 //{
1237 // MenuItem *ret =
1238 //}
1239 // - (UIImage *) foodImage: (NSInteger) choice
1240 // {
1241 // UIImage *cellFood;
1242 // switch (choice) {
1243 // case 0:
1244 // cellFood = [UIImage imageNamed:@"barbecued-ribs-200.png"];
1245 // break;
1246 //
1247 // case 1:
1248 // cellFood = [UIImage imageNamed:@"beef-tostadas.jpg"];
1249 // break;
1250 //
1251 // case 2:
1252 // cellFood = [UIImage imageNamed:@"mex-crab-dip.jpg"];
1253 // break;
1254 //
1255 // case 3:
1256 // cellFood = [UIImage imageNamed:@"salami2.jpg"];
1257 // break;
1258 //
1259 // case 4:
1260 // cellFood = [UIImage imageNamed:@"salamiAndCheese.jpg"];
1261 // break;
1262 //
1263 // case 5:
1264 // cellFood = [UIImage imageNamed:@"mashedPotatoes.jpg"];
1265 // break;
1266 //
1267 // case 6:
1268 // cellFood = [UIImage imageNamed:@"orange-chicken-5.jpg"];
1269 // break;
1270 //
1271 // default:
1272 // break;
1273 // }
1274 // // cellFood = [UIImage imageNamed:@"barbecued-ribs-200.png"];
1275 // return cellFood;
1276 // }
1277 
1279  [super didReceiveMemoryWarning];
1280  // Dispose of any resources that can be recreated.
1281 }
1282 @end
NSUInteger galleryFocusRow
NSInteger editMenuItemIndexRow
NSString * nutritionMenuPortraitBackgroundName
UIView< Z6ContainedView > * menuPanoramaView
void extraLabelize:withText:(UILabel *targLabel, [withText] NSString *txt)
MenuItem * galleryFocusMenuItem
Z1ContainedLabel * menuApprovalInstructions
Boolean isInitialSectionCollapsed
NSArray< MenuItem * > * retrieveAllMenuItems:(NSNumber *galleryNo)
Z6MenuItemControlbar * menuItemControlBar
U3MenuChoicesTableView * parameterTableView
Boolean galleryFocusSet
MenuItem * menuItem
instancetype sharedInstance()
UIView< Z6ContainedView > * menuApproveView
UIImageView * backgrdImageView
void hideSelectedItemInfo:(Boolean hideFlag)
NSArray * fetchMenuChoicesGalleryNum:(NSNumber *galleryNum)
void logTodaysMenu:(NSString *caption)
UILabel * messageLabel
UIImage * imageNamed:sizedToPortrait:(NSString *imageName, [sizedToPortrait] CGSize imageSize)
void todaysMealRequested:(NSDate *date)
Z1ContainedLabel * produceNoChoicesLabel()
UIImage * imageNamed:sizedToLandscape:(NSString *imageName, [sizedToLandscape] CGSize imageSize)
NSMutableArray * focusUSDAparameterArray
UICollectionViewCell * currentlySelectedCell
NSString * nutritionMenuLandscapeBackgroundName
Singleton interface to both core and remote data sources.
NSArray * usdaArrayFromJSON:(NSString *jsonString)
NSInteger focusGallery
Z3DateTimeFocusView * dateTimeView
CGSize overallNutritionSize
void putMenuItemOnToday:(MenuItem *focusMenuItem)
NSIndexPath * swipedMenuIndexPath
NSMutableArray * nutritionComponents
NSMutableArray< NSString * > * rows
MenuItem * dbFocusMenuItem
NSMutableArray< MenuItem * > * todaysMenuItems
float navbarBottomEdge