Today's Menu  Portugal
journal and plan nutrition
B2CalendarViewController.m
Go to the documentation of this file.
1 //
2 // B2CalendarViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 4/28/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 #import "Z5DataController.h"
11 #import "Y3GradientLabel.h"
13 #import "NSDate+CalendarView.h"
16 
17 @interface B2CalendarViewController ()
18 
19 @end
20 
39 @implementation B2CalendarViewController {
40  NSDateFormatter *mDateFormatter;
41  // CalendarView *calendarView;
43  UIButton *dateHeaderButton;
44 }
45 
46 UIButton *bckMonthButton;
47 UIButton *fwdMonthButton;
48 
49 - (instancetype) init
50 {
51  self = [super init];
52  if (self) {
53  mDateFormatter = [[NSDateFormatter alloc] init];
54  }
55  return self;
56 }
57 - (void)viewDidLoad {
58  [super viewDidLoad];
59  // Do any additional setup after loading the view.
60  self.title = @"Log";
61 
62  NSString *imageName = @"softGreenBackgroud.png";
63  self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imageName]]];
64 
65  [self orientLoadUI];
66 
67  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
68  [[NSNotificationCenter defaultCenter] addObserver:self
69  selector:@selector(deviceOrientationDidChange:)
70  name: UIDeviceOrientationDidChangeNotification
71  object: nil];
72 
73 }
77 - (void)deviceOrientationDidChange:(NSNotification *)notification {
78  // NSLog(@"B2CalendarViewController.deviceOrientationDidChange: %@", notification);
79  [self orientAppearUI];
80 }
81 
82 - (void) orientLoadUI {
83  // NSLog(@"B2CalendarViewController.orientLoadUI");
84  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
85  switch (orientation)
86  {
87  case UIDeviceOrientationPortrait:
88  case UIDeviceOrientationFaceUp:
89  {
90  [self viewDidLoadPortrait];
91  }
92  break;
93  case UIDeviceOrientationLandscapeLeft:
94  case UIDeviceOrientationLandscapeRight:
95  {
96  [self viewDidLoadLandscape];
97  }
98  break;
99  default:
100  {
101  NSLog(@"B2CalendarViewController.orientLoadUI: unk orrientation");
102  }
103  break;
104  }
105 }
106 - (void) viewDidLoadPortrait
107 {
108  CGSize totalFrameSize = self.view.frame.size;
109  CGRect topNavigationFrame = [[[self navigationController] navigationBar] frame];
110  float topOfPageF = topNavigationFrame.origin.y + topNavigationFrame.size.height;
111  topOfPageF +=2.0;
112 
113  float dirButtonWidth = 60.0;
114  float centerLabelWidth = totalFrameSize.width - (2 * dirButtonWidth) - 8.0;
115  float buttonHeight = 46.0;
116  UIColor *gradientBandColor = [[Z5DataController sharedInstance] wheatColor]; // [UIColor blueColor];
117  UIColor *buttonBckgrndColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.60 alpha:0.9];
118 
119  float bckMonthButtonLeftEdge = 4.0f;
120  CGRect bckMonthFrame = CGRectMake(bckMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
121  bckMonthButton = [[UIButton alloc] initWithFrame:bckMonthFrame];
122  [bckMonthButton setBackgroundColor:buttonBckgrndColor];
123  [bckMonthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
124  [bckMonthButton addGradientFace:gradientBandColor];
125  [bckMonthButton setTitle:@"<<" forState:UIControlStateNormal];
126  [bckMonthButton addTarget:self action:@selector(didClickPageBck) forControlEvents:UIControlEventTouchUpInside];
127  [self.view addSubview:bckMonthButton];
128 
129  float dateHeaderButtonLeftEdge = dirButtonWidth + 8.0f;
130  float labelWidth = centerLabelWidth-4.0;
131  CGRect dateHeaderButtonFrame = CGRectMake(dateHeaderButtonLeftEdge, topOfPageF, labelWidth, buttonHeight);
132  dateHeaderButton = [[UIButton alloc] initWithFrame:dateHeaderButtonFrame];
133  [dateHeaderButton setBackgroundColor:[UIColor whiteColor]];
134  [dateHeaderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
135  [dateHeaderButton addGradientFace:gradientBandColor];
136  [dateHeaderButton addTarget:self action:@selector(didClickMonthHeader) forControlEvents:UIControlEventTouchUpInside];
137  [self.view addSubview:dateHeaderButton];
138 
139  float fwdMonthButtonLeftEdge =totalFrameSize.width - (dirButtonWidth+4.0);
140  CGRect fwdMonthFrame =CGRectMake(fwdMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
141  fwdMonthButton = [[UIButton alloc] initWithFrame:fwdMonthFrame];
142  [fwdMonthButton setBackgroundColor:[UIColor whiteColor]];
143  [fwdMonthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
144  [fwdMonthButton addGradientFace:gradientBandColor];
145  [fwdMonthButton setTitle:@">>" forState:UIControlStateNormal];
146  [fwdMonthButton addTarget:self action:@selector(didClickPageFwd) forControlEvents:UIControlEventTouchUpInside];
147  [self.view addSubview:fwdMonthButton];
148 
149  topOfPageF += buttonHeight;
150 
151  calendarView = [[B2NutritionCalendarView alloc] initWithPosition:6.0 y:topOfPageF];
152  [calendarView setBgColor:[[Z5DataController sharedInstance] wheatColor]];
153  [calendarView setPreferredWeekStartIndex:0];
154  [calendarView setCalendarDelegate:self];
155  // [calendarView setCurrentDate:[NSDate currentDateInSystemTimezone]];
156 
157  float calendarWidth = totalFrameSize.width-12;
158  float calendarHeight = totalFrameSize.height-12-topOfPageF;
159  CGRect calendarFrame = CGRectMake(6, topOfPageF+6, calendarWidth, totalFrameSize.height-12-topOfPageF);
160  [calendarView setFrame:calendarFrame];
161 
162  float dayCellWidth = calendarWidth/8;
163  float dayCellHeight = calendarHeight/6;
164  [calendarView setDayCellWidth:dayCellWidth];
165  [calendarView setDayCellHeight:dayCellHeight];
166  [calendarView refresh];
167  [self.view addSubview:calendarView];
168 }
169 - (void) viewDidLoadLandscape
170 {
171  CGSize totalFrameSize = self.view.frame.size;
172  CGRect topNavigationFrame = [[[self navigationController] navigationBar] frame];
173  float topOfPageF = topNavigationFrame.origin.y + topNavigationFrame.size.height;
174  topOfPageF +=2.0;
175 
176  float dirButtonWidth = 60.0;
177  float centerLabelWidth = totalFrameSize.width - (2 * dirButtonWidth) - 8.0;
178  float buttonHeight = 46.0;
179  UIColor *gradientBandColor = [[Z5DataController sharedInstance] wheatColor]; // [UIColor blueColor];
180  UIColor *buttonBckgrndColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.60 alpha:0.9];
181 
182 
183  float bckMonthButtonLeftEdge = 4.0f;
184  CGRect bckMonthFrame =CGRectMake(bckMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
185  bckMonthButton = [[UIButton alloc] initWithFrame:bckMonthFrame];
186  [bckMonthButton setBackgroundColor:buttonBckgrndColor];
187  [bckMonthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
188  [bckMonthButton addGradientFace:gradientBandColor];
189  [bckMonthButton setTitle:@"<<" forState:UIControlStateNormal];
190  [bckMonthButton addTarget:self action:@selector(didClickPageBck) forControlEvents:UIControlEventTouchUpInside];
191  [self.view addSubview:bckMonthButton];
192 
193  float dateHeaderButtonLeftEdge = dirButtonWidth + 8.0f;
194  float labelWidth = centerLabelWidth-4.0;
195  CGRect dateHeaderButtonFrame = CGRectMake(dateHeaderButtonLeftEdge, topOfPageF, labelWidth, buttonHeight);
196  dateHeaderButton = [[UIButton alloc] initWithFrame:dateHeaderButtonFrame];
197  [dateHeaderButton setBackgroundColor:[UIColor whiteColor]];
198  [dateHeaderButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
199  [dateHeaderButton addGradientFace:gradientBandColor];
200  [dateHeaderButton addTarget:self action:@selector(didClickMonthHeader) forControlEvents:UIControlEventTouchUpInside];
201  [self.view addSubview:dateHeaderButton];
202 
203  float fwdMonthButtonLeftEdge =totalFrameSize.width - (dirButtonWidth+4.0);
204  CGRect fwdMonthFrame =CGRectMake(fwdMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
205  fwdMonthButton = [[UIButton alloc] initWithFrame:fwdMonthFrame];
206  [fwdMonthButton setBackgroundColor:[UIColor whiteColor]];
207  [fwdMonthButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
208  [fwdMonthButton addGradientFace:gradientBandColor];
209  [fwdMonthButton setTitle:@">>" forState:UIControlStateNormal];
210  [fwdMonthButton addTarget:self action:@selector(didClickPageFwd) forControlEvents:UIControlEventTouchUpInside];
211  [self.view addSubview:fwdMonthButton];
212 
213  //topOfPageF += 4.0;
214  topOfPageF += buttonHeight;
215 
216  calendarView = [[B2NutritionCalendarView alloc] initWithPosition:6.0 y:topOfPageF];
217  [calendarView setBgColor:[[Z5DataController sharedInstance] wheatColor]];
218  [calendarView setPreferredWeekStartIndex:0];
219  [calendarView setCalendarDelegate:self];
220  // [calendarView setCurrentDate:[NSDate currentDateInSystemTimezone]];
221 
222  float calendarWidth = totalFrameSize.width-12;
223  float calendarHeight = totalFrameSize.height-12-topOfPageF;
224  CGRect calendarFrame = CGRectMake(6, topOfPageF+6, calendarWidth, totalFrameSize.height-12-topOfPageF);
225  [calendarView setFrame:calendarFrame];
226 
227  float dayCellWidth = calendarWidth/8;
228  float dayCellHeight = calendarHeight/6;
229  [calendarView setDayCellWidth:dayCellWidth];
230  [calendarView setDayCellHeight:dayCellHeight];
231  [calendarView refresh];
232  [self.view addSubview:calendarView];
233 }
234 
235 - (void) orientAppearUI {
236  NSLog(@"B2CalendarViewController.orientAppearUI");
237  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
238  switch (orientation)
239  {
240  case UIDeviceOrientationPortrait:
241  case UIDeviceOrientationFaceUp:
242  {
243  [self viewDidAppearPortrait];
244  }
245  break;
246  case UIDeviceOrientationLandscapeLeft:
247  case UIDeviceOrientationLandscapeRight:
248  {
249  [self viewDidAppearLandscape];
250  }
251  break;
252  default:
253  {
254  NSLog(@"B2CalendarViewController.viewDidLoad: unk orrientation");
255  }
256  break;
257  }
258 }
259 - (void) viewDidAppearPortrait
260 {
261  CGSize totalFrameSize = self.view.frame.size;
262  CGRect topNavigationFrame = [[[self navigationController] navigationBar] frame];
263  float topOfPageF = topNavigationFrame.origin.y + topNavigationFrame.size.height;
264  topOfPageF +=2.0;
265 
266  float dirButtonWidth = 60.0;
267  float centerLabelWidth = totalFrameSize.width - (2 * dirButtonWidth) - 8.0;
268  float buttonHeight = 46.0;
269 
270  float bckMonthButtonLeftEdge = 4.0f;
271  CGRect bckMonthFrame =CGRectMake(bckMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
272  [bckMonthButton setFrame:bckMonthFrame];
273 
274  float dateHeaderButtonLeftEdge = dirButtonWidth + 8.0f;
275  float labelWidth = centerLabelWidth-4.0;
276  CGRect dateHeaderButtonFrame = CGRectMake(dateHeaderButtonLeftEdge, topOfPageF, labelWidth, buttonHeight);
277  [dateHeaderButton setFrame:dateHeaderButtonFrame];
278 
279  float fwdMonthButtonLeftEdge =totalFrameSize.width - (dirButtonWidth+4.0);
280  CGRect fwdMonthFrame =CGRectMake(fwdMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
281  [fwdMonthButton setFrame:fwdMonthFrame];
282 
283  topOfPageF += buttonHeight;
284 
285 // calendarView = [[B2NutritionCalendarView alloc] initWithPosition:6.0 y:topOfPageF];
286 // [calendarView setBgColor:[[Z5DataController sharedInstance] wheatColor]];
287 // [calendarView setPreferredWeekStartIndex:0];
288 // [calendarView setCalendarDelegate:self];
289 // // [calendarView setCurrentDate:[NSDate currentDateInSystemTimezone]];
290 //
291  float calendarWidth = totalFrameSize.width-12;
292  float calendarHeight = totalFrameSize.height-12-topOfPageF;
293  CGRect calendarFrame = CGRectMake(6, topOfPageF+6, calendarWidth, totalFrameSize.height-12-topOfPageF);
294  [calendarView setFrame:calendarFrame];
295 
296  float dayCellWidth = calendarWidth/8;
297  float dayCellHeight = calendarHeight/6;
298  [calendarView setDayCellWidth:dayCellWidth];
299  [calendarView setDayCellHeight:dayCellHeight];
300  [calendarView refresh];
301 }
302 - (void) viewDidAppearLandscape
303 {
304  CGSize totalFrameSize = self.view.frame.size;
305  CGRect topNavigationFrame = [[[self navigationController] navigationBar] frame];
306  float topOfPageF = topNavigationFrame.origin.y + topNavigationFrame.size.height;
307  topOfPageF +=2.0;
308 
309  float dirButtonWidth = 60.0;
310  float centerLabelWidth = totalFrameSize.width - (2 * dirButtonWidth) - 8.0;
311  float buttonHeight = 46.0;
312 
313  float bckMonthButtonLeftEdge = 4.0f;
314  CGRect bckMonthFrame =CGRectMake(bckMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
315  [bckMonthButton setFrame:bckMonthFrame];
316 
317  float dateHeaderButtonLeftEdge = dirButtonWidth + 8.0f;
318  float labelWidth = centerLabelWidth-4.0;
319  CGRect dateHeaderButtonFrame = CGRectMake(dateHeaderButtonLeftEdge, topOfPageF, labelWidth, buttonHeight);
320  [dateHeaderButton setFrame:dateHeaderButtonFrame];
321 
322  float fwdMonthButtonLeftEdge =totalFrameSize.width - (dirButtonWidth+4.0);
323  CGRect fwdMonthFrame =CGRectMake(fwdMonthButtonLeftEdge, topOfPageF, dirButtonWidth, buttonHeight);
324  [fwdMonthButton setFrame:fwdMonthFrame];
325 
326  //topOfPageF += 4.0;
327  topOfPageF += buttonHeight;
328 
329 // calendarView = [[B2NutritionCalendarView alloc] initWithPosition:6.0 y:topOfPageF];
330 // [calendarView setBgColor:[[Z5DataController sharedInstance] wheatColor]];
331 // [calendarView setPreferredWeekStartIndex:0];
332 // [calendarView setCalendarDelegate:self];
333 
334  float calendarWidth = totalFrameSize.width-12;
335  float calendarHeight = totalFrameSize.height-12-topOfPageF;
336  CGRect calendarFrame = CGRectMake(6, topOfPageF+6, calendarWidth, totalFrameSize.height-12-topOfPageF);
337  [calendarView setFrame:calendarFrame];
338 
339  float dayCellWidth = calendarWidth/8;
340  float dayCellHeight = calendarHeight/6;
341  [calendarView setDayCellWidth:dayCellWidth];
342  [calendarView setDayCellHeight:dayCellHeight];
343  [calendarView refresh];
344 }
345 
346 - (void) viewWillAppear:(BOOL)animated
347 {
348  [self orientAppearUI];
349 
350  NSString *monthLabel = [self produceDateLabelText];
351  [dateHeaderButton setTitle:monthLabel forState:UIControlStateNormal];
352 
353  [calendarView refreshCalendar];
354 }
355 #pragma mark - CalendarViewDelegate
356 
357 // @required
358 - (void)didChangeCalendarDate:(NSDate *)date
359 {
360  NSLog(@"B2CalendarViewController.didChangeCalendarDate: date: %@", date);
361 
362 }
363 
364 // @optional
365 - (void)didChangeCalendarDate:(NSDate *)date withType:(NSInteger)type withEvent:(NSInteger)event
366 {
367  NSLog(@"B2CalendarViewController.didChangeCalendarDate: %@ withType: %ld withEvent: %ld", date, (long)type, (long)event);
368  switch(event) {
369  case 0:
370  NSLog(@"B2CalendarViewController.didChangeCalendarDate: chg adv/rev by month");
371  break;
372  case 1:
373  {
374  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
375 
376  // NSDate *localDate = [localData toLocalTime:date];
377 
378  NSArray<Meal *> *dayMeals = [localData findMealsWithDate: date];
379 
380  if (0 < [dayMeals count]) {
381  Y4MenuPostViewController *menuPostVC = [[Y4MenuPostViewController alloc] init];
382  [menuPostVC setPostedMeals:dayMeals];
383  [menuPostVC setPostingDate:date];
384 
385  NSLog(@"B2CalendarViewController.didChangeCalendarDate");
386 
387  [self.navigationController pushViewController:menuPostVC animated:YES];
388  }
389  }
390 
391  break;
392  }
393 }
394 - (void)didDoubleTapCalendar:(NSDate *)date withType:(NSInteger)type
395 {
396  NSLog(@"B2CalendarViewController.didDoubleTapCalendar");
397 
398 }
399 - (void)didSelectRangeForStartDate:(NSDate *) startDate andEndDate:(NSDate *) endDate
400 {
401  [mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
402  [mDateFormatter setTimeStyle:NSDateFormatterShortStyle];
403 
404  NSLog(@"B2CalendarViewController.didSelectRangeForStartDate: %@ and EndDate: %@", [mDateFormatter stringFromDate:startDate], [mDateFormatter stringFromDate:endDate]);
405 }
406 
407 #pragma mark - Local controls
408 
409 - (NSString *) produceDateLabelText
410 {
411 // NSCalendar *gregorian = [[NSCalendar alloc]
412 // initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
413 // NSDateComponents *dateComponents =
414 // [gregorian components:(NSCalendarUnitMonth|NSCalendarUnitYear) fromDate:[calendarView currentDate]];
415 // NSInteger monthId = [dateComponents month];
416 // NSInteger yearId = [dateComponents year];
417 // NSString *monthLabel = [[NSString alloc] initWithFormat:@" - %ld/%ld -", (long)monthId, (long)yearId];
418 
419 // [mDateFormatter setDateStyle:NSDateFormatterLongStyle];
420 // [mDateFormatter setTimeStyle:NSDateFormatterNoStyle];
421 // NSString *monthLabel = [mDateFormatter stringFromDate:[calendarView currentDate]];
422 
423  //NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
424  mDateFormatter.dateFormat = @"MMM yyyy";
425  NSString *monthLabel = [mDateFormatter stringFromDate:[calendarView currentDate]];
426 
427  return monthLabel;
428 }
429 - (void) didClickMonthHeader
430 {
432  [self.navigationController pushViewController:menuPostVC animated:YES];
433 }
434 - (void) didClickPageBck
435 {
436  [calendarView rewindCalendarContents];
437  NSString *monthLabel = [self produceDateLabelText];
438  [dateHeaderButton setTitle:monthLabel forState:UIControlStateNormal];
439 
440  [calendarView refreshCalendar];
441 }
442 
443 - (void) didClickPageFwd
444 {
445  [calendarView advanceCalendarContents];
446  NSString *monthLabel = [self produceDateLabelText];
447  [dateHeaderButton setTitle:monthLabel forState:UIControlStateNormal];
448 }
449 
450 #pragma mark - Safety
451 
452 - (void)didReceiveMemoryWarning {
453  [super didReceiveMemoryWarning];
454  // Dispose of any resources that can be recreated.
455 }
456 
457 /*
458 #pragma mark - Navigation
459 
460 // In a storyboard-based application, you will often want to do a little preparation before navigation
461 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
462  // Get the new view controller using [segue destinationViewController].
463  // Pass the selected object to the new view controller.
464 }
465 */
466 
467 @end
instancetype sharedInstance()
NSDateFormatter * mDateFormatter
NSArray< Meal * > * findMealsWithDate:(NSDate *findDate)
UIButton * dateHeaderButton
B2NutritionCalendarView * calendarView
UIButton * fwdMonthButton
Singleton interface to both core and remote data sources.
UIButton * bckMonthButton