Today's Menu  Portugal
journal and plan nutrition
Instance Methods | Properties | List of all members
V7USDAfoodViewController Class Reference

#import <V7USDAfoodViewController.h>

Inheritance diagram for V7USDAfoodViewController:
Inheritance graph
[legend]
Collaboration diagram for V7USDAfoodViewController:
Collaboration graph
[legend]

Instance Methods

(void) - viewDidLoad [implementation]
 
(void) - viewWillAppear: [implementation]
 
(void) - fetchUSDAFoodItem: [implementation]
 
(void) - parseFoodItem: [implementation]
 
(NSInteger) - numberOfSectionsInTableView: [implementation]
 
(NSInteger) - tableView:numberOfRowsInSection: [implementation]
 
(nonnull UITableViewCell *) - tableView:cellForRowAtIndexPath: [implementation]
 
(void) - didReceiveMemoryWarning [implementation]
 

Properties

NSNumber * focusFoodItemNDB
 
NSString * focusFoodName
 

Detailed Description

Definition at line 11 of file V7USDAfoodViewController.h.

Method Documentation

◆ didReceiveMemoryWarning()

- (void) didReceiveMemoryWarning
implementation

Definition at line 238 of file V7USDAfoodViewController.m.

238  {
239  [super didReceiveMemoryWarning];
240  // Dispose of any resources that can be recreated.
241 }

◆ fetchUSDAFoodItem:()

- (void) fetchUSDAFoodItem: (NSInteger)  ndbInt
implementation

Definition at line 70 of file V7USDAfoodViewController.m.

70  :(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 }
instancetype sharedInstance()
void fetchUSDAdata:withCompletionBlock:(NSURLRequest *request, [withCompletionBlock] void(^ incomingDictionary)(NSDictionary *informationDictionary, NSError *error))
Singleton interface to both core and remote data sources.

◆ numberOfSectionsInTableView:()

- (NSInteger) numberOfSectionsInTableView: (UITableView *)  tableView
implementation

Definition at line 139 of file V7USDAfoodViewController.m.

139  :(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 }

◆ parseFoodItem:()

- (void) parseFoodItem: (NSDictionary *)  foodItem
implementation

Definition at line 105 of file V7USDAfoodViewController.m.

105  : (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 }
void extraLabelize:withText:(UILabel *targLabel, [withText] NSString *txt)
Singleton interface to both core and remote data sources.
NSString * foodName
NSMutableArray * focusFoodNutritionArray

◆ tableView:cellForRowAtIndexPath:()

- (nonnull UITableViewCell *) tableView: (nonnull UITableView *)  tableView
cellForRowAtIndexPath: (nonnull NSIndexPath *)  indexPath 
implementation

Definition at line 158 of file V7USDAfoodViewController.m.

158  :(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 }
instancetype sharedInstance()
Singleton interface to both core and remote data sources.
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)

◆ tableView:numberOfRowsInSection:()

- (NSInteger) tableView: (UITableView *)  tableView
numberOfRowsInSection: (NSInteger)  section 
implementation

Definition at line 148 of file V7USDAfoodViewController.m.

148  :(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 }

◆ viewDidLoad()

- (void) viewDidLoad
implementation

Definition at line 28 of file V7USDAfoodViewController.m.

28  {
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 }
instancetype sharedInstance()
Z1ContainedLabel * focusFoodLabel
Singleton interface to both core and remote data sources.

◆ viewWillAppear:()

- (void) viewWillAppear: (BOOL)  animated
implementation

Definition at line 53 of file V7USDAfoodViewController.m.

53  :(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 }
void extraLabelize:withText:(UILabel *targLabel, [withText] NSString *txt)
Singleton interface to both core and remote data sources.

Property Documentation

◆ focusFoodItemNDB

- (NSNumber*) focusFoodItemNDB
readwritenonatomicstrong

Definition at line 13 of file V7USDAfoodViewController.h.

◆ focusFoodName

- (NSString*) focusFoodName
readwritenonatomicstrong

Definition at line 14 of file V7USDAfoodViewController.h.


The documentation for this class was generated from the following files: