Today's Menu  Portugal
journal and plan nutrition
U3ParameterTableView.m
Go to the documentation of this file.
1 //
2 // U3ParameterTableView.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 9/7/16.
6 // Copyright © 2016 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
25 // #import <HealthKit/HealthKit.h>
26 
27 #import "Z5DataController.h"
28 #import "U3ParameterTableView.h"
30 // #import "Z6MenuPanoramaView.h"
31 #import "Z6ContainedView.h"
32 #import "Z5Colors.h"
33 
34 @implementation U3ParameterTableView
35 {
36  Boolean askedOnce;
37  id <Z6ContainedView> tableHead;
38 
39  NSMutableArray *focusUSDAparameterArray;
40  UIView<Z6ContainedView> *tableTail;
41 
42  NSDateFormatter *mDateFormatter;
43 
46 }
47 
49 
50 - (id) initWithFrame:(CGRect)frame
51  andComponents:(NSArray<NSDictionary *>*)components
52  inContainer:(id<U3ParameterTableDelegate>) container
53 {
54  NSLog(@"U3parameterTableView.init: #-components: %ld", (long)[components count]);
55 
56  self = [super initWithFrame:frame style:UITableViewStyleGrouped];
57  [self setBackgroundColor:[[Z5DataController sharedInstance] wheatColor]];
58  [self setDelegate:self];
59  self.dataSource = self;
60 
61  mDateFormatter = [[NSDateFormatter alloc] init];
62  [mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
63  [mDateFormatter setTimeStyle:NSDateFormatterShortStyle];
64 
65  [self setGivenParameters:[[NSMutableArray alloc] initWithArray:components]];
66  [self setGivenContainer:container];
67 
68  [self setScrollEnabled:YES];
69  paramHeaderHeight = 0.0f;
70  paramFooterHeight = 0.0f;
71  tableHead = nil;
72  tableTail = nil;
73 
74  return self;
75 }
76 
77 - (void) setViewForHead: (id <Z6ContainedView>)headView
78 {
79  tableHead = headView;
80  paramHeaderHeight = [headView heightRequirement];
81  NSLog(@"U3ParameterTableView.setViewForHead: paramHeaderHeight: %3.2f",paramHeaderHeight);
82 }
83 - (void) setViewForTail: (UIView<Z6ContainedView> *) tailView
84 {
85  tableTail = tailView;
86  paramFooterHeight = [tailView heightRequirement];
87  NSLog(@"U3ParameterTableView.setViewForTail: paramFooterHeight: %3.2f", paramFooterHeight);
88 }
89 
90 // called from NutritionViewController.viewWillAppear
91 // called from NutritionViewController.focusGallery
92 // called from MenuChoicesTableView.clearParameters
93 // called from MenuPostViewController.updateToAuthoirzationOK
94 - (void) update:(NSArray<NSDictionary *> *)components
95 {
96  NSLog(@"U3ParameterTableView.update: components: %@", components);
97 
98  [[self givenParameters] removeAllObjects];
99  if (components) {
100  for (NSDictionary *givenPrmDict in components) {
101  NSLog(@"U3ParameterTableView.update: given-parameter-dictionary: %@", givenPrmDict);
102  }
103  [[self givenParameters] addObjectsFromArray:components];
104  }
105  [self reloadData];
106 
107  NSMutableArray<NSDictionary *> *givParam = [self givenParameters];
108  [[self givenContainer] updatedParameterArray:givParam];
109  [[self givenContainer] resizeParameterFrame];
110 }
111 - (NSInteger) isListedAlready: (NSString *)checkCaption
112 {
113  NSInteger ret = -1;
114  int parameterIndex = 0;
115  for (NSDictionary *item in [self givenParameters]) {
116  NSString *componentType = [item objectForKey:[Z5LocalDataController componentNameKey]];
117  NSString *compoTypeClass = NSStringFromClass([componentType class]);
118  if ([componentType isEqualToString:checkCaption]) {
119  NSLog(@"Z6ParameterTableView.isListedAlready: yes, class: %@", compoTypeClass);
120  ret = parameterIndex;
121  break;
122  }
123  parameterIndex++;
124  }
125  return ret;
126 }
127 -(void) addParameter: (NSDictionary *)newComponentSpecDict
128 {
129  [[self givenParameters] addObject:newComponentSpecDict];
130 
131  [[self givenContainer] resizeParameterFrame];
132  [[self givenContainer] updatedParameterArray:[self givenParameters]];
133 }
134 -(void) updateParameter: (NSDictionary *)newComponentSpecDict
135 {
136  NSString *updateParamName = [newComponentSpecDict objectForKey:[Z5LocalDataController componentNameKey]];
137  for (NSUInteger index = 0; index < [[self givenParameters] count]; index++) {
138  NSDictionary *paramListed = [[self givenParameters] objectAtIndex:index];
139  NSString *listedParamName = [paramListed objectForKey:[Z5LocalDataController componentNameKey]];
140  if ([updateParamName isEqualToString:listedParamName]) {
141  [[self givenParameters] replaceObjectAtIndex:index withObject:newComponentSpecDict];
142  break;
143  }
144  }
145 
146  [[self givenContainer] resizeParameterFrame];
147  NSMutableArray<NSDictionary *> *givParam = [self givenParameters];
148  [[self givenContainer] updatedParameterArray:givParam];
149 }
150 
152 {
153  paramHeaderHeight = [tableHead heightRequirement];
154 
155  float ret = paramHeaderHeight;
156  NSInteger parameterCount = [[self givenParameters] count];
157  if (0 < parameterCount){
158  ret += 2 + (parameterCount * 44);
159  }
160  NSInteger tailHeightReqmt = [tableTail heightRequirement];
161  if (0 < tailHeightReqmt) {
162  ret += 2 + [tableTail heightRequirement];
163  }
164  NSLog(@"U3ParameterTableView.getViewHeightReqmt: head: %3.2f total: %3.2f", paramHeaderHeight, ret);
165  return ret;
166 }
168 {
169  if ( 0 < [[self givenParameters] count]) {
170  NSIndexPath *firstIndex = [NSIndexPath indexPathForRow:0 inSection:0];
171  [self scrollToRowAtIndexPath:firstIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
172  }
173 }
175 {
176  NSInteger nParams = [[self givenParameters] count];
177  if ( 0 < nParams) {
178  NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:nParams-1 inSection:0];
179  [self scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
180  }
181 }
183 {
184  NSLog(@"ParameterTablView.scrollForInstructionPanel: entry");
185  NSInteger nSections = [self numberOfSections];
186  NSLog(@" .. nSections: %ld", (long)nSections);
187  for (int j=0;j<nSections;j++) {
188  NSLog(@" ... nRows, section %d: %ld", j, (long)[self numberOfRowsInSection:j]);
189  }
190  NSInteger endSection = nSections - 1;
191  NSInteger nRows = [self numberOfRowsInSection:endSection];
192 
193  if (0 < nRows) {
194  NSInteger lastRowEndSection = nRows - 1;
195  NSLog(@" .. last index: S:%ld R:%ld", (long)endSection, (long)lastRowEndSection);
196  NSAssert(!(lastRowEndSection<0), @"empty last section");
197  NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:lastRowEndSection inSection:endSection];
198  [self scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
199  }
200  NSLog(@"ParameterTablView.scrollForInstructionPanel: exit");
201 // NSInteger nParams = [[self givenParameters] count];
202 // if ( 0 < nParams) {
203 // NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:nParams-1 inSection:0];
204 // [self scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
205 // }
206 }
207 - (NSUInteger) parameterCount
208 {
209  return [[self givenParameters] count];
210 }
211 
212 #pragma mark - UITableViewDelegate
213 - (void)tableView:(UITableView *)tableView
214 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
215 {
216  NSLog(@"U3ParameterTableView.didSelectRowAtIndexPath: %ld", (long)indexPath.row);
217 
218  [[self givenContainer] didSelectParameterRow:indexPath];
219 }
220 #pragma mark - UITableViewDataSource
221 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
222 {
223  NSLog(@"U3ParameterTableView.numberOfSectionsInTableView: 1");
224  // this is called at "Originate Item"?!!
225  return 1; // default is 1
226 }
227 - (NSInteger)tableView:(UITableView *)tableView
228  numberOfRowsInSection:(NSInteger)section
229 {
230  askedOnce = NO; // initialize somehow
231  NSInteger ret = [[self givenParameters] count];
232  NSLog(@"U3ParameterTableView.numberOfRowsInSection: # of rows in table: %ld", (long)ret);
233  return ret;
234 }
235 - (UITableViewCell *)tableView:(UITableView *)tableView
236  cellForRowAtIndexPath:(NSIndexPath *)indexPath
237 {
238  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usda-parameters-menu"];
239  // NSLog(@"U3parameterTableView.tableCellForRow: %ld cell: %@", (long)[indexPath row], cell);
240  if (cell == nil) {
241  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"usda-parameters-menu"];
242 
243  cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
244  [cell.layer setCornerRadius:7.0f];
245  [cell.layer setMasksToBounds:YES];
246  [cell.layer setBorderWidth:1.0f];
247  }
248 
249  NSInteger nComponents = [[self givenParameters] count];
250 
251  if (nComponents > [indexPath row]) {
252 
253  NSObject *parameterObj = [[self givenParameters] objectAtIndex:[indexPath row]];
254  if ([NSStringFromClass([parameterObj class]) containsString:@"Dictionary"]) {
255  NSDictionary *focusNutritionComp = (NSDictionary *)parameterObj;
256  // NSLog(@"U3parameterTableView.table-cellForRow: %ld focusNutritionComp: %@", (long)[indexPath row], focusNutritionComp);
257  NSString *caption = [focusNutritionComp objectForKey:[Z5LocalDataController componentNameKey]];
258  NSMutableString *captionWithSpace = [[NSMutableString alloc] initWithString:caption];
259  if ([caption hasPrefix:@"Fat"]) {
260  [captionWithSpace insertString:@" " atIndex:3];
261  } else if ([caption hasPrefix:@"Vitamin"]) {
262  [captionWithSpace insertString:@" " atIndex:7];
263  }
264 
265  NSString *valueCaption = [focusNutritionComp objectForKey:[Z5LocalDataController componentValueKey]];
266  NSString *compoTypeClass = NSStringFromClass([valueCaption class]);
267 
268  NSString *detailCaption;
269  if ([compoTypeClass isEqualToString:@"HKQuantity"]) {
270  // NSLog(@"U3parameterTableView.didSelectPickerRow: compo-class: %@", compoTypeClass);
271  HKQuantity* hkq = (HKQuantity *)valueCaption;
272  // NSLog(@"U3parameterTableView.tableCellForRow: desc: %@", hkq.description);
273  // cell.detailTextLabel.text = hkq.description;
274  detailCaption = hkq.description;
275  } else {
276  // cell.detailTextLabel.text = valueCaption;
277  detailCaption = valueCaption;
278  }
279  // NSLog(@"U3parameterTableView.table-cellForRow: caption/valueCaption: %@/%@", caption, valueCaption);
280  [Z5DataController labelizeCell:cell withText:captionWithSpace inColor:[UIColor redColor] andDetail:detailCaption];
281 
282  NSNumber *authoStat = [focusNutritionComp objectForKey:[Z5LocalDataController componentAuthorityKey]];
283  // NSLog(@"U3parameterTableView: authoStat: %@", authoStat);
284  UIColor *authorityColor = [Z5LocalDataController colorForAuthority:authoStat];
285  [cell setBackgroundColor:authorityColor];
286  } else if ([@"Meal" isEqualToString:NSStringFromClass([parameterObj class]) ]) {
287  Meal *focusNutritionMeal = (Meal *)parameterObj;
288  NSDate *mealDate = [focusNutritionMeal orderDate];
289  NSString *mealDateStr = [mDateFormatter stringFromDate:mealDate];
290  // NSString *caption = @"jklegr";
291  NSString *detailCaption = @"none";
292  [Z5DataController labelizeCell:cell withText:mealDateStr inColor:[UIColor orangeColor] andDetail:detailCaption];
293  } else {
294  NSLog(@"U3ParameterTableView.cellForRow: parameter-obj class: %@", NSStringFromClass([parameterObj class]));
295  }
296  } else if (0 == nComponents) {
297  cell.textLabel.text = @"no parameter";
298  }
299 
300  // NSLog(@"U3parameterTableView.tableCellForRow: %ld return cell: %@", (long)[indexPath row], cell);
301  return cell;
302 }
303 
304 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
305 
306  // height of the section header
307  // this needs to be set, otherwise the height is zero and no footer will show
308  // NSLog(@"U3ParameterTableView.heightForHeaderInSection: %3.2f", paramHeaderHeight);
309  return paramHeaderHeight;
310 }
311 
312 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
313 
314  // NSLog(@"U3ParameterTableView.viewForHeaderInSecion: %ld", section);
315  return (UIView *)tableHead;
316 }
317 
318 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
319 
320  // height of the footer
321  // this needs to be set, otherwise the height is zero and no footer will show
322  // NSLog(@"U3ParameterTableView.heightForFooterInSection: %3.2f", paramFooterHeight);
323  // return [tableTail heightRequirement];
324  return paramFooterHeight;
325 }
326 
327 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
328 {
329  return (UIView *)tableTail;
330 }
331 
332 /*
333 // Only override drawRect: if you perform custom drawing.
334 // An empty implementation adversely affects performance during animation.
335 - (void)drawRect:(CGRect)rect {
336  // Drawing code
337 }
338 */
339 
340 @end
UIColor * colorForAuthority:(NSNumber *authorityValue)
UIColor * paramTableBackgrndColor
float paramHeaderHeight
NSMutableArray< NSDictionary * > * givenParameters
instancetype sharedInstance()
UIView< Z6ContainedView > * tableTail
NSDateFormatter * mDateFormatter
NSMutableArray * focusUSDAparameterArray
id< Z6ContainedView > tableHead
id< U3ParameterTableDelegate > givenContainer
float paramFooterHeight
Singleton interface to both core and remote data sources.
NSDate * orderDate
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)