Today's Menu  Portugal
journal and plan nutrition
B2NutritionCalendarView.m
Go to the documentation of this file.
1 //
2 // NutritionCalendarView.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 6/14/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 #import "NSDate+CalendarView.h"
11 #import "NSString+CalendarView.h"
12 #import "Z5DataController.h"
13 
14 
15 
16 @implementation B2NutritionCalendarView : CalendarView
17 NSCalendar *gregorianCalendar;
18 NSArray<NSArray<Meal *> *> *mMeals;
19 - (instancetype)init
20 {
21  self = [super init];
22  gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
23  return self;
24 }
25 
27 {
28  mMeals = [self monthMeals];
29 }
30 
31 - (NSArray<NSArray<Meal *> *> *) monthMeals
32 {
33  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
34 
35  NSMutableArray *ret = [[NSMutableArray alloc] initWithCapacity:32];
36  NSMutableArray *mealsInDay;
37  for (int day=0; day<32; day++) {
38  mealsInDay = [[NSMutableArray alloc] init];
39  [ret addObject:mealsInDay];
40  }
41 
42  gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
43  NSInteger currentYear = [self getDateYear:[self currentDate]];
44  NSInteger currentMonth = [self getDateMonth:[self currentDate]];
45 
46  NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
47  [dateComponents setYear:currentYear];
48  [dateComponents setMonth:currentMonth];
49 
50  NSDate *calendarMonthDate = [gregorianCalendar dateFromComponents:dateComponents];
51  NSArray<Meal *> *monthMeals = [localData findMealsThisMonthDate:calendarMonthDate];
52 
53  NSInteger mealDay;
54  // set value of return array: value at index 0 unused
55  for (Meal *meal in monthMeals) {
56  if (nil == [meal consumeDate]) {
57  NSLog(@"NutritionCalendarView.monthMeals: no consume date: %@", meal);
58  } else {
59  mealDay = [self getDateDay:[meal consumeDate]];
60  NSLog(@"NutritionCalendarView.monthMeals: mealDay: %ld", (long)mealDay);
61  if ((0<mealDay)&&(31>=mealDay)){
62  [ret[mealDay] addObject:meal];
63  } else {
64  NSLog(@"NutritionCalendarView.monthMeals: ERROR?");
65  }
66  }
67  }
68 
69  return ret;
70 }
71 - (NSInteger) getDateDay:(NSDate *)date
72 {
73  assert(date);
74  NSInteger mealDay = [gregorianCalendar component:(NSCalendarUnitDay) fromDate:date];
75  return mealDay;
76 }
77 - (NSInteger) getDateMonth:(NSDate *)date
78 {
79  assert(date);
80  NSInteger currentMonth = [gregorianCalendar component:NSCalendarUnitMonth fromDate:date];
81  return currentMonth;
82 }
83 
84 - (NSInteger) getDateYear:(NSDate *)date
85 {
86  assert(date);
87 // NSInteger currentYear = [gregorianCalendar component:NSCalendarUnitYear fromDate:[self currentDate]];
88  NSInteger currentYear = [gregorianCalendar component:NSCalendarUnitYear fromDate:date];
89  return currentYear;
90 }
91 - (void)drawRect:(CGRect)rect
92 {
93  [super drawRect:rect];
94 
95  // NSLog(@"B2NutritionCalendarView.drawRect: self class: %@", NSStringFromClass([self class]));
96  // draws entire rectangle
97  // NSLog(@"B2NutritionCalendarView.drawRect: %3.2f/%3.2f @ %3.2f/%3.2f\n", rect.size.width, rect.size.height, rect.origin.x, rect.origin.y);
98  CGContextRef context = UIGraphicsGetCurrentContext();
99  NSDictionary *attributesBlack = [self generateAttributes:self.fontName
100  withFontSize:self.dayFontSize
101  withColor:self.fontColor
102  withAlignment:NSTextAlignmentFromCTTextAlignment(kCTTextAlignmentCenter)];
103 
104  NSMutableArray *rects = [super dayRects];
105 
106 
107  if (rects) {
108  for (CalendarViewRect *rect in rects) {
109  NSDictionary *attrs = attributesBlack;
110  CGRect rectText = rect.frame;
111  //dateComponents.day = 4;
112  // NSDate *calendarCellDate = [gregorianCalendar dateFromComponents:dateComponents];
113 
114 
115  NSArray *dayMeals = [mMeals objectAtIndex:rect.value];
116  NSInteger nMealsInDay = [dayMeals count];
117  if (0 < nMealsInDay) {
118  [self drawCircle:rect.frame toContext:&context withColor:[UIColor yellowColor]];
119  } else {
120  [self drawCircle:rect.frame toContext:&context withColor:[UIColor greenColor]];
121  }
122 
123  // attrs = attributesBlack;
124  [rect.str drawUsingRect:rectText withAttributes:attrs];
125  }
126  }
127 }
128 
129 @end
NSArray< Meal * > * findMealsThisMonthDate:(NSDate *findDate)
NSDate * currentDate
Definition: CalendarView.h:72
NSMutableArray * dayRects
Definition: CalendarView.h:69
NSArray< NSArray< Meal *> *> * monthMeals()
instancetype sharedInstance()
instancetype init()
Definition: CalendarView.m:138
NSInteger getDateMonth:(NSDate *date)
NSInteger currentMonth
Definition: CalendarView.m:67
void drawCircle:toContext:withColor:(CGRect rect, [toContext] CGContextRef *context, [withColor] UIColor *color)
Definition: CalendarView.m:795
NSInteger getDateDay:(NSDate *date)
NSArray< NSArray< Meal * > * > * mMeals
void drawRect:(CGRect rect)
Definition: CalendarView.m:645
Singleton interface to both core and remote data sources.
NSInteger currentYear
Definition: CalendarView.m:68
NSInteger getDateYear:(NSDate *date)
NSDictionary * generateAttributes:withFontSize:withColor:withAlignment:(NSString *fontName, [withFontSize] CGFloat fontSize, [withColor] UIColor *color, [withAlignment] NSTextAlignment textAlignment)