Today's Menu  Portugal
journal and plan nutrition
Y3MailDisplayViewController.m
Go to the documentation of this file.
1 //
2 // Y3MailDisplayViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 5/22/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import <GTLRGmail.h>
10 #import "Z5DataController.h"
14 
15 @interface Y3MailDisplayViewController ()
16 
17 @end
18 
19 @implementation Y3MailDisplayViewController {
20 
21  EmailMessage *focusEmailMessage;
22  NSMutableArray *outstandingTickets;
23  NSDateFormatter *RFC3339DateFormatter;
24  NSDateFormatter *msgNearDateFormatter;
25  NSDateFormatter *dspNearDateFormatter;
26  UIImageView *imageView;
27  UITextView *msgPlainText;
28  UIButton *itemRecipeButton;
29  UIButton *itemUSDAButton;
31 }
32 
33 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
34 {
35  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
36  if (self) {
37  // Custom initialization
38  self.title = @"Focus";
39  UIColor *ivory = [UIColor colorWithRed:0.9297f green:0.9297f blue:0.8784f alpha:1.0f];
40  [self.view setBackgroundColor:ivory];
41  outstandingTickets = [[NSMutableArray alloc] init];
42 
43  RFC3339DateFormatter = [[NSDateFormatter alloc] init];
44  RFC3339DateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
45  RFC3339DateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
46  RFC3339DateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
47  msgNearDateFormatter = [[NSDateFormatter alloc] init];
48  msgNearDateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
49  msgNearDateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss ZZZZZ";
50  msgNearDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8];
51  dspNearDateFormatter = [[NSDateFormatter alloc] init];
52  [dspNearDateFormatter setDateStyle:NSDateFormatterMediumStyle];
53  [dspNearDateFormatter setTimeStyle:NSDateFormatterShortStyle];
54 
55 
56  }
57  return self;
58 }
59 - (void)viewDidLoad {
60  [super viewDidLoad];
61  // Do any additional setup after loading the view.
62 
63  NSLog(@"\f\n"); // does not new-page
64  NSLog(@"GmailDisplayController.viewDidLoad: focus: %@", [self focusEmailMsg]);
65  // NSLog(@"GmailDisplayController.viewDidLoad: messageUID: %@", [self focusEmailUniqueID]);
66 
67  CGRect totalFrame = self.view.frame;
68  CGRect topNavigationFrame = [[[self navigationController] navigationBar] frame];
69  float topOfPageF = topNavigationFrame.origin.y + topNavigationFrame.size.height;
70 
71  topOfPageF = 60.0;
72  float tailHeightF = topOfPageF + 8.0;
73 
74  float buttonBarFrameHeight = 44.0;
75  float buttonWidth = totalFrame.size.width/3.0 - 6.0;
76  float buttonLeft = 6.0;
77 
78  CGRect itemRecipeButtonFrame = CGRectMake(buttonLeft, tailHeightF, buttonWidth, buttonBarFrameHeight);
79 // itemRecipeButton = [[UIButton alloc] initWithFrame:itemRecipeButtonFrame];
80 // [itemRecipeButton addTarget:self action:@selector(showItemRecipe) forControlEvents:UIControlEventTouchUpInside];
81 // // [itemRecipeButton setTitle:@"recipe" forState:UIControlStateNormal];
82 // // [itemRecipeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
83 // [itemRecipeButton setBackgroundColor:[[Z5DataController sharedInstance] wheatColor]];
84 // [itemRecipeButton setHidden:NO];
85 // [Z5DataController labelizeButton:itemRecipeButton withText:@"recipe"];
86  UIFont *messageDisplayButtonFont = [UIFont fontWithName:@"Georgia-Bold" size:20.0];
87  itemRecipeButton = [Z5DataController makeGradientButton:@"recipe" withFrame:itemRecipeButtonFrame andFont:messageDisplayButtonFont];
88  [itemRecipeButton addTarget:self action:@selector(showItemRecipe) forControlEvents:UIControlEventTouchUpInside];
89 
90 
91  [self.view addSubview:itemRecipeButton];
92  buttonLeft += (buttonWidth + 2.0);
93 
94  CGRect itemUSDAButtonFrame = CGRectMake(buttonLeft, tailHeightF, buttonWidth, buttonBarFrameHeight);
95 // itemUSDAButton = [[UIButton alloc] initWithFrame:itemUSDAButtonFrame];
96 // [itemUSDAButton addTarget:self action:@selector(showItemUSDA) forControlEvents:UIControlEventTouchUpInside];
97 // [itemUSDAButton setTitle:@"usda" forState:UIControlStateNormal];
98 // [itemUSDAButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
99 // [itemUSDAButton setBackgroundColor:[UIColor grayColor]];
100 // [itemUSDAButton setHidden:NO];
101 
102  itemUSDAButton = [Z5DataController makeGradientButton:@"usda" withFrame:itemUSDAButtonFrame andFont:messageDisplayButtonFont];
103  [itemUSDAButton addTarget:self action:@selector(showItemUSDA) forControlEvents:UIControlEventTouchUpInside];
104  [self.view addSubview:itemUSDAButton];
105  buttonLeft += (buttonWidth + 2.0);
106 
107 // NSLog(@"Y3MailDisplayViewController.viewDidLoad: add-item-button frame:\nX: %3.2f Y: %3.2f\nWid: %3.2f Hgt: %3.2f", itemAddToMenuButtonFrame.origin.x, itemAddToMenuButtonFrame.origin.y, itemAddToMenuButtonFrame.size.width, itemAddToMenuButtonFrame.size.height);
108 
109  CGRect itemAddToMenuButtonFrame = CGRectMake(buttonLeft, tailHeightF, buttonWidth, buttonBarFrameHeight);
110 // itemAddToMenuButton = [[UIButton alloc] initWithFrame:itemAddToMenuButtonFrame];
111 // [itemAddToMenuButton addTarget:self action:@selector(execAddMenuItem) forControlEvents:UIControlEventTouchUpInside];
112 // [itemAddToMenuButton setTitle:@"add item" forState:UIControlStateNormal];
113 // [itemAddToMenuButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
114 // [itemAddToMenuButton setBackgroundColor:[UIColor greenColor]];
115 // [itemAddToMenuButton setHidden:NO];
116 
117  itemAddToMenuButton = [Z5DataController makeGradientButton:@"add item" withFrame:itemAddToMenuButtonFrame andFont:messageDisplayButtonFont];
118  [itemAddToMenuButton addTarget:self action:@selector(execAddMenuItem) forControlEvents:UIControlEventTouchUpInside];
119  [self.view addSubview:itemAddToMenuButton];
120  tailHeightF += (buttonBarFrameHeight + 4.0);
121 
122  float imageViewFrameHeight = 300.0f;
123  CGRect imageViewFrame = CGRectMake(10.0f, tailHeightF, totalFrame.size.width - 20, imageViewFrameHeight);
124  imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
125  [self.view addSubview:imageView];
126  tailHeightF += (imageViewFrameHeight + 8.0);
127 
128  float plainTextFrameHeight = 180.0;
129  CGRect msgPlainTextFrame = CGRectMake(10.0f, tailHeightF, totalFrame.size.width - 20, plainTextFrameHeight);
130  msgPlainText = [[UITextView alloc] initWithFrame:msgPlainTextFrame];
131  [self.view addSubview:msgPlainText];
132  tailHeightF += (plainTextFrameHeight + 8.0);
133 
134 }
135 - (void) viewWillAppear:(BOOL)animated
136 {
137 
138 
139  NSLog(@"GmailDisplay.viewWillAppear: focus: %@", _focusEmailMsg);
140  //
141  NSString *caption = [_focusEmailMsg subject];
142  NSLog(@"GmailDisplayController.viewWillAppear: caption: %@", caption);
143 
144  UIImage *img = [[UIImage alloc] initWithData:[_focusEmailMsg photo]];
145  [imageView setImage: img];
146 
147  NSString *displayText = [[NSString alloc] initWithString:caption];
148  displayText = [displayText stringByAppendingString:@"\n"];
149  NSDate *messageDate = [_focusEmailMsg date];
150  NSString *msgDateStr = [dspNearDateFormatter stringFromDate:messageDate];
151  displayText = [displayText stringByAppendingString: msgDateStr];
152  displayText = [displayText stringByAppendingString:@"\nFrom: "];
153  displayText = [displayText stringByAppendingString:[_focusEmailMsg from]];
154  displayText = [displayText stringByAppendingString:@"\n"];
155  if (nil == [_focusEmailMsg bodytext]) {
156  displayText = [displayText stringByAppendingString:@"nil bodytext"];
157  } else {
158  displayText = [displayText stringByAppendingString:[_focusEmailMsg bodytext]];
159  }
160  [msgPlainText setText:displayText];
161 }
162 
163 - (void) showItemRecipe
164 {
165  // this does init and viewDidLoad
167  [vc2 setFocusEmailMsg:_focusEmailMsg];
168  // this does viewWillAppear
169  // NSLog(@"GmailViewController.didSelectRow: vc.focusEmailMsg: %@", vc2.focusEmailMsg);
170  [[self navigationController] pushViewController:vc2 animated:YES];
171 }
172 - (void) showItemUSDA
173 {
174  // this does init and viewDidLoad
176  [vc2 setFocusEmailMsg:_focusEmailMsg];
177  // this does viewWillAppear
178  // NSLog(@"GmailViewController.didSelectRow: vc.focusEmailMsg: %@", vc2.focusEmailMsg);
179  [[self navigationController] pushViewController:vc2 animated:YES];
180 }
185 - (void) execAddMenuItem
186 {
187  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
188 
189  NSString *remoteUniqueIID = [_focusEmailMsg remoteUniqueIID];
190  NSLog(@"Y3MailDisplayViewController.execAddMenuItem: remoteUniqueIID: %@", remoteUniqueIID);
191  // MenuItem *newMenuItem = [localData findOrCreateMenuItemWithUID:uidnum];
192  MenuItem *newMenuItem = [localData findOrCreateMenuItemWithFileIdentification:remoteUniqueIID];
193 
194  [_focusEmailMsg setRandomkey:remoteUniqueIID];
195 
196  // update data base item (with ==uid) with message photo and name
197  newMenuItem.photo = [_focusEmailMsg photo];
198  newMenuItem.name = [[_focusEmailMsg subject] stringByReplacingOccurrencesOfString:@"(Nutrition Journal Information)" withString:@""];
199  newMenuItem.galleryNumber = [NSNumber numberWithInt:1]; // home gallery
200  newMenuItem.galleryPositionNo = [NSNumber numberWithLong:[localData getPrevGalleryPositionIdx]];
201 
202  [localData saveContext];
203  [self.navigationController popViewControllerAnimated: YES];
204 }
205 
215 - (void)logMessagePart:(GTLRGmail_MessagePart *)msgPt underCaption:(NSString *)caption
216 {
217  if ([NSStringFromClass([msgPt.body class]) isEqualToString:@"GTLRGmail_MessagePartBody"]) {
218  NSLog(@"Y3MailDisplayController/log.%@\n body size: %ld\n filename: %@\n mimeType: %@\n partID %@\n header count: %lu\n parts count: %lu",caption,
219  [msgPt.body.size integerValue],
220  msgPt.filename,
221  msgPt.mimeType,
222  msgPt.partId,
223  [msgPt.headers count],
224  [msgPt.parts count]);
225 
226  } else {
227  NSLog(@"display gmail:%@\n body: %@\n filename: %@\n mimeType: %@\n partID %@\n header count: %lu\n parts count:%luu",caption,
228  msgPt.body,
229  msgPt.filename,
230  msgPt.mimeType,
231  msgPt.partId,
232  (unsigned long)[msgPt.headers count],
233  [msgPt.parts count]);
234  }
235 }
236 
237 - (void)didReceiveMemoryWarning {
238  [super didReceiveMemoryWarning];
239  // Dispose of any resources that can be recreated.
240 }
241 
242 /*
243 #pragma mark - Navigation
244 
245 // In a storyboard-based application, you will often want to do a little preparation before navigation
246 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
247  // Get the new view controller using [segue destinationViewController].
248  // Pass the selected object to the new view controller.
249 }
250 */
251 
252 @end
UIButton * itemRecipeButton
UIImageView * imageView
NSDateFormatter * msgNearDateFormatter
UIButton * itemAddToMenuButton
instancetype sharedInstance()
UITextView * msgPlainText
NSNumber * galleryPositionNo
UIButton * itemUSDAButton
NSDateFormatter * RFC3339DateFormatter
NSDateFormatter * dspNearDateFormatter
NSMutableArray * outstandingTickets
Singleton interface to both core and remote data sources.
UIButton * makeGradientButton:withFrame:andFont:(NSString *caption, [withFrame] CGRect buttonFrame, [andFont] UIFont *buttonFont)
MenuItem * findOrCreateMenuItemWithFileIdentification:(NSString *googlefileid)