Today's Menu  Portugal
journal and plan nutrition
ZCMNutritionRootViewController.m
Go to the documentation of this file.
1 //
2 // ZCMNutritionRootViewController.m
3 // CoordMedicRove
4 //
5 // Created by Don Zeek on 5/5/15.
6 // Copyright (c) 2015 net.dzeek.cp125a. All rights reserved.
7 //
8 #import <MobileCoreServices/MobileCoreServices.h>
9 
16 #import "Z5DataController.h"
22 
23 #import "Meal+CoreDataClass.h"
25 
27 {
28  UIButton *menuButton;
29  UIButton *snapMealButton;
30  UIButton *timelineButton;
31  UIButton *editorButton;
32  UIButton *healthkitButton;
33 
44 
45  UIImageView *backgrdImageView;
48 }
49 @end
50 
83 @implementation ZCMNutritionRootViewController
84 
85 - (id)init
86 {
87  self = [super init];
88  if (self) {
89  self.title = @"Nutrition";
90  rootPortraitBackgroundName = @"Wine-cheese-bread-grapes.png";
91  rootLandscapeBackgroundName = @"honeyCheeseGrapesWineBread.png";
92  }
93  return self;
94 }
95 
103 - (void)viewDidLoad {
104  [super viewDidLoad];
105 
106  // determine approriate frames
107  [self orientLoadUI];
108 
109  // do one-time creates and connects
110  [menuButton addTarget:self action:@selector(goToMenuScreen) forControlEvents:UIControlEventTouchUpInside];
111  [snapMealButton addTarget:self action:@selector(goToSnapMealScreen) forControlEvents:UIControlEventTouchUpInside];
112  [timelineButton addTarget:self action:@selector(goToTimelineScreen) forControlEvents:UIControlEventTouchUpInside];
113  [editorButton addTarget:self action:@selector(goToEditorScreen) forControlEvents:UIControlEventTouchUpInside];
114  [healthkitButton addTarget:self action:@selector(toggleAppleHealthKit) forControlEvents:UIControlEventTouchUpInside];
115  [self.view addSubview:menuButton];
116  [self.view addSubview:snapMealButton];
117  [self.view addSubview:timelineButton];
118  [self.view addSubview:editorButton];
119  [self.view addSubview:healthkitButton];
120 
121  [self orientAppearUI];
122 
123  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
124  [[NSNotificationCenter defaultCenter] addObserver:self
125  selector:@selector(deviceOrientationDidChange:)
126  name: UIDeviceOrientationDidChangeNotification
127  object: nil];
128 
129  Z5GoogleLoginController *googleLogin = [[[Z5DataController sharedInstance] remoteDataController] loginController];
130  [googleLogin addLoginListener:self];
131 }
135 - (void) viewDidAppear:(BOOL)animated
136 {
137  // NSLog(@"NutritionRootView.viewDidAppear");
138  [self orientAppearUI];
139 
140  Z5GoogleLoginController *loginData = [[[Z5DataController sharedInstance] remoteDataController] loginController];
141 
142  if (loginData.signedIn.boolValue) {
143  [self placeLogoutButton];
144  }
145 }
146 - (void) viewWillDisappear:(BOOL)animated
147 {
148  // save database to user defaults
149  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
150  [localData saveContext];
151 }
155 - (void)deviceOrientationDidChange:(NSNotification *)notification {
156  // NSLog(@"ZCMNutritionRootViewController.deviceOrientationDidChange: %@", notification);
157  [self orientAppearUI];
158 }
159 
160 #pragma mark - ZOrientProtocol
161 - (void) orientLoadUI {
162 
163  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
164  switch (orientation)
165  {
166  case UIDeviceOrientationPortrait:
167  case UIDeviceOrientationFaceUp:
168  {
169  [self positionFramesPortrait];
170  [self viewDidLoadPortrait];
171  }
172  break;
173  case UIDeviceOrientationLandscapeLeft:
174  case UIDeviceOrientationLandscapeRight:
175  {
177  [self viewDidLoadLandscape];
178  }
179  break;
180  default:
181  {
182  ;
183  }
184  break;
185  }
186 }
187 - (void) orientAppearUI {
188  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
189  switch (orientation)
190  {
191  case UIDeviceOrientationPortrait:
192  case UIDeviceOrientationFaceUp:
193  {
194  [self positionFramesPortrait];
195  [self viewDidAppearPortrait];
196  }
197  break;
198  case UIDeviceOrientationLandscapeLeft:
199  case UIDeviceOrientationLandscapeRight:
200  {
202  [self viewDidAppearLandscape];
203  }
204  break;
205  default:
206  {
207  ;
208  }
209  break;
210  }
211 }
212 
214 {
215  CGSize viewSize = self.view.frame.size;
216  UIImage *resizedImage = [Z5DataController imageNamed:rootPortraitBackgroundName sizedToPortrait:viewSize];
217  backgrdImageView = [[UIImageView alloc] initWithImage:resizedImage];
218  [backgrdImageView setContentMode:UIViewContentModeScaleAspectFill];
219  [self.view addSubview:backgrdImageView];
220 
221  [self createFiveButtons];
222 }
224 {
225  CGSize viewSize = self.view.frame.size;
226  UIImage *resizedImage = [Z5DataController imageNamed:rootLandscapeBackgroundName sizedToLandscape:viewSize];
227  backgrdImageView = [[UIImageView alloc] initWithImage:resizedImage];
228  [backgrdImageView setContentMode:UIViewContentModeScaleAspectFill];
229  [self.view addSubview:backgrdImageView];
230 
231  [self createFiveButtons];
232 }
233 
235 {
236  // NSLog(@"ZCMNutritionRoolViewController.viewDidAppearPortrait");
237  [menuButton setFrame:menuButtonFramePortrait];
238  [snapMealButton setFrame:snapMealButtonFramePortrait];
239  [timelineButton setFrame:timelineButtonFramePortrait];
240  [editorButton setFrame:editorButtonFramePortrait];
241  [healthkitButton setFrame:healthkitButtonFramePortrait];
242 
243  CGSize viewSize = self.view.frame.size;
244  UIImage *resizedImage = [Z5DataController imageNamed:rootPortraitBackgroundName sizedToPortrait:viewSize];
245  [backgrdImageView setImage:resizedImage];
246 
247 }
249 {
250  // NSLog(@"ZCMNutritionRoolViewController.viewDidAppearLandscape");
251  [menuButton setFrame:menuButtonFrameLandscape];
252  [snapMealButton setFrame:snapMealButtonFrameLandscape];
253  [timelineButton setFrame:timelineButtonFrameLandscape];
254  [editorButton setFrame:editorButtonFrameLandscape];
255  [healthkitButton setFrame:healthkitButtonFrameLandscape];
256 
257  CGSize viewSize = self.view.frame.size;
258  UIImage *resizedImage = [Z5DataController imageNamed:rootLandscapeBackgroundName sizedToLandscape:viewSize];
259  [backgrdImageView setImage:resizedImage];
260 }
261 
262 #pragma mark -
263 
265 {
266  CGRect totalFrame = self.view.frame;
267  float topButtonTop = totalFrame.size.height * 0.40;
268  float leftMargin = totalFrame.size.width * 0.04;
269  float buttonWidth = totalFrame.size.width * 0.72;
270  float buttonHeight = 70; // totalFrame.size.height * 0.08;
271  float buttonInterval = 8.0;
272 
273  float buttonTop = topButtonTop;
274  menuButtonFramePortrait = CGRectMake(leftMargin,
275  buttonTop,
276  buttonWidth,
277  buttonHeight);
278  buttonTop += (buttonHeight + buttonInterval);
279  snapMealButtonFramePortrait = CGRectMake(leftMargin,
280  buttonTop,
281  buttonWidth,
282  buttonHeight);
283  buttonTop += (buttonHeight + buttonInterval);
284  timelineButtonFramePortrait = CGRectMake(leftMargin,
285  buttonTop,
286  buttonWidth,
287  buttonHeight);
288  buttonTop += (buttonHeight + buttonInterval);
289  editorButtonFramePortrait = CGRectMake(leftMargin,
290  buttonTop,
291  buttonWidth,
292  buttonHeight);
293  buttonTop += (buttonHeight + buttonInterval);
294  healthkitButtonFramePortrait = CGRectMake(leftMargin,
295  buttonTop,
296  buttonWidth,
297  buttonHeight);
298 }
300 {
301  CGRect totalFrame = self.view.frame;
302  float buttonTop = totalFrame.size.height * 0.15 ;
303  float leftButtonLeft = totalFrame.size.width * 0.04;
304  float buttonWidth = 66;
305  float buttonHeight = 70;
306  float buttonInterval = 8.0;
307 
308  float buttonLeft = leftButtonLeft;
309  menuButtonFrameLandscape = CGRectMake(buttonLeft,
310  buttonTop,
311  buttonWidth,
312  buttonHeight );
313  buttonLeft += (buttonWidth + buttonInterval);
314  snapMealButtonFrameLandscape = CGRectMake(buttonLeft,
315  buttonTop,
316  buttonWidth,
317  buttonHeight);
318  buttonLeft += (buttonWidth + buttonInterval);
319  timelineButtonFrameLandscape = CGRectMake(buttonLeft,
320  buttonTop,
321  buttonWidth,
322  buttonHeight);
323  buttonLeft += (buttonWidth + buttonInterval);
324  editorButtonFrameLandscape = CGRectMake(buttonLeft,
325  buttonTop,
326  buttonWidth,
327  buttonHeight);
328  buttonLeft += (buttonWidth + buttonInterval);
329  healthkitButtonFrameLandscape = CGRectMake(buttonLeft,
330  buttonTop,
331  buttonWidth,
332  buttonHeight);
333 }
334 
336 {
337  UIImage *pomegraniteSeedsImage = [UIImage imageNamed:@"pomegraniteSeedWhtCtr120.png"];
338 
339  CGRect zeroButtonFrame = CGRectMake(100, 100, 100, 100);
340 
341  // "Today's Menu"
342  menuButton = [Z5DataController makeGradientButton:@"Today's Menu" withFrame:zeroButtonFrame andFont:nil];
343  UIImage *menuButtonImage = [UIImage imageNamed:@"menuIcon60.png"];
344  [menuButton setImage:menuButtonImage forState:UIControlStateNormal];
345  [menuButton setBackgroundImage:pomegraniteSeedsImage forState:UIControlStateNormal];
346  [menuButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
347  [menuButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
348  // [menuButton addMask];
349 
350  // "Snap a Meal"
351  snapMealButton = [Z5DataController makeGradientButton:@"Snap" withFrame:zeroButtonFrame andFont:nil];
352  UIImage *snapMealButtonImage = [UIImage imageNamed:@"cameraIcon60.png"];
353  [snapMealButton setImage:snapMealButtonImage forState:UIControlStateNormal];
354  [snapMealButton setBackgroundImage:pomegraniteSeedsImage forState:UIControlStateNormal];
355  [snapMealButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
356  [snapMealButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
357 
358  // "Recent Meals"
359  timelineButton = [Z5DataController makeGradientButton:@"Recent Meals" withFrame:zeroButtonFrame andFont:nil];
360  UIImage *timelineButtonImage = [UIImage imageNamed:@"calendarIcon60.png"];
361  [timelineButton setImage:timelineButtonImage forState:UIControlStateNormal];
362  [timelineButton setBackgroundImage:pomegraniteSeedsImage forState:UIControlStateNormal];
363  [timelineButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
364  [timelineButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
365 
366  // "Gallery editor"
367  editorButton = [Z5DataController makeGradientButton:@"Kitchen" withFrame:zeroButtonFrame andFont:nil];
368  UIImage *editorButtonImage = [UIImage imageNamed:@"kitchenIcon60.png"];
369  [editorButton setImage:editorButtonImage forState:UIControlStateNormal];
370  [editorButton setBackgroundImage:pomegraniteSeedsImage forState:UIControlStateNormal];
371  [editorButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
372  [editorButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
373 
374 
375  UIFont *healthKitButtonFont = [UIFont fontWithName:@"Georgia-Bold" size:24.0];
376  healthkitButton = [Z5DataController makeGradientButton:@"Apple Healthkit" withFrame:zeroButtonFrame andFont:healthKitButtonFont];
377  UIImage *healthkitButtonImage = [UIImage imageNamed:@"heartIcon60.png"];
378  [healthkitButton setImage:healthkitButtonImage forState:UIControlStateNormal];
379  [healthkitButton setBackgroundImage:pomegraniteSeedsImage forState:UIControlStateNormal];
380  [healthkitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
381  [healthkitButton setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
382 
383 }
384 //- (UIImage *) imageNamed:(NSString *)imageName sizedToPortrait:(CGSize)imageSize
385 //{
386 // UIImage *backgrdImage = [UIImage imageNamed:imageName];
387 // CGSize photoSize = [backgrdImage size];
388 //
389 // float resizeRatio = photoSize.height / imageSize.height;
390 // UIImage *resizedImage = [UIImage imageWithCGImage:[backgrdImage CGImage]
391 // scale:backgrdImage.scale * resizeRatio
392 // orientation:(backgrdImage.imageOrientation) ];
393 // return resizedImage;
394 //}
395 //- (UIImage *) imageNamed:(NSString *)imageName sizedToLandscape:(CGSize)imageSize
396 //{
397 // UIImage *backgrdImage = [UIImage imageNamed:imageName];
398 // CGSize photoSize = [backgrdImage size];
399 //
400 //
401 // float resizeRatio = photoSize.width / imageSize.width;
402 // UIImage *resizedImage = [UIImage imageWithCGImage:[backgrdImage CGImage]
403 // scale:backgrdImage.scale * resizeRatio
404 // orientation:(backgrdImage.imageOrientation) ];
405 // return resizedImage;
406 //}
407 
408 #pragma mark - Z5GoogleLoginListener
409 - (void) signedIn: (GIDGoogleUser *)user
410 {
411  [self placeLogoutButton];
412 }
413 - (void) signedOut:(GIDGoogleUser *)user
414 {
415  self.navigationItem.rightBarButtonItem = nil;
416 }
417 
419 {
420  Z5GoogleLoginController *googleLogin = [[[Z5DataController sharedInstance] remoteDataController] loginController];
421 
422  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout"
423  style:UIBarButtonItemStylePlain
424  target:googleLogin
425  action:@selector(logOutGoogle)];
426  self.navigationItem.rightBarButtonItem = anotherButton;
427  self.navigationItem.rightBarButtonItem.enabled = YES;
428 }
429 
430 - (void)goToMenuScreen {
431  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
433  [localData logTodaysMenu:@"didFinishLaunchingWithOptions"];
434 
435  if (0 == [[localData todaysMenuItems] count]) {
437  [self.navigationController pushViewController:vc animated:YES];
438  } else {
439  Y4MenuPostViewController *menuPostVC = [[Y4MenuPostViewController alloc] init];
440  [self.navigationController pushViewController:menuPostVC animated:YES];
441  }
442 
443 }
446  usingDelegate: (id <UINavigationControllerDelegate, UIImagePickerControllerDelegate>) self];
447 }
449  NSLog(@"NutritionRoot.goToTimelineScreen");
451  [[self navigationController] pushViewController:vc2 animated:YES];
452 // CalendarDemoViewController *vc2 = [[CalendarDemoViewController alloc] init];
453 // [[self navigationController] pushViewController:vc2 animated:YES];
454 }
456  NSLog(@"NutritionRoot.goToOriginateScreen");
458  [[self navigationController] pushViewController:vc3 animated:YES];
459 }
461  NSLog(@"NutritionRoot.goToEditorScreen");
463  [[self navigationController] pushViewController:vc5 animated:YES];
464 }
466  NSLog(@"NutritionRoot.goToHealthKitScreen");
467  T1HealthKitViewController *vc3 = [[T1HealthKitViewController alloc] initWithStyle:UITableViewStyleGrouped];
468  [[self navigationController] pushViewController:vc3 animated:YES];
469 }
470 
472 {
473  NSLog(@"ZCMNutritionRootViewController.reviewComplete");
474 
475  // TODO: log/display meals
476 }
478  NSNumber *usingHealthKit = [[[Z5DataController sharedInstance] localDataController] usingHealthKit];
479  Boolean updatedStatus = ![usingHealthKit boolValue];
480  [[[Z5DataController sharedInstance] localDataController] setUseHealthKit:updatedStatus];
481  if (updatedStatus) {
482  [self goToHealthKitScreen];
483  }
484 }
485 
486 
487 #pragma mark - UIImagePickerControllerDelegate
488 
489 - (void)imagePickerController:(UIImagePickerController *)picker
490 didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
491 {
492  // NSLog(@"CameraSnap.didFinishPickingMediaWithInfo: info: %@", info);
493  // UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
494 
495  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
496  UIImage *originalImage, *editedImage, *imageToSave;
497 
498  // Handle a still image capture
499  if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
500  == kCFCompareEqualTo) {
501 
502  editedImage = (UIImage *) [info objectForKey:
503  UIImagePickerControllerEditedImage];
504  originalImage = (UIImage *) [info objectForKey:
505  UIImagePickerControllerOriginalImage];
506 
507  if (editedImage) {
508  imageToSave = editedImage;
509  } else {
510  imageToSave = originalImage;
511  }
512 
513 // Originally, example saved the new image (original or
514 // edited) to the Camera Roll:
515 // UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
516 
517  // within the app, at this poing a meal is created, the image
518  // set according to what we have, and the time/date. The meal is
519  // provided as input to the snapMealEntryVC
520 
521  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
522  NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
523  NSArray <Meal *> *snapmeals = [localData findOrCreateMealWithDate:nowDate];
524  Meal *snapMeal = [snapmeals lastObject];
525  snapMeal.photo = UIImagePNGRepresentation(imageToSave);
526 
527  P2SnapMealEntryViewController *mealSnapReview = [[P2SnapMealEntryViewController alloc] init];
528  [mealSnapReview setFocusImage:imageToSave];
529  [mealSnapReview setFocusMeal:snapMeal];
530  [mealSnapReview setMealDelegate:self];
531  [self.navigationController pushViewController:mealSnapReview animated:YES];
532  }
533 
534  // Handle a movie capture
535  if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
536  == kCFCompareEqualTo) {
537 
538  NSString *moviePath = [[info objectForKey:
539  UIImagePickerControllerMediaURL] path];
540 
541  if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
542  UISaveVideoAtPathToSavedPhotosAlbum (
543  moviePath, nil, nil, nil);
544  }
545  }
546 
547  [picker dismissViewControllerAnimated:YES completion:nil];
548 
549 }
550 - (void) returnFromEntryReviewWithInfo:(UIImage *)imageToSave withCaption:(NSString *)caption
551 {
552  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
553 
554  NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
555  NSArray<Meal *> *snapMealArr = [localData findOrCreateMealWithDate:nowDate];
556  Meal *snapMeal = [snapMealArr lastObject];
557  snapMeal.photo = UIImageJPEGRepresentation(imageToSave, 0.90);
558  snapMeal.galleryNumber = [NSNumber numberWithInt:1];
559 }
560 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
561 {
562  NSLog(@"CameraSnap.imagePickerControllerDidCancel");
563  [picker dismissViewControllerAnimated:YES completion:nil];
564 };
565 
567  [super didReceiveMemoryWarning];
568  // Dispose of any resources that can be recreated.
569 }
570 
571 @end
void addLoginListener:(id< Z5GoogleLoginListener > ear)
NSNumber * galleryNumber
instancetype sharedInstance()
void logTodaysMenu:(NSString *caption)
NSMutableArray * createTodaysMenuFromUserDefaults()
UIImage * imageNamed:sizedToPortrait:(NSString *imageName, [sizedToPortrait] CGSize imageSize)
UIImage * imageNamed:sizedToLandscape:(NSString *imageName, [sizedToLandscape] CGSize imageSize)
NSArray< Meal * > * findOrCreateMealWithDate:(NSDate *findDate)
Singleton interface to both core and remote data sources.
UIButton * makeGradientButton:withFrame:andFont:(NSString *caption, [withFrame] CGRect buttonFrame, [andFont] UIFont *buttonFont)
NSData * photo
BOOL startCameraControllerFromViewController:usingDelegate:(UIViewController *controller, [usingDelegate] id< UINavigationControllerDelegate, UIImagePickerControllerDelegate > delegate)