Today's Menu  Portugal
journal and plan nutrition
CameraSnapViewController.m
Go to the documentation of this file.
1 //
2 // cameraSnapViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 11/13/16.
6 // Copyright © 2016 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import <MobileCoreServices/MobileCoreServices.h>
10 
11 #import "Z5DataController.h"
13 #import "U3ParameterTableView.h"
15 #import "SecondViewController.h"
18 #import "U4WebViewController.h"
20 
21 @interface CameraSnapViewController ()
22 
23 @end
24 
54 @implementation CameraSnapViewController
55 {
56  WKWebView *webView;
59  UIButton *buttonCammera;
60  UIButton *buttonPictures;
61  UIButton *buttonMessages;
62  UIButton *buttonUSDA;
63  UIButton *buttonDrive;
64  UIButton *buttonWeb;
65 }
66 UIImageView *cameraImage;
67 // U3ParameterTableView *parameterTableView;
68 UIButton *addDataButton;
69 UIColor *lightGreenColor;
71 NSMutableArray *usdaParameterArray;
72 NSString *instructionHTML;
73 
74 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
75 {
76  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
77  if (self) {
78  // Custom initialization
79  self.title = @"Create";
80  [self.view setBackgroundColor:[UIColor colorWithWhite:0.7f alpha:1.0f]];
81  usdaParameterArray = [[NSMutableArray alloc] init];
82  }
83  return self;
84 }
85 
86 - (void)viewDidLoad {
87  [super viewDidLoad];
88  // Do any additional setup after loading the view.
89 
90  lightGreenColor = [UIColor colorWithRed:0.2 green:1.0 blue:0.5 alpha:1.0];
91 
92  instructionHTML = @"<html><body style=\"background-color:#f0c07d;margin:20;padding-top:36;padding-left:160;top-margin:8\"><font size=\"+6\" face=\"verdana\"><b>Create a new menu item</b><ul><li> from your Camera<li>or Photos<li> from a message in your email<li> from a shared folder on Google-Drive<li> or from the web</ul></b></font></body></html>";
93 
94  self.tabBarItem.image = [UIImage imageNamed:@"picture_3"];
95  CGRect totalFrame = self.view.frame;
96  float navigationBarHeight = 62.0;
97  float instructionPanelHeight = 192.0;
98 
99  // [self.view setBackgroundColor:[[Z5DataController sharedInstance] wheatColor]];
100 
101  UIImage *backgrdImage = [UIImage imageNamed:@"empty_table.png"];
102  UIImageView *backgrd = [[UIImageView alloc] initWithImage:backgrdImage];
103  float topSpace = navigationBarHeight + instructionPanelHeight;
104  topSpace = 0.0; // TODO: tune page placement
105  CGRect backImageFrame = CGRectMake(0.0, topSpace, totalFrame.size.width, totalFrame.size.height - topSpace);
106  [backgrd setFrame:backImageFrame];
107  [self.view addSubview:backgrd];
108 
109  [self addButtons];
110 
111  CGRect webViewFrame = CGRectMake(32,
112  navigationBarHeight+8,
113  totalFrame.size.width - 64,
114  instructionPanelHeight);
115  webView = [[WKWebView alloc] initWithFrame:webViewFrame];
116  webView.navigationDelegate = self;
117  webView.UIDelegate = nil;
118  // background-color:#e5ceb3;
119  // background-image:url(\"tiffany_gradient.png\");
120  [webView loadHTMLString:instructionHTML baseURL:nil];
121  [webView setOpaque:NO];
122  webView.layer.cornerRadius = 8;
123  webView.layer.borderColor = [[UIColor blackColor] CGColor];
124  webView.layer.borderWidth = 3.0;
125  webView.clipsToBounds = YES;
126  [self.view addSubview:webView];
127 
128  topOfLoginControlbar = navigationBarHeight+8 + instructionPanelHeight + 8;
129 
131 }
132 
134 {
135  CGRect totalFrame = self.view.frame;
136  float loginControlbarHeight = 52;
137  CGRect loginControlbarFrame = CGRectMake(32, topOfLoginControlbar, totalFrame.size.width - 64, loginControlbarHeight);
138  loginControlbar = [[V7GoogleLoginControlbar alloc] initWithFrame:loginControlbarFrame andDelegate:self];
139  [self.view addSubview:loginControlbar];
140 }
141 - (void) viewWillAppear:(BOOL)animated
142 {
143  NSLog(@"CameraSnapViewController.viewWillAppear");
144  [loginControlbar appear];
145 }
146 - (void) addButtons
147 {
148  CGRect totalFrame = self.view.frame;
149 
150  float topButtonTop = 0.72;
151  float buttonInterval = 0.088;
152  float cameraButtonTop = totalFrame.size.height * topButtonTop;
153  float picturesButtonTop = totalFrame.size.height * (topButtonTop+buttonInterval);
154  float messagesButtonTop = totalFrame.size.height * (topButtonTop+2*buttonInterval);
155  // float driveButtonTop = totalFrame.size.height * (topButtonTop+3*buttonInterval);
156  // float webButtonTop = totalFrame.size.height * (topButtonTop+4*buttonInterval);
157  float buttonColumn1LeftEdge = totalFrame.size.width * 0.02;
158  float buttonColumn2LeftEdge = totalFrame.size.width * 0.52;
159  float buttonWidth = totalFrame.size.width * 0.44;
160 
161  CGRect buttonCameraFrame = CGRectMake(buttonColumn1LeftEdge,
162  cameraButtonTop,
163  buttonWidth,
164  totalFrame.size.height * 0.08);
166  withFrame:buttonCameraFrame
167  andFont:nil];
168  [buttonCammera addTarget:self
169  action:@selector(cameraButtonHit)
170  forControlEvents:UIControlEventTouchUpInside];
171  [self.view addSubview:buttonCammera];
172 
173  CGRect buttonPicturesFrame = CGRectMake(buttonColumn1LeftEdge,
174  picturesButtonTop,
175  buttonWidth,
176  totalFrame.size.height * 0.08);
178  withFrame:buttonPicturesFrame
179  andFont:nil];
180  [buttonPictures addTarget:self
181  action:@selector(picturesButtonHit)
182  forControlEvents:UIControlEventTouchUpInside];
183  [self.view addSubview:buttonPictures];
184 
185  CGRect buttonMessagesFrame = CGRectMake(buttonColumn1LeftEdge,
186  messagesButtonTop,
187  buttonWidth,
188  totalFrame.size.height * 0.08);
190  withFrame:buttonMessagesFrame
191  andFont:nil];
192  [buttonMessages addTarget:self
193  action:@selector(messagesButtonHit)
194  forControlEvents:UIControlEventTouchUpInside];
195  [self.view addSubview:buttonMessages];
196 
197  CGRect buttonUSDAFrame = CGRectMake(buttonColumn2LeftEdge,
198  cameraButtonTop,
199  buttonWidth,
200  totalFrame.size.height * 0.08);
202  withFrame:buttonUSDAFrame
203  andFont:nil];
204  [buttonUSDA addTarget:self
205  action:@selector(usdaButtonHit)
206  forControlEvents:UIControlEventTouchUpInside];
207  [self.view addSubview:buttonUSDA];
208 
209  CGRect buttonDriveFrame = CGRectMake(buttonColumn2LeftEdge,
210  picturesButtonTop,
211  buttonWidth,
212  totalFrame.size.height * 0.08);
214  withFrame:buttonDriveFrame
215  andFont:nil];
216  [buttonDrive addTarget:self
217  action:@selector(driveButtonHit)
218  forControlEvents:UIControlEventTouchUpInside];
219  [self.view addSubview:buttonDrive];
220 
221  CGRect buttonWebFrame = CGRectMake(buttonColumn2LeftEdge,
222  messagesButtonTop,
223  buttonWidth,
224  totalFrame.size.height * 0.08);
226  withFrame:buttonWebFrame
227  andFont:nil];
228  [buttonWeb addTarget:self
229  action:@selector(webButtonHit)
230  forControlEvents:UIControlEventTouchUpInside];
231  [self.view addSubview:buttonWeb];
232 }
233 
235 {
236  // NSDate *now = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
237 
238  U7UsdaParamEntryViewController *recipeView =
239  [[U7UsdaParamEntryViewController alloc] initWithMenuItem:menuItem];
240  [self.navigationController pushViewController:recipeView animated:YES];
241 }
242 
243 #pragma mark - WKUIDelegate
244 
245 - (void)webView:(WKWebView *)webView2 didFinishNavigation:(WKNavigation *)navigation
246 {
247  NSLog(@"CameraSnapViewController.didFinishNavigation");
248 
249  // NSInteger height = 260;
250  // [NSThread sleepForTimeInterval:.5];
251  // NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %ld);", (long)height];
252  // [webView2 evaluateJavaScript:javascript completionHandler:^(id objec, NSError *error) { /* objec = null */ } ];
253 }
255 {
257 }
259 {
261 }
263 {
264  Y3GmailViewController *googleBit = [[Y3GmailViewController alloc] init];
265  [googleBit setParentController:self];
266  [self.navigationController pushViewController:googleBit animated:YES];
267 }
269 {
270  V7USDAdataViewController *gUsdaVC = [[V7USDAdataViewController alloc] init];
271  [self.navigationController pushViewController:gUsdaVC animated:YES];
272 }
273 
275 {
276  V7GoogDriveViewController *gDriveVC = [[V7GoogDriveViewController alloc] init];
277  [self.navigationController pushViewController:gDriveVC animated:YES];
278 
279 
280  // SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
281  // [self.navigationController pushViewController:secondVC animated:YES];
282 }
283 - (void) webButtonHit
284 {
285  U4WebViewController *gWebVC = [[U4WebViewController alloc] init];
286  [self.navigationController pushViewController:gWebVC animated:YES];
287 }
288 
289 #pragma mark - UIImagePickerControllerDelegate
290 UIImagePickerController *mealEntryReviewPicker;
291 - (void)imagePickerController:(UIImagePickerController *)picker
292 didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
293 {
294  mealEntryReviewPicker = picker;
295 
296  P2SnapMealEntryViewController *recipeView =
297  [[P2SnapMealEntryViewController alloc] init];
298  [self.navigationController pushViewController:recipeView animated:YES];
299 }
300 - (void) returnFromEntryReviewWithInfo:(NSDictionary<NSString *,id> *)info
301 {
302  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
303  // NSLog(@"CameraSnap.didFinishPickingMediaWithInfo: info: %@", info);
304  // UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
305 
306  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
307  UIImage *originalImage, *editedImage, *imageToSave;
308 
309  // Handle a still image capture
310  if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
311  == kCFCompareEqualTo) {
312 
313  editedImage = (UIImage *) [info objectForKey:
314  UIImagePickerControllerEditedImage];
315  originalImage = (UIImage *) [info objectForKey:
316  UIImagePickerControllerOriginalImage];
317 
318  if (editedImage) {
319  imageToSave = editedImage;
320  } else {
321  imageToSave = originalImage;
322  }
323 
324  // Save the new image (original or edited) to the Camera Roll
325  // UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
326 
327  NSString *randomkey = [Z5DataController produceRandomkey];
328  menuItem = [localData findOrCreateMenuItemWithRandomkey:randomkey];
329  menuItem.photo = UIImageJPEGRepresentation(imageToSave, 0.90);
330  menuItem.galleryNumber = [NSNumber numberWithInt:1];
331 
332  usdaParameterArray = [[NSMutableArray alloc] init];
333  // [parameterTableView update:nil];
334 
335  [cameraImage setImage:imageToSave];
336  [buttonCammera setHidden:YES];
337  [buttonPictures setHidden:YES];
338  [buttonMessages setHidden:YES];
339  [buttonDrive setHidden:YES];
340  [buttonWeb setHidden:YES];
341  [addDataButton setHidden:NO];
342  }
343 
344  // Handle a movie capture
345  if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
346  == kCFCompareEqualTo) {
347 
348  NSString *moviePath = [[info objectForKey:
349  UIImagePickerControllerMediaURL] path];
350 
351  if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
352  UISaveVideoAtPathToSavedPhotosAlbum (
353  moviePath, nil, nil, nil);
354  }
355  }
356 
357  [mealEntryReviewPicker dismissViewControllerAnimated:YES completion:nil];
358 
359 }
360 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
361 {
362  NSLog(@"CameraSnap.imagePickerControllerDidCancel");
363  [picker dismissViewControllerAnimated:YES completion:nil];
364 };
366 {
367  }
368 + (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
369  usingDelegate: (id <UINavigationControllerDelegate,UIImagePickerControllerDelegate>) delegate
370 {
371 
372  if (([UIImagePickerController isSourceTypeAvailable:
373  UIImagePickerControllerSourceTypeCamera] == NO)
374  || (delegate == nil)
375  || (controller == nil)) {
376  NSLog(@"CameraSnap.startCameraControllerFromViewController: no source");
377  return NO;
378  }
379 
380 
381  UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
382  cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
383 
384  // Displays a control that allows the user to choose picture or
385  // movie capture, if both are available:
386  cameraUI.mediaTypes =
387  [UIImagePickerController availableMediaTypesForSourceType:
388  UIImagePickerControllerSourceTypeCamera];
389 
390  // Hides the controls for moving & scaling pictures, or for
391  // trimming movies. To instead show the controls, use YES.
392  cameraUI.allowsEditing = NO;
393 
394  cameraUI.delegate = delegate;
395 
396  [controller presentViewController: cameraUI animated: YES completion:^(void) {
397 
398  // this completion handler is running within a class function.
399  NSLog(@"CameraSnapViewContoller.camera controller complete");
400 
401  }];
402  return YES;
403 }
404 - (BOOL) startPictureControllerFromViewController: (UIViewController*) controller
405  usingDelegate: (id <UIImagePickerControllerDelegate,
406  UINavigationControllerDelegate>) delegate {
407 
408  if (([UIImagePickerController isSourceTypeAvailable:
409  UIImagePickerControllerSourceTypeCamera] == NO)
410  || (delegate == nil)
411  || (controller == nil)) {
412  NSLog(@"CameraSnap.startCameraControllerFromViewController: no source");
413  return NO;
414  }
415 
416 
417  UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
418  cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
419 
420  // Displays a control that allows the user to choose picture or
421  // movie capture, if both are available:
422  cameraUI.mediaTypes =
423  [UIImagePickerController availableMediaTypesForSourceType:
424  UIImagePickerControllerSourceTypePhotoLibrary];
425 
426  // Hides the controls for moving & scaling pictures, or for
427  // trimming movies. To instead show the controls, use YES.
428  cameraUI.allowsEditing = YES;
429 
430  cameraUI.delegate = delegate;
431 
432  [controller presentViewController: cameraUI animated: YES completion:^(void) {
433  NSLog(@"CameraSnap.starPictureController");
434  }];
435  return YES;
436 }
437 #pragma mark <U3ParameterTableDelegate>
438 - (void) updatedParameterArray:(NSArray *)menuItemArray
439 {
440  NSLog(@"CameraSnapViewController.updatedParameterArray: ??");
441 }
442 - (float) resizeParameterFrame
443 {
444  /*
445  CGRect totalFrame = self.view.frame;
446  float leftMargin = 0.02*totalFrame.size.width;
447  float topMargin = 48;
448  float portalHeight = 280;
449  float controlWidth = 0.96*totalFrame.size.width;
450  */
451 
452  float parameterPanelHeight = [self parameterViewHeight];
453  // CGRect menuItemParametersFrame = CGRectMake(leftMargin, topMargin + portalHeight +4, controlWidth, parameterPanelHeight);
454  // [parameterTableView setFrame:menuItemParametersFrame];
455  return parameterPanelHeight;
456 }
458 - (void) didSelectParameterRow: (NSIndexPath *)iPath
459 {
460  // editParameterIndexRow = [iPath row];
461 }
462 // - (void) didSelectGalleryMenuItem:(MenuItem *) menuItem
463 // {
464 // NSLog(@"CameraSnapView.didSelectGalleryMenuItem");
465 // }
467 - (float) parameterViewHeight
468 {
469  CGRect totalFrame = self.view.frame;
470  float paramLineHgt = 42.0 / totalFrame.size.height;
471  // float paramLineHgt = 30;
472 
473  // NSLog(@"CameraSnapViewController.parameterViewHeight: nutrComp count: %ld",[focusUSDAparameterArray count]);
474 
475  float suggestedParamHeight = paramLineHgt * [usdaParameterArray count]; // = 42;
476  if (0.28 < suggestedParamHeight) {
477  suggestedParamHeight = 0.28;
478  }
479  // float parameterViewHeight = totalFrame.size.height * 0.30;
480  float parameterViewHeight = totalFrame.size.height * suggestedParamHeight;
481  // NSLog(@"CameraSnapViewController.parameterViewHeight: parameterViewHeight: %3.2f", parameterViewHeight);
482  return parameterViewHeight;
483 }
485 - (void) popPushController:(UIViewController *)rebuildVC
486 {
487  NSString *rebuildClassStr = NSStringFromClass([rebuildVC class]);
488  if ([rebuildClassStr isEqualToString:@"Y3GmailViewController"]) {
489  UIViewController *popped = [self.navigationController popViewControllerAnimated:NO];
490  NSLog(@"CameraSnapViewController.popPushController: %@", NSStringFromClass([popped class]));
491 
492  // NSLog(@"CameraSnapViewController.popPushController: HIT MESSAGES BUTTON");
493  [self messagesButtonHit];
494  }
495 }
496 
498 - (void)didReceiveMemoryWarning {
499  [super didReceiveMemoryWarning];
500  // Dispose of any resources that can be recreated.
501 }
503 - (void) regenerateLoginView
504 {
505  [loginControlbar removeFromSuperview];
506  [NSThread sleepForTimeInterval:5.0]; // seconds
508 }
509 /*
510 #pragma mark - Navigation
511 
512 // In a storyboard-based application, you will often want to do a little preparation before navigation
513 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
514  // Get the new view controller using [segue destinationViewController].
515  // Pass the selected object to the new view controller.
516 }
517 */
518 /*
519  CGRect cameraImageFrame = CGRectMake(totalFrame.size.width * 0.20,
520  totalFrame.size.height * 0.14,
521  totalFrame.size.width * 0.6,
522  totalFrame.size.height * 0.36);
523  cameraImage = [[UIImageView alloc] initWithFrame:cameraImageFrame];
524  [self.view addSubview:cameraImage];
525 
526  CGRect addDataButtonFrame = CGRectMake(totalFrame.size.width * 0.06,
527  totalFrame.size.height * 0.68,
528  totalFrame.size.width * 0.88,
529  totalFrame.size.height * 0.08);
530  addDataButton = [Z5DataController makeGradientButton:@"Nutrition input"
531  withFrame:addDataButtonFrame
532  andFont:nil];
533  [addDataButton addTarget:self
534  action:@selector(fireDataCollectScreen)
535  forControlEvents:UIControlEventTouchUpInside];
536  [self.view addSubview:addDataButton];
537  [addDataButton setHidden:YES];
538  */
539 
540 @end
NSString * produceRandomkey()
UIImagePickerController * mealEntryReviewPicker
float topOfLoginControlbar
UIButton * buttonDrive
WKWebView * webView
UIButton * addDataButton
MenuItem * menuItem
instancetype sharedInstance()
UIButton * buttonUSDA
NSString * instructionHTML
id< U7ParamEditControlDelegate > delegate
UIButton * buttonPictures
UIButton * buttonWeb
UIButton * buttonCammera
Singleton interface to both core and remote data sources.
UIButton * buttonMessages
UIColor * lightGreenColor
UIButton * makeGradientButton:withFrame:andFont:(NSString *caption, [withFrame] CGRect buttonFrame, [andFont] UIFont *buttonFont)
UIImageView * cameraImage
NSMutableArray * usdaParameterArray
MenuItem * findOrCreateMenuItemWithRandomkey:(NSString *randomkey)
BOOL startCameraControllerFromViewController:usingDelegate:(UIViewController *controller, [usingDelegate] id< UINavigationControllerDelegate, UIImagePickerControllerDelegate > delegate)
Fetch all marked emails into the local database, and display a list.
V7GoogleLoginControlbar * loginControlbar
BOOL startPictureControllerFromViewController:usingDelegate:(UIViewController *controller, [usingDelegate] id< UIImagePickerControllerDelegate, UINavigationControllerDelegate > delegate)