Today's Menu  Portugal
journal and plan nutrition
V7USDAfoodViewController.m
Go to the documentation of this file.
1 //
2 // V7USDAfoodViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 6/22/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
11 #import "Z5DataController.h"
12 #import "Z7EmpaneledTableView.h"
14 #import "Z1ContainedLabel.h"
15 
16 @interface V7USDAfoodViewController ()
17 
18 @end
19 
20 @implementation V7USDAfoodViewController
21 {
22  U7USDAFoodDataTableView *usdaFoodInfoTableView;
24  NSMutableArray *focusFoodNutritionArray;
25  NSString *foodName;
26 }
27 
28 - (void)viewDidLoad {
29  [super viewDidLoad];
30  // Do any additional setup after loading the view.
31 
32  self.title = @"USDA Food";
33  [self.view setBackgroundColor:[[Z5DataController sharedInstance] grapeSkinDeepPurple]];
34  CGRect totalFrame = self.view.frame;
35  float tableWidth = totalFrame.size.width - 8;
36 
37  CGRect usdaFoodInfoTableViewFrame = CGRectMake(4, 4, tableWidth, totalFrame.size.height - 16);
38 
39  float focusFoodLabelFrameHeight = 60;
40  CGRect focusFoodLabelFrame = CGRectMake(4.0, 0.0, tableWidth - 8.0, focusFoodLabelFrameHeight);
41  focusFoodLabel = [[Z1ContainedLabel alloc] initWithFrame:focusFoodLabelFrame];
42  NSNumber *labelHeight = [NSNumber numberWithFloat:56.0];
43  [focusFoodLabel setContainedHeight:labelHeight];
44  [focusFoodLabel setBackgroundColor:[UIColor greenColor]];
45 
46  usdaFoodInfoTableView = [[U7USDAFoodDataTableView alloc] initWithFrame:usdaFoodInfoTableViewFrame];
47  [usdaFoodInfoTableView setDataSource:self];
48  [usdaFoodInfoTableView setViewForHead:focusFoodLabel];
49  [usdaFoodInfoTableView setFocusFoodName:_focusFoodName];
50  [self.view addSubview:usdaFoodInfoTableView];
51 }
52 
53 - (void)viewWillAppear:(BOOL)animated
54 {
55  NSInteger ndbInt = [_focusFoodItemNDB integerValue];
56  [self fetchUSDAFoodItem:ndbInt];
57 
58  CGRect totalFrame = self.view.frame;
59  CGRect labelFrame = CGRectMake(totalFrame.origin.x, totalFrame.origin.y, totalFrame.size.width - 8.0, totalFrame.size.height);
60 
61  float tableWidth = totalFrame.size.width - 8;
62  CGRect focusFoodBaseLabelFrame = CGRectMake(4.0, 0.0, tableWidth - 8.0, 36);
63  UILabel *focusFoodBaseLabel = [[UILabel alloc] initWithFrame:focusFoodBaseLabelFrame];
64 
65  [Z5DataController extraLabelize:focusFoodBaseLabel withText:@"USDA Food"];
66  [focusFoodLabel setMessageLabel:focusFoodBaseLabel];
67  [focusFoodLabel appear:labelFrame];
68 
69 }
70 - (void) fetchUSDAFoodItem:(NSInteger) ndbInt
71 {
72  Z5URLSessionController *sessionController = [[[Z5DataController sharedInstance] remoteDataController] sessionController];
73 
74  NSString *usdaQuery = [[NSString alloc] initWithFormat:@"https://api.nal.usda.gov/ndb/V2/reports?ndbno=%ld&type=b&format=json&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK", (long)ndbInt];
75  NSURL *foodDataURL = [NSURL URLWithString:usdaQuery];
76  NSURLRequest *request = [NSURLRequest requestWithURL:foodDataURL];
77 
78  [sessionController fetchUSDAdata: request
79  withCompletionBlock: ^(NSDictionary *informationDictionary, NSError *error) {
80  if (!error) {
81  // very informative!
82 
83  NSArray *foodItems = [informationDictionary objectForKey:@"foods"];
84  assert(foodItems);
85 
86  NSInteger nFoodItems = [foodItems count];
87  assert(nFoodItems == 1);
88  // NSLog(@"V7USDAfoodViewController.viewWillAppear: nFoodItems: %ld", nFoodItems);
89  // food { desc { ..., name,..}, nutrients ( {..., name, unit, value}
90  NSDictionary *foodItem = [foodItems objectAtIndex:0];
91  self->foodName = [foodItem objectForKey:@"name"];
92  [self parseFoodItem:foodItem];
93 
94  [self->usdaFoodInfoTableView reloadData];
95  } else {
96  // [[NSOperationQueue mainQueue] addOperaionWithBlock:^{
97  // // [NSApp presentError:error];
98  // NSLog(@"USDADataView.viewWillAppear: Error: %@", error);
99  // }];
100  }
101  } ];
102 
103 }
104 
105 - (void) parseFoodItem: (NSDictionary *)foodItem
106 {
107  NSString *nutrientDescription;
108  focusFoodNutritionArray = [[NSMutableArray alloc] init];
109 
110  NSDictionary *foodParameters = [foodItem objectForKey:@"food"];
111  NSLog(@"V7USDAfoodViewController.parseFoodItem food parameter keys: %@", [foodParameters allKeys]);
112  NSDictionary *foodDesc = [foodParameters objectForKey:@"desc"];
113  NSString *foodName = [foodDesc objectForKey:@"name"];
114  NSLog(@"V7USDAfoodViewController.parseFoodItem: name: %@", foodName);
115  NSArray *foodNutrients = [foodParameters objectForKey:@"nutrients"];
116  if (foodNutrients) {
117  nutrientDescription = @"V7USDAfoodViewController.parseFoodItem: ";
118  for (NSDictionary *nutrientInfo in foodNutrients) {
119  NSString *nutriName = [nutrientInfo objectForKey:@"name"];
120  NSString *nutriUnit = [nutrientInfo objectForKey:@"unit"];
121  NSString *nutriValue = [nutrientInfo objectForKey:@"value"];
122  NSString *nutrientEntryString = [NSString stringWithFormat:@"\nname %@, %@ %@", nutriName, nutriValue, nutriUnit];
123  nutrientDescription = [nutrientDescription stringByAppendingString:nutrientEntryString];
124 
125  NSMutableDictionary *nutrientEntryDict = [[NSMutableDictionary alloc] initWithDictionary:nutrientInfo];
126  [self->focusFoodNutritionArray addObject:nutrientEntryDict];
127  [Z5DataController extraLabelize:self->focusFoodLabel.messageLabel withText:foodName];
128 
129  } // for nutrients in nutrient-info
130  NSLog(@"V7USDAfoodViewController.parseFoodItem: %@", nutrientDescription);
131  } else {
132  NSLog(@"V7USDAfoodViewController.parseFoodItem: no nutrientDescription");
133  }
134 }
135 
136 
137 #pragma mark - UITableViewDataSource
138 
139 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
140 {
141  NSInteger ret = 1;
142  // Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
143 
144  NSLog(@"V7USDAfoodViewController.numberOfSectionsInTableView: %ld", (long)ret);
145 
146  return ret;
147 }
148 - (NSInteger)tableView:(UITableView *)tableView
149  numberOfRowsInSection:(NSInteger)section
150 {
151  NSInteger ret = [focusFoodNutritionArray count];
152 
153  NSLog(@"V7USDAfoodViewController.numberOfRowsInSection: section# %ld, rows: %ld", (long)section, (long)ret);
154 
155  return ret;
156 }
157 
158 - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
159 
160  UITableViewCell *cell = nil;
161  // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usda-data-display"];
162  // NSLog(@"U3parameterTableView.tableCellForRow: %ld cell: %@", (long)[indexPath row], cell);
163  if (cell == nil) {
164  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"usda-pdata-display"];
165 
166  cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
167  [cell.layer setCornerRadius:7.0f];
168  [cell.layer setMasksToBounds:YES];
169  [cell.layer setBorderWidth:1.0f];
170 
171  [cell.textLabel setTextColor:[UIColor blackColor]];
172  [cell.textLabel setOpaque:NO];
173  }
174 
175  NSInteger nComponents = [focusFoodNutritionArray count];
176 
177  UIColor *cellBackgrd = [[Z5DataController sharedInstance] keylimePieColor];
178  if (nComponents > [indexPath row]) {
179 
180  NSObject *parameterObj = [focusFoodNutritionArray objectAtIndex:[indexPath row]];
181  if ([NSStringFromClass([parameterObj class]) containsString:@"Dictionary"]) {
182 
183  // for (NSDictionary *nutrientInfo in focusFoodNutritionArray) {
184  NSDictionary *nutrientInfo = (NSDictionary *)parameterObj;
185  NSString *nutriName = [nutrientInfo objectForKey:@"name"];
186  NSString *nutriUnit = [nutrientInfo objectForKey:@"unit"];
187  NSString *nutriValue = [nutrientInfo objectForKey:@"value"];
188  NSString *nutrientEntryString = [NSString stringWithFormat:@"%@ %@", nutriValue, nutriUnit];
189 
190  [cell setBackgroundColor:[[Z5DataController sharedInstance] wheatColor]];
191 
192  // NSLog(@"USDAFoodViewController.cellForRow: item text: %@", nutriName);
193  [Z5DataController labelizeCell:cell withText:nutriName inColor:cellBackgrd andDetail:nutrientEntryString];
194 
195  // }
196  } else {
197  NSLog(@"USDAFoodViewController.cellForRow: parameter-obj class: %@", NSStringFromClass([parameterObj class]));
198  }
199  } else if (0 == nComponents) {
200  cell.textLabel.text = @"no food data";
201  }
202 
203  // NSLog(@"U3parameterTableView.tableCellForRow: %ld return cell: %@", (long)[indexPath row], cell);
204  return cell;
205 }
206 
207 //- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
208 // NSInteger ret = [_foodDataElementsArray count];
209 // NSLog(@"U7USDAFoodDataTableView.numberOfRowsInSection: # of rows in table: %ld", (long)ret);
210 // return ret;
211 //
212 //}
213 //
214 //- (UITableViewCell *)tableView:(UITableView *)tableView
215 // cellForRowAtIndexPath:(NSIndexPath *)indexPath
216 //{
217 // UITableViewCell *cell = nil;
218 // cell = [tableView dequeueReusableCellWithIdentifier:@"usda-food-data"];
219 // if (cell == nil) {
220 // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"usda-food-data"];
221 // cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
222 // }
223 // // [cell setBackgroundColor:[UIColor colorWithHue:0.23f saturation:0.7f brightness:0.9f alpha:1.0f]];
224 // [cell setBackgroundColor:[[Z5DataController sharedInstance] grapeSkinDeepPurple]];
225 //
226 // NSDictionary *nutrientInfo = [self->focusFoodNutritionArray objectAtIndex:[indexPath row]];
227 // NSString *nutriName = [nutrientInfo objectForKey:@"name"];
228 // NSString *nutriUnit = [nutrientInfo objectForKey:@"unit"];
229 // NSString *nutriValue = [nutrientInfo objectForKey:@"value"];
230 // NSString *nutrientEntryString = [NSString stringWithFormat:@"%@ %@", nutriValue, nutriUnit];
231 //
232 // [Z5DataController labelizeCell:cell withText:nutriName inColor:[UIColor brownColor] andDetail:nutrientEntryString];
233 //
234 // return cell;
235 //}
236 #pragma mark -
237 
239  [super didReceiveMemoryWarning];
240  // Dispose of any resources that can be recreated.
241 }
242 
243 /*
244 #pragma mark - Navigation
245 
246 // In a storyboard-based application, you will often want to do a little preparation before navigation
247 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
248  // Get the new view controller using [segue destinationViewController].
249  // Pass the selected object to the new view controller.
250 }
251 */
252 
253 @end
void extraLabelize:withText:(UILabel *targLabel, [withText] NSString *txt)
instancetype sharedInstance()
Z1ContainedLabel * focusFoodLabel
void fetchUSDAdata:withCompletionBlock:(NSURLRequest *request, [withCompletionBlock] void(^ incomingDictionary)(NSDictionary *informationDictionary, NSError *error))
void parseFoodItem:(NSDictionary *foodItem)
Singleton interface to both core and remote data sources.
NSString * foodName
void fetchUSDAFoodItem:(NSInteger ndbInt)
NSMutableArray * focusFoodNutritionArray
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)