Today's Menu  Portugal
journal and plan nutrition
V3KitchenTableView.m
Go to the documentation of this file.
1 //
2 // V3KitchenTableView.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 1/9/19.
6 // Copyright © 2019 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
9 #import "V3KitchenTableView.h"
10 #import "Z5DataController.h"
14 
15 @implementation V3KitchenTableView
16 {
17  id<V3KitchenTableProtocol> dataDelegate;
18  // EmailMessage *focusEmailMsg;
19  NSDateFormatter *mDateFormatter;
20  // NSIndexPath *swipedMenuIndexPath;
21 
24  // this is the gallery that dictates the pick from the database
26  NSMutableArray<MenuItem *> *kitchenTableMenuItems;
27  id<V3KitchenTableProtocol> memDelegate;
29 }
30 
31 - (id) initWithFrame:(CGRect)frame
32  andDelegate:(id<V3KitchenTableProtocol>)delegate
33 {
34  self = [super initWithFrame:frame];
35  if (self) {
37  kitchenTableMenuItems = [[NSMutableArray alloc] init];
38  focusKitchenGallery = [[memDelegate focusGallery] integerValue];
40 
41  mDateFormatter = [[NSDateFormatter alloc] init];
42  [mDateFormatter setTimeStyle:NSDateFormatterNoStyle];
43  [mDateFormatter setDateStyle:NSDateFormatterMediumStyle];
44 
45  CGRect kitchenTableHeadFrame = CGRectMake(0, 0, self.bounds.size.width - 8.0, 60);
46  kitchenTableHeadView = [[V3GalleryMenuItemControlbarView alloc] initWithFrame:kitchenTableHeadFrame andDelegate:self];
47 
48  }
49  return self;
50 }
51 
53 {
54  unsigned long nEntries = [kitchenTableMenuItems count];
55  float ret = (nEntries * 67.0) + [self getViewHeightReqmt];
56  NSLog(@"V3KitchentableView.heghtRequirement: nEntries: %lu ret: %3.2f", nEntries, ret);
57  return ret + 80.0;
58 }
59 - (void) appear:(CGRect)bound
60 {
61  self.dataSource = self;
62  [self setDelegate:self];
63 
64  [self.layer setBorderColor: [[UIColor blackColor] CGColor]];
65  [self.layer setBorderWidth: 2.0];
66  self.layer.cornerRadius = 8.0f;
67  self.layer.masksToBounds = YES;
68 
69  [self fillDataForGallery:(int)focusKitchenGallery];
70  [self reloadData];
71 }
72 
73 #pragma mark - V3GalleryMenuItemControlbarProtocol protocol
75 {
76  NSLog(@"V3KitchenTableView.fireRecipeCheck");
77 }
78 - (void) addMenuItemOnToday:(UIButton *)sender
79 {
80  NSLog(@"V3KitchenTableView.V3GalleryMenuItemControlbarProtocol");
81 }
83 {
84  NSLog(@"V3KitchenTableView.clickPlusButton");
85  [memDelegate clickPlusButton];
86 }
88 {
89  return focusKitchenGallery;
90 }
91 
92 #pragma mark - UITableViewDataSource protocol
93 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
94 {
95  NSInteger ret = 1;
96  // NSLog(@"V3KitchenTableView.numberOfSectionsInTableView: %ld", (long)ret);
97  return ret;
98 }
99 - (NSInteger)tableView:(UITableView *)tableView
100  numberOfRowsInSection:(NSInteger)section
101 {
102  NSInteger ret = [kitchenTableMenuItems count];
103  // NSLog(@"V3KitchenTableView.numberOfRowsInSection: section# %ld, rows: %ld", (long)section, (long)ret);
104  return ret;
105 }
106 
107 - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
108  NSString *cellKey = @"usda-pdata-display";
109  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellKey];
110  // NSLog(@"U3parameterTableView.tableCellForRow: %ld de-Qd cell: %@", (long)[indexPath row], cell);
111  if (cell == nil) {
112 
113  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellKey];
114 
115  // cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
116 
117  CGRect contentRect = [cell.contentView frame];
118  CGRect fetchPublishButton1Frame = CGRectMake(contentRect.size.width - 86.0f, 5.0, 64.0f, contentRect.size.height - 10);
119  UIButton *pushMe = [[UIButton alloc] initWithFrame:fetchPublishButton1Frame];
120  [pushMe setBackgroundColor:[UIColor grayColor]];
121  [pushMe setTitle:@"GO" forState:UIControlStateNormal];
122  [pushMe setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
123 
124  [cell.contentView addSubview:pushMe];
125 
126  [cell.layer setCornerRadius:7.0f];
127  [cell.layer setMasksToBounds:YES];
128  [cell.layer setBorderWidth:1.0f];
129 
130  [cell.textLabel setTextColor:[UIColor purpleColor]];
131  [cell.textLabel setOpaque:YES];
132  }
133  [cell setBackgroundColor:[UIColor brownColor]];
134  NSInteger nComponents = [kitchenTableMenuItems count];
135 
136  if (nComponents > [indexPath row]) {
137 
138  MenuItem *item = [kitchenTableMenuItems objectAtIndex:[indexPath row]];
139  NSString *itemName = [item name];
140  [Z5DataController labelizeCell:cell withText:itemName inColor:[UIColor pearlColor] andDetail:nil];
141 
142  } else if (0 == nComponents) {
143  cell.textLabel.text = @"no food data";
144  }
145  NSLog(@"V3KitchenTableView.tableCellForRow: %3.2f", cell.bounds.size.height);
146  return cell;
147 }
148 
149 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
150 {
151  switch ([fromIndexPath section]) {
152  case 0:
153  {
154  NSInteger fromRow = [fromIndexPath row];
155  NSInteger toRow = [toIndexPath row];
156  NSLog(@"V3GalleryEdit.moveRowAtIndexPath: from %ld to %ld", (long)fromRow, (long)toRow);
157 
158  MenuItem *menuItem = [kitchenTableMenuItems objectAtIndex:fromRow];
159  [kitchenTableMenuItems removeObjectAtIndex:fromRow];
160  [kitchenTableMenuItems insertObject:menuItem atIndex:toRow];
161  }
162  break;
163  }
164 }
165 #pragma mark - UITableViewDelegate
166 
167 - (void)tableView:(UITableView *)tableView
168 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
169 {
170  NSInteger selectSection = [indexPath section];
171  NSInteger selectRow = [indexPath row];
172  NSLog(@"V3GalleryEdit.didSelectRowAtIndexPath: sect: %ld row: %ld", (long)selectSection, (long)selectRow);
173  switch (selectSection) {
174  case 0:
175  {
176  MenuItem *selectedMenuItem = [kitchenTableMenuItems objectAtIndex:selectRow];
177 
178  [memDelegate itemSelected:selectedMenuItem];
179  }
180  break;
181  }
182 }
183 
184 - (CGFloat)tableView:(UITableView *)tableView
185 heightForRowAtIndexPath:(NSIndexPath *)indexPath
186 {
187  CGFloat ret = 42.0;
188  if (tableRowImageHeight > 42) {
189  ret = tableRowImageHeight + 2;
190  }
191  return ret;
192 }
193 
194 #pragma mark - section header
195 // - (void) set
196 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
197 {
198  [kitchenTableHeadView appear:self.bounds];
200 
201  NSLog(@"V3KitchenTableView.heightForHeaderInSection: ret: %3.2f", ret);
202  return ret;
203 }
204 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
205 {
206  NSLog(@"V3KitchenTableView.viewForHeaderInSection: section: %ld width: %f", (long)section, self.contentSize.width);
207 
208  return kitchenTableHeadView;
209 }
210 
211 #pragma mark - editting
212 
213 // Override to support conditional editing of the table view.
214 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
215  // Return NO if you do not want the specified item to be editable.
216  return YES;
217 }
218 
219 // Override to support editing the table view.
220 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
221  // xxx [tableView beginUpdates];
222 
223  if (editingStyle == UITableViewCellEditingStyleDelete) {
224  // Delete the row from the data source
225  NSInteger rowNo = [indexPath row];
226  MenuItem *removedItem = [kitchenTableMenuItems objectAtIndex:rowNo];
227  [removedItem setGalleryNumber:0];
228 
229  [kitchenTableMenuItems removeObjectAtIndex:rowNo];
230 
231  [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
232  } else if (editingStyle == UITableViewCellEditingStyleInsert) {
233  // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
234  }
235 
236  // xxx [tableView endUpdates];
237 }
238 
239 
240 
241 // Override to support rearranging the table view.
242 
243 
244 // Override to support conditional rearranging of the table view.
245 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
246  // Return NO if you do not want the item to be re-orderable.
247  return YES;
248 }
249 
250 #pragma mark - Picker View delegate
251 
252 - (CGFloat)pickerView:(UIPickerView *)pickerView
253 rowHeightForComponent:(NSInteger)component
254 {
255  return 48.0;
256 }
257 
258 - (NSAttributedString *)pickerView:(UIPickerView *)pickerView
259  attributedTitleForRow:(NSInteger)row
260  forComponent:(NSInteger)component
261 {
262  NSString *txt;
263  if (0 == row) {
264  txt = @"Removed";
265  } else if (1 == row) {
266  txt = @"Available Now";
267  } else if (2 == row) {
268  txt = @"Off-menu Kitchen Recipes";
269  }
270  UIFont *herefont = [UIFont fontWithName:@"Helvetica-Bold" size:18];
271  NSDictionary *attribs = @{
272  NSForegroundColorAttributeName: [UIColor blackColor],
273  NSFontAttributeName: herefont
274  };
275  NSMutableAttributedString *attributedText =
276  [[NSMutableAttributedString alloc] initWithString:txt
277  attributes:attribs];
278  return attributedText;
279 }
280 
281 - (void)pickerView:(UIPickerView *)pickerView
282  didSelectRow:(NSInteger)row
283  inComponent:(NSInteger)component
284 {
285  NSLog(@"V3GalleryEdit.didSelectRow: row %ld", (long)row);
286 
287  focusKitchenGallery = row;
288  // initialize class var with all menu items for display by the gallery
289  [self fillDataForGallery:(int)focusKitchenGallery];
290  [self reloadData];
291 
292  [memDelegate sizeKitchenTable];
293 }
294 -(void) fillDataForGallery:(int)galleryNo
295 {
296  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
297  NSLog(@"V3GalleryEdit.fillDataForGallery: gallery#: %d", galleryNo);
298  NSNumber *galleryChoice = [NSNumber numberWithInt:galleryNo];
299  NSArray *tmp = [localData retrieveAllMenuItems:galleryChoice];
300  NSLog(@"V3GalleryEdit.fillDataForGallery: count of all menu items in d/b: %ld", (unsigned long)[tmp count]);
301  kitchenTableMenuItems = [[NSMutableArray alloc] initWithArray:tmp];
302 }
303 
304 #pragma mark - Picker View data source
305 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
306 {
307  return 1;
308 }
309 - (NSInteger)pickerView:(UIPickerView *)pickerView
310 numberOfRowsInComponent:(NSInteger)component
311 {
312  return 3;
313 }
314 
315 /*
316 // Only override drawRect: if you perform custom drawing.
317 // An empty implementation adversely affects performance during animation.
318 - (void)drawRect:(CGRect)rect {
319  // Drawing code
320 }
321 */
322 
323 
324 
325 @end
int tableRowImageHeight
NSInteger focusKitchenGallery
id initWithFrame:(CGRect frame)
NSArray< MenuItem * > * retrieveAllMenuItems:(NSNumber *galleryNo)
MenuItem * menuItem
instancetype sharedInstance()
NSInteger getKitchenFocusGallery()
NSDateFormatter * mDateFormatter
id< V3KitchenTableProtocol > memDelegate
id< U7ParamEditControlDelegate > delegate
V3GalleryMenuItemControlbarView * kitchenTableHeadView
Singleton interface to both core and remote data sources.
float tableHeaderHeight
void fillDataForGallery:(int galleryNo)
NSMutableArray< MenuItem * > * kitchenTableMenuItems
void labelizeCell:withText:inColor:andDetail:(UITableViewCell *targCell, [withText] NSString *txt, [inColor] UIColor *color, [andDetail] NSString *detailTxt)