32 self.title =
@"USDA Food";
34 CGRect totalFrame =
self.view.frame;
35 float tableWidth = totalFrame.size.width - 8;
37 CGRect usdaFoodInfoTableViewFrame = CGRectMake(4, 4, tableWidth, totalFrame.size.height - 16);
39 float focusFoodLabelFrameHeight = 60;
40 CGRect focusFoodLabelFrame = CGRectMake(4.0, 0.0, tableWidth - 8.0, focusFoodLabelFrameHeight);
42 NSNumber *labelHeight = [NSNumber numberWithFloat:56.0];
43 [focusFoodLabel setContainedHeight:labelHeight];
44 [focusFoodLabel setBackgroundColor:[UIColor greenColor]];
47 [usdaFoodInfoTableView setDataSource:self];
48 [usdaFoodInfoTableView setViewForHead:focusFoodLabel];
49 [usdaFoodInfoTableView setFocusFoodName:_focusFoodName];
50 [
self.view addSubview:usdaFoodInfoTableView];
53 - (void)viewWillAppear:(BOOL)animated
55 NSInteger ndbInt = [_focusFoodItemNDB integerValue];
58 CGRect totalFrame =
self.view.frame;
59 CGRect labelFrame = CGRectMake(totalFrame.origin.x, totalFrame.origin.y, totalFrame.size.width - 8.0, totalFrame.size.height);
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];
66 [focusFoodLabel setMessageLabel:focusFoodBaseLabel];
67 [focusFoodLabel appear:labelFrame];
70 - (void) fetchUSDAFoodItem:(NSInteger) ndbInt
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];
83 NSArray *foodItems = [informationDictionary objectForKey:@"foods"];
86 NSInteger nFoodItems = [foodItems count];
87 assert(nFoodItems == 1);
90 NSDictionary *foodItem = [foodItems objectAtIndex:0];
91 self->foodName = [foodItem objectForKey:@"name"];
94 [
self->usdaFoodInfoTableView reloadData];
105 - (void) parseFoodItem: (NSDictionary *)foodItem
107 NSString *nutrientDescription;
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"];
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];
125 NSMutableDictionary *nutrientEntryDict = [[NSMutableDictionary alloc] initWithDictionary:nutrientInfo];
126 [
self->focusFoodNutritionArray addObject:nutrientEntryDict];
130 NSLog(
@"V7USDAfoodViewController.parseFoodItem: %@", nutrientDescription);
132 NSLog(
@"V7USDAfoodViewController.parseFoodItem: no nutrientDescription");
137 #pragma mark - UITableViewDataSource 139 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
144 NSLog(
@"V7USDAfoodViewController.numberOfSectionsInTableView: %ld", (
long)ret);
148 - (NSInteger)tableView:(UITableView *)tableView
149 numberOfRowsInSection:(NSInteger)section
151 NSInteger ret = [focusFoodNutritionArray count];
153 NSLog(
@"V7USDAfoodViewController.numberOfRowsInSection: section# %ld, rows: %ld", (
long)section, (
long)ret);
158 - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
160 UITableViewCell *cell = nil;
164 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"usda-pdata-display"];
166 cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
167 [cell.layer setCornerRadius:7.0f];
168 [cell.layer setMasksToBounds:YES];
169 [cell.layer setBorderWidth:1.0f];
171 [cell.textLabel setTextColor:[UIColor blackColor]];
172 [cell.textLabel setOpaque:NO];
175 NSInteger nComponents = [focusFoodNutritionArray count];
178 if (nComponents > [indexPath row]) {
180 NSObject *parameterObj = [focusFoodNutritionArray objectAtIndex:[indexPath row]];
181 if ([NSStringFromClass([parameterObj
class]) containsString:
@"Dictionary"]) {
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];
197 NSLog(
@"USDAFoodViewController.cellForRow: parameter-obj class: %@", NSStringFromClass([parameterObj
class]));
199 }
else if (0 == nComponents) {
200 cell.textLabel.text =
@"no food data";
239 [
super didReceiveMemoryWarning];
void extraLabelize:withText:(UILabel *targLabel, [withText] NSString *txt)
instancetype sharedInstance()
Z1ContainedLabel * focusFoodLabel
void didReceiveMemoryWarning()
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.
void fetchUSDAFoodItem:(NSInteger ndbInt)
NSMutableArray * focusFoodNutritionArray
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)