Today's Menu  Portugal
journal and plan nutrition
Instance Methods | Protected Attributes | List of all members
V7USDAdataViewController Class Reference

#import <V7USDAdataViewController.h>

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

Instance Methods

(void) - viewDidLoad [implementation]
 
(void) - viewWillAppear: [implementation]
 
(void) - hitScanDatabase [implementation]
 
(void) - reframeTable [implementation]
 
(NSInteger) - assessFoodsToDisplay [implementation]
 
(void) - clickScan [implementation]
 
(nonnull UITableViewCell *) - tableView:cellForRowAtIndexPath: [implementation]
 
(NSInteger) - tableView:numberOfRowsInSection: [implementation]
 
(void) - tableView:didSelectRowAtIndexPath: [implementation]
 
(NSMutableArray< NSDictionary *> *) - retrieveFoodEntries [implementation]
 
(void) - connection:didReceiveResponse: [implementation]
 
(void) - connection:didReceiveData: [implementation]
 
(NSCachedURLResponse *) - connection:willCacheResponse: [implementation]
 
(void) - connectionDidFinishLoading: [implementation]
 
(void) - connection:didFailWithError: [implementation]
 
(void) - didReceiveMemoryWarning [implementation]
 

Protected Attributes

NSMutableData * _responseData
 

Detailed Description

Definition at line 15 of file V7USDAdataViewController.h.

Method Documentation

◆ assessFoodsToDisplay()

- (NSInteger) assessFoodsToDisplay
implementation

Definition at line 146 of file V7USDAdataViewController.m.

147 {
148  NSInteger ret = [usdaFoodItems count];
149 
150  NSLog(@"USDAdataViewController.assessMessagesToDisplay: ret: %ld", (long)ret);
151  return ret;
152 }

◆ clickScan()

- (void) clickScan
implementation

Definition at line 156 of file V7USDAdataViewController.m.

157 {
158  UITextField *searchFoodtextF = [headerView foodTextField];
159  searchFood = [searchFoodtextF text];
160  if (nil!=searchFood) {
161  [self hitScanDatabase];
162  } else {
163  NSLog(@"USDAdataViewController.clickScan: no search food");
164  }
165 }
NSString * searchFood

◆ connection:didFailWithError:()

- (void) connection: (NSURLConnection *)  connection
didFailWithError: (NSError *)  error 
implementation

Definition at line 258 of file V7USDAdataViewController.m.

258  :(NSURLConnection *)connection didFailWithError:(NSError *)error {
259  // The request has failed for some reason!
260  // Check the error var
261 }

◆ connection:didReceiveData:()

- (void) connection: (NSURLConnection *)  connection
didReceiveData: (NSData *)  data 
implementation

Definition at line 241 of file V7USDAdataViewController.m.

241  :(NSURLConnection *)connection didReceiveData:(NSData *)data {
242  // Append the new data to the instance variable you declared
243  [_responseData appendData:data];
244 }

◆ connection:didReceiveResponse:()

- (void) connection: (NSURLConnection *)  connection
didReceiveResponse: (NSURLResponse *)  response 
implementation

Definition at line 233 of file V7USDAdataViewController.m.

233  :(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
234  // A response has been received, this is where we initialize the instance var you created
235  // so that we can append data to it in the didReceiveData method
236  // Furthermore, this method is called each time there is a redirect so reinitializing it
237  // also serves to clear it
238  _responseData = [[NSMutableData alloc] init];
239 }

◆ connection:willCacheResponse:()

- (NSCachedURLResponse *) connection: (NSURLConnection *)  connection
willCacheResponse: (NSCachedURLResponse*)  cachedResponse 
implementation

Definition at line 246 of file V7USDAdataViewController.m.

246  :(NSURLConnection *)connection
247  willCacheResponse:(NSCachedURLResponse*)cachedResponse {
248  // Return nil to indicate not necessary to store a cached response for this connection
249  return nil;
250 }

◆ connectionDidFinishLoading:()

- (void) connectionDidFinishLoading: (NSURLConnection *)  connection
implementation

Definition at line 252 of file V7USDAdataViewController.m.

252  :(NSURLConnection *)connection {
253  // The request is complete and data has been received
254  // You can parse the stuff in your instance variable now
255 
256 }

◆ didReceiveMemoryWarning()

- (void) didReceiveMemoryWarning
implementation

Definition at line 264 of file V7USDAdataViewController.m.

264  {
265  [super didReceiveMemoryWarning];
266  // Dispose of any resources that can be recreated.
267 }

◆ hitScanDatabase()

- (void) hitScanDatabase
implementation

Definition at line 66 of file V7USDAdataViewController.m.

67 {
68  Z5URLSessionController *sessionController = [[[Z5DataController sharedInstance] remoteDataController] sessionController];
69 
70  // Create the request.
71  // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
72 
73 // NSString *searchStr = @"https://api.nal.usda.gov/ndb/search/?format=json&q=";
74 // NSArray<NSString *> *listItems = [searchFood componentsSeparatedByString:@", "];
75 // for (NSString *foodSpec in listItems) {
76 // NSString *foodSpecQuery = [NSString stringWithFormat:@"%@%%20",foodSpec];
77 // searchStr = [searchStr stringByAppendingString:foodSpecQuery];
78 // }
79 // searchStr = [searchStr stringByAppendingString:@"sort=n&max=25&offset=0&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK"];
80 
81  searchFood = [searchFood stringByReplacingOccurrencesOfString:@", " withString:@"+"];
82  NSString *searchStr = [NSString stringWithFormat:@"https://api.nal.usda.gov/ndb/search/?format=json&q=%@&sort=n&max=25&offset=0&api_key=MHeHC2wqKJHmMxYqHzzLlMfnl4ZOLQLAZeKGEkdK", searchFood];
83 
84  NSLog(@"V7USDAdataViewController.hitScanDatabase: searchStr: %@", searchStr);
85  [headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseQueried];
86  NSURL *searchURL = [NSURL URLWithString:searchStr];
87  NSURLRequest *request = [NSURLRequest requestWithURL:searchURL];
88 
89  [sessionController fetchUSDAdata: request withCompletionBlock: ^(NSDictionary *informationDictionary, NSError *error) {
90  if (!error) {
91  // NSLog(@"V7USDAdataViewController.viewWillAppear: USDA-data %@", informationDictionary);
92  [self->headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseRespOK];
93 
94  NSDictionary *foodList = [informationDictionary objectForKey:@"list"];
95  assert(foodList);
96 
97  NSArray *foodItems = [foodList objectForKey:@"item"];
98  assert(foodItems);
99 
100  NSInteger nFoodItems = [foodItems count];
101  NSLog(@"V7USDAdataViewController.viewWillAppear: nFoodItems: %ld", (long)nFoodItems);
102 
103  [self->usdaFoodItems removeAllObjects];
104  [self->usdaFoodItems addObjectsFromArray:foodItems];
105  [self reframeTable];
106 
107  }
108  else
109  {
110  [[NSOperationQueue mainQueue] addOperationWithBlock:^{
111  // [NSApp presentError:error];
112  [self->headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseRespErr];
113  NSLog(@"USDADataView.viewWillAppear: Error: %@", error);
114  }];
115  }
116  }];
117 }
NSString * searchFood
instancetype sharedInstance()
void fetchUSDAdata:withCompletionBlock:(NSURLRequest *request, [withCompletionBlock] void(^ incomingDictionary)(NSDictionary *informationDictionary, NSError *error))
Singleton interface to both core and remote data sources.

◆ reframeTable()

- (void) reframeTable
implementation

whenever the array of emailMessages changes, call reframeTable.

Definition at line 122 of file V7USDAdataViewController.m.

123 {
124  CGRect totalFrame = self.view.frame;
125  float foodsTableHeight = 0.0;
126  CGRect topNaviFrame = [[[self navigationController] navigationBar] frame];
127  float availableWindowTop = topNaviFrame.origin.y + topNaviFrame.size.height;
128 
129  if (0 < [usdaFoodItems count]) {
130  foodsTableHeight = [foodEntriesTableView getViewHeightReqmt]
131  + 42.0 * [self assessFoodsToDisplay];
132  float maxHgt = totalFrame.size.height * 0.80;
133  if (maxHgt < foodsTableHeight) {
134  foodsTableHeight = maxHgt;
135  }
136  }
137  foodEntriesTableFrame = CGRectMake(totalFrame.size.width * 0.04,
138  availableWindowTop + totalFrame.size.height * 0.04,
139  totalFrame.size.width * 0.92,
140  foodsTableHeight);
141  [foodEntriesTableView setFrame:foodEntriesTableFrame];
142 
143  [self.view setNeedsDisplay];
144  [foodEntriesTableView reloadData];
145 }
CGRect foodEntriesTableFrame

◆ retrieveFoodEntries()

- (NSMutableArray< NSDictionary * > *) retrieveFoodEntries
implementation

Definition at line 227 of file V7USDAdataViewController.m.

228 {
229  return usdaFoodItems;
230 }

◆ tableView:cellForRowAtIndexPath:()

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

Definition at line 169 of file V7USDAdataViewController.m.

169  :(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
170  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usda-items"];
171  // NSLog(@"GmailView.cellForRow: cell: %@", cell);
172  if (cell == nil) {
173  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"usda-items"];
174 
175  cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
176  [cell.layer setCornerRadius:7.0f];
177  [cell.layer setMasksToBounds:YES];
178  [cell.layer setBorderWidth:1.0f];
179 
180  CGRect contentRect = [cell.contentView frame];
181  CGRect fetchPublishButton1Frame = CGRectMake(contentRect.size.width - 86.0f, 5.0, 34.0f, contentRect.size.height - 10);
182  UIButton *pushMe = [[UIButton alloc] initWithFrame:fetchPublishButton1Frame];
183  [pushMe setTitle:@"GO" forState:UIControlStateApplication];
184 
185  [cell.contentView addSubview:pushMe];
186  [cell setHidden:NO];
187  }
188 
189  NSInteger nFoodItems = [usdaFoodItems count];
190  NSInteger iRow = [indexPath row];
191  if (nFoodItems > iRow) {
192  NSDictionary *foodItem = [usdaFoodItems objectAtIndex:iRow];
193  NSString *caption = [foodItem objectForKey:@"name"];
194 
195  // cell.textLabel.text = caption;
196  [Z5DataController labelizeCell:cell withText:caption inColor:[UIColor lightGrayColor] andDetail:@"detailed"];
197  // [cell setBackgroundColor:[UIColor colorWithHue:0.18f saturation:1.0f brightness:0.6f alpha:0.4f]];
198  }
199 
200  return cell;
201 }
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:didSelectRowAtIndexPath:()

- (void) tableView: (UITableView *)  tableView
didSelectRowAtIndexPath: (NSIndexPath *)  indexPath 
implementation

This method sets the class selected-molecule, it also sets the molecule back to the requestor, which should be done by the 'select' button.

Definition at line 212 of file V7USDAdataViewController.m.

212  :(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
213  int row = (int)[indexPath row];
214 
215  NSDictionary *focusFoodItem = [usdaFoodItems objectAtIndex:row];
216 
217  NSString *focusFoodName = [focusFoodItem objectForKey:@"name"];
218  NSLog(@"GmailViewController.didSelectRow: food: %@", focusFoodName);
219  NSNumber *ndbNumber = [focusFoodItem objectForKey:@"ndbno"];
220 
222  [vc2 setFocusFoodItemNDB:ndbNumber];
223  [vc2 setFocusFoodName:focusFoodName];
224  [[self navigationController] pushViewController:vc2 animated:YES];
225 }

◆ tableView:numberOfRowsInSection:()

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

Definition at line 203 of file V7USDAdataViewController.m.

203  :(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
204  return [usdaFoodItems count];
205 }

◆ viewDidLoad()

- (void) viewDidLoad
implementation

Definition at line 28 of file V7USDAdataViewController.m.

28  {
29  [super viewDidLoad];
30  // Do any additional setup after loading the view.
31  self.title = @"USDA List";
32  CGRect totalFrame = self.view.frame;
33 
34  // get and scale image for background
35  UIImage *backgrdImage = [UIImage imageNamed:@"antiqueSunburstWood.png"];
36  CGSize photoSize = [backgrdImage size];
37  float resizeRatio = photoSize.height / totalFrame.size.height;
38  UIImage *resizedImage = [UIImage imageWithCGImage:[backgrdImage CGImage]
39  scale:backgrdImage.scale * resizeRatio
40  orientation:(backgrdImage.imageOrientation) ];
41  UIImageView *backgrd = [[UIImageView alloc] initWithImage:resizedImage];
42  [self.view addSubview:backgrd];
43  // end - get and scale image for background
44 
45  usdaFoodItems = [[NSMutableArray alloc] init];
46  foodEntriesTableFrame = CGRectMake(totalFrame.size.width * 0.04,
47  totalFrame.size.height * 0.12,
48  totalFrame.size.width * 0.92,
49  totalFrame.size.height * 0.86);
50  headerView = [[V7foodTextFieldView alloc] initWithDelegate:self];
51 
52  foodEntriesTableView = [[V7UsdaFoodsTableView alloc] initWithFrame:foodEntriesTableFrame andHeader:headerView inContainer:self];
53  [foodEntriesTableView setDataSource:self];
54  [foodEntriesTableView setDelegate:self];
55 
56 // [_foodEntriesTableView setViewForHead:headerView];
57  [foodEntriesTableView setBackgroundColor:[[Z5DataController sharedInstance] wheatColor]];
58  [self.view addSubview:foodEntriesTableView];
59 
60 }
instancetype sharedInstance()
V7foodTextFieldView * headerView
CGRect foodEntriesTableFrame
Singleton interface to both core and remote data sources.
V7UsdaFoodsTableView * foodEntriesTableView

◆ viewWillAppear:()

- (void) viewWillAppear: (BOOL)  animated
implementation

Definition at line 61 of file V7USDAdataViewController.m.

61  :(BOOL)animated
62 {
63  [headerView appear:self.view.frame];
64 }

Member Data Documentation

◆ _responseData

- (NSMutableData*) _responseData
protected

Definition at line 22 of file V7USDAdataViewController.h.


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