22 NSMutableArray<NSDictionary *> *usdaFoodItems;
31 self.title =
@"USDA List";
32 CGRect totalFrame =
self.view.frame;
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];
45 usdaFoodItems = [[NSMutableArray alloc] init];
47 totalFrame.size.height * 0.12,
48 totalFrame.size.width * 0.92,
49 totalFrame.size.height * 0.86);
53 [foodEntriesTableView setDataSource:self];
54 [foodEntriesTableView setDelegate:self];
58 [
self.view addSubview:foodEntriesTableView];
61 -(void) viewWillAppear:(BOOL)animated
63 [headerView appear:self.view.frame];
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];
84 NSLog(
@"V7USDAdataViewController.hitScanDatabase: searchStr: %@", searchStr);
85 [headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseQueried];
86 NSURL *searchURL = [NSURL URLWithString:searchStr];
87 NSURLRequest *request = [NSURLRequest requestWithURL:searchURL];
92 [
self->headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseRespOK];
94 NSDictionary *foodList = [informationDictionary objectForKey:@"list"];
97 NSArray *foodItems = [foodList objectForKey:@"item"];
100 NSInteger nFoodItems = [foodItems count];
101 NSLog(@"V7USDAdataViewController.viewWillAppear: nFoodItems: %ld", (long)nFoodItems);
103 [
self->usdaFoodItems removeAllObjects];
104 [
self->usdaFoodItems addObjectsFromArray:foodItems];
110 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
112 [
self->headerView showUSDAFoodDatabaseResponse:USDAFoodDatabaseRespErr];
113 NSLog(@"USDADataView.viewWillAppear: Error: %@", error);
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;
129 if (0 < [usdaFoodItems count]) {
130 foodsTableHeight = [foodEntriesTableView getViewHeightReqmt]
132 float maxHgt = totalFrame.size.height * 0.80;
133 if (maxHgt < foodsTableHeight) {
134 foodsTableHeight = maxHgt;
138 availableWindowTop + totalFrame.size.height * 0.04,
139 totalFrame.size.width * 0.92,
141 [foodEntriesTableView setFrame:foodEntriesTableFrame];
143 [
self.view setNeedsDisplay];
144 [foodEntriesTableView reloadData];
148 NSInteger ret = [usdaFoodItems count];
150 NSLog(
@"USDAdataViewController.assessMessagesToDisplay: ret: %ld", (
long)ret);
154 #pragma mark - V7foodTextFieldDelegate 158 UITextField *searchFoodtextF = [headerView foodTextField];
163 NSLog(
@"USDAdataViewController.clickScan: no search food");
167 #pragma mark - UITableView Delegate Methods 169 - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
170 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usda-items"];
173 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"usda-items"];
175 cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
176 [cell.layer setCornerRadius:7.0f];
177 [cell.layer setMasksToBounds:YES];
178 [cell.layer setBorderWidth:1.0f];
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];
185 [cell.contentView addSubview:pushMe];
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"];
203 - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
204 return [usdaFoodItems count];
206 #pragma mark - Table view delegate 212 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
213 int row = (int)[indexPath row];
215 NSDictionary *focusFoodItem = [usdaFoodItems objectAtIndex:row];
217 NSString *focusFoodName = [focusFoodItem objectForKey:@"name"];
218 NSLog(
@"GmailViewController.didSelectRow: food: %@", focusFoodName);
219 NSNumber *ndbNumber = [focusFoodItem objectForKey:@"ndbno"];
222 [vc2 setFocusFoodItemNDB:ndbNumber];
223 [vc2 setFocusFoodName:focusFoodName];
224 [[
self navigationController] pushViewController:vc2 animated:YES];
226 #pragma mark - V7UsdaFoods table delegate 229 return usdaFoodItems;
231 #pragma mark - NSURLConnection Delegate Methods 233 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
241 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
246 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
247 willCacheResponse:(NSCachedURLResponse*)cachedResponse {
252 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
258 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
265 [
super didReceiveMemoryWarning];
instancetype sharedInstance()
NSMutableArray< NSDictionary *> * retrieveFoodEntries()
V7foodTextFieldView * headerView
void didReceiveMemoryWarning()
void fetchUSDAdata:withCompletionBlock:(NSURLRequest *request, [withCompletionBlock] void(^ incomingDictionary)(NSDictionary *informationDictionary, NSError *error))
CGRect foodEntriesTableFrame
Singleton interface to both core and remote data sources.
V7UsdaFoodsTableView * foodEntriesTableView
NSInteger assessFoodsToDisplay()
NSMutableData * _responseData
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)