-
Notifications
You must be signed in to change notification settings - Fork 0
/
PLAddOnViewController.m
220 lines (177 loc) · 7.04 KB
/
PLAddOnViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// PLAddOnViewController.m
// Plate
//
// Created by emileleon on 1/16/14.
// Copyright (c) 2014 Plate SF. All rights reserved.
//
#import "PLAddOnViewController.h"
#import "PLBasketStore.h"
#import "PLAddOnItem.h"
#import "PLMenuItemTableViewCell.h"
#import "PLALaCarteSummaryViewController.h"
#import "PLAddOnSummaryViewController.h"
#import "Colours.h"
#import "PLPlateStore.h"
#import "PLMenu.h"
#import "PLItemDetailController.h"
@interface PLAddOnViewController ()
@end
@implementation PLAddOnViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PLMenuItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PLMenuItemTableViewCell"];
if (cell == nil) {
cell = [[PLMenuItemTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PLMenuItemTableViewCell"];
}
[cell setupDelegate:self parentTable:(UITableView *)tableView];
PLAddOnItem *item = nil;
item = [[self addOns] objectAtIndex:[indexPath row]];
cell.itemNameLabel.text = [item name];
cell.quantityLabel.text = [NSString stringWithFormat:@"%d",[[PLBasketStore sharedStore] quantityOfItemInAddOnBuilder: item]]; // get value from basket builder
cell.itemId = [item plateId];
return cell;
}
- (void)displayItemDetail:(PLMenuItemTableViewCell *)cell
{
PLAddOnItem *item = [self getItemFromCell:cell];
PLItemDetailController *plidc = [[PLItemDetailController alloc]initWithMenuItem:item];
[plidc setDelegate:self];
[self presentViewController:plidc animated:YES completion:nil];
}
- (void)dismissItemDetailView
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (PLAddOnItem *)getItemFromCell:(PLMenuItemTableViewCell *)cell
{
PLAddOnItem *item = nil;
for (PLAddOnItem *addOnItem in self.addOns) {
if ([cell.itemId isEqualToString:[addOnItem plateId]]) {
item = addOnItem;
break;
}
}
return item;
}
// Action for + button; adds the item to the builder
-(int)addItemWithCell:(PLMenuItemTableViewCell *)cell
{
PLAddOnItem *item = [self getItemFromCell:cell];
int numItems = [[PLBasketStore sharedStore] addAddOnItem:item];
[self setStatusButton];
return numItems;
}
// Action for - button; removes the item from the builder
-(int)removeItemWithCell:(PLMenuItemTableViewCell *)cell
{
PLAddOnItem *item = [self getItemFromCell:cell];
int numItems = [[PLBasketStore sharedStore] removeAddOnItem:item];
[self setStatusButton];
return numItems;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTableViewData)
name:@"AddOnBuilderEmptied" object:nil];
}
return self;
}
- (void)reloadTableViewData
{
[self.addOnsTable reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self addOns] count];
}
- (void)setStatusButton
{
UIBarButtonItem *statusButton = [[self toolbarItems] objectAtIndex:0];
int numAddOnsAdded = [[PLBasketStore sharedStore] quantityOfItemsInAddOnBuilder];
NSString *status = [NSString stringWithFormat:@"%d items selected", numAddOnsAdded];
[statusButton setTitle:status];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Display the Continue button
self.navigationController.toolbarHidden = NO;
// We don't want extra space above tables for scrolling
self.automaticallyAdjustsScrollViewInsets = NO;
self.addOnsTable.backgroundColor = [UIColor clearColor];
[self.addOnsTable reloadData];
[super displayBasketInNavBar];
[self setStatusButton];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"PLMenuItemTableViewCell" bundle:nil];
[[self addOnsTable] registerNib:nib forCellReuseIdentifier:@"PLMenuItemTableViewCell"];
[self fetchAddOnItems];
[self setTitle:@"Add Ons"];
// Setup the navigation bar
[[self navigationItem] setTitle:@"Add Ons"];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:@"Helvetica" size:24], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = attributes;
}
- (void)fetchAddOnItems
{
UIView *currentTitleView = [[self navigationItem] titleView];
UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[[self navigationItem] setTitleView:aiView];
[aiView startAnimating];
__weak PLAddOnViewController *weakSelf = self;
[[PLPlateStore sharedStore]getAddOnMenu:^(PLMenu *menuResult, NSError *err) {
[[weakSelf navigationItem] setTitleView:currentTitleView];
if (!err) {
_addOns = [menuResult addons];
[[weakSelf addOnsTable] reloadData];
} else {
UIAlertView *av =[[UIAlertView alloc]
initWithTitle:@"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}
}];
}
//-(void)fetchAddOnItems
//{
// NSMutableArray *addOnItems = [[NSMutableArray alloc]init];
//
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Coca Cola" itemType:MenuItemAddOn itemId:@"1"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Cheez Whiz" itemType:MenuItemAddOn itemId:@"2"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Faygo" itemType:MenuItemAddOn itemId:@"3"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Frito Lays" itemType:MenuItemAddOn itemId:@"4"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Chips Ahoy" itemType:MenuItemAddOn itemId:@"5"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Saltines" itemType:MenuItemAddOn itemId:@"6"]];
// [addOnItems addObject:[[PLAddOnItem alloc]initWithName:@"Vanilla Wafers" itemType:MenuItemAddOn itemId:@"7"]];
//
// for (PLAddOnItem *item in addOnItems) {
// item.price = 2.0;
// }
//
// [self setAddOns:addOnItems];
//}
- (IBAction)actionContinue:(id)sender {
[[self navigationController] pushViewController:[[PLAddOnSummaryViewController alloc]init] animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end