-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLBasketViewController.m
292 lines (240 loc) · 9.93 KB
/
PLBasketViewController.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// PLBasketViewController.m
// Plate
//
// Created by emileleon on 1/24/14.
// Copyright (c) 2014 Plate SF. All rights reserved.
//
#import "PLBasketViewController.h"
#import "PLBasketStore.h"
#import "PLAddOnItem.h"
#import "PLALaCarteItem.h"
#import "PLPlate.h"
#import "Colours.h"
@interface PLBasketViewController ()
@end
@implementation PLBasketViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *theCell;
switch ([indexPath section]) {
case 0: { // Plates section
PLBasketViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PLBasketViewTableCellPlate"];
if (cell == nil) {
cell = [[PLBasketViewTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PLBasketViewTableCellPlate"];
}
cell.itemType = Plate;
cell.itemIndex = [indexPath row];
cell.delegate = self;
PLPlate *plate = (PLPlate *)[[[PLBasketStore sharedStore] plates] objectAtIndex:[indexPath row]];
if ([plate main]) {
cell.itemNameLabel.text = [[plate main] name];
} else {
cell.itemNameLabel.text = @"No main";
}
if ([plate sides]) {
PLMenuItem *side = [[plate sides] objectAtIndex:0];
if (side) {
cell.side1Label.text = [side name];
} else {
cell.side1Label.text = @"";
}
side = [[plate sides] objectAtIndex:1];
if (side) {
cell.side2Label.text = [side name];
} else {
cell.side2Label.text = @"";
}
NSString *slug = [[[[PLBasketStore sharedStore] plateBuilder] plateTypeSize] typeSlug];
if ([slug isEqualToString:FourSides]) {
side = [[plate sides] objectAtIndex:2];
cell.side3Label.text = [side name];
side = [[plate sides] objectAtIndex:3];
cell.side4Label.text = [side name];
} else {
cell.side3Label.text = @"";
cell.side4Label.text = @"";
}
}
if ([plate size] == Ultra) {
cell.plateSizeLabel.text = @"Ultra";
cell.priceLabel.text = @"$15";
} else if ([plate size] == Fit) {
cell.plateSizeLabel.text = @"Fit";
cell.priceLabel.text = @"$12";
} else if ([plate size] == Kids) {
cell.plateSizeLabel.text = @"Kids";
cell.priceLabel.text = @"$8";
}
theCell = cell;
break;
}
case 1: { // A La Carte section
PLBasketViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PLBasketViewTableCellAddOn"];
if (cell == nil) {
cell = [[PLBasketViewTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PLBasketViewTableCellAddOn"];
}
cell.itemType = ALaCarte;
cell.delegate = self;
cell.itemIndex = [indexPath row];
PLALaCarteItem *item = nil;
item = (PLALaCarteItem *)[[[PLBasketStore sharedStore] alaCarteItems] objectAtIndex:[indexPath row]];
cell.itemId = item.plateId;
cell.itemNameLabel.text = item.name;
cell.quantityLabel.text = [NSString stringWithFormat:@"%d",[item quantity]];
cell.priceLabel.text = [NSString stringWithFormat:@"$%.2f", item.price * [item quantity]];
theCell = cell;
break;
}
case 2: { // Add On Section
PLBasketViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PLBasketViewTableCellAddOn"];
if (cell == nil) {
cell = [[PLBasketViewTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PLBasketViewTableCellAddOn"];
}
cell.itemType = AddOn;
cell.delegate = self;
cell.itemIndex = [indexPath row];
PLAddOnItem *item = nil;
item = (PLAddOnItem *)[[[PLBasketStore sharedStore] addOns] objectAtIndex:[indexPath row]];
cell.itemId = item.plateId;
cell.itemNameLabel.text = item.name;
cell.quantityLabel.text = [NSString stringWithFormat:@"%d",[item quantity]];
float price = item.price * [item quantity];
cell.priceLabel.text = [NSString stringWithFormat:@"$%.2f", price];
theCell = cell;
break;
}
}
return theCell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0: {
if ([[[PLBasketStore sharedStore] plates] count] > 0) {
return @"Plates";
} else {
return nil;
}
}
case 1: {
if ([[[PLBasketStore sharedStore] alaCarteItems] count] > 0) {
return @"A La Carte";
} else {
return nil;
}
}
case 2: {
if ([[[PLBasketStore sharedStore] addOns] count] > 0) {
return @"Add Ons";
} else {
return nil;
}
}
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0: {
return [[[PLBasketStore sharedStore] plates] count];
}
case 1: {
return [[[PLBasketStore sharedStore] alaCarteItems] count];
}
case 2: {
return [[[PLBasketStore sharedStore] addOns] count];
}
}
return 0;
}
-(void)reloadTableViewData
{
[[self basketTable] reloadData];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTableViewData)
name:@"BasketUpdated" object:nil];
}
return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return 123.0f;
} else {
return 82.0f;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nibAddOn = [UINib nibWithNibName:@"PLBasketViewTableCellAddOn" bundle:nil];
[[self basketTable] registerNib:nibAddOn forCellReuseIdentifier:@"PLBasketViewTableCellAddOn"];
UINib *nibPlate = [UINib nibWithNibName:@"PLBasketViewTableCellPlate" bundle:nil];
[[self basketTable] registerNib:nibPlate forCellReuseIdentifier:@"PLBasketViewTableCellPlate"];
// Setup the navigation bar
[[self navigationItem] setTitle:@"My Basket"];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:@"Helvetica" size:24], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = attributes;
// Setup the bottom toolbar
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Continue to Payment" style:UIBarButtonItemStylePlain target:self action:@selector(actionContinue:)];
[barButton setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:18.0],
NSForegroundColorAttributeName: [UIColor moneyGreenColor]
} forState:UIControlStateNormal];
self.toolbarItems = [NSArray arrayWithObjects: flexibleSpaceLeft, barButton, nil];
// We don't want extra space above tables for scrolling
self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)viewDidAppear:(BOOL)animated
{
self.navigationController.toolbarHidden = NO;
}
- (IBAction)actionContinue:(id)sender {
// if ([[PLBasketStore sharedStore] quantityOfAllItemsInBasket] > 0) {
// GO TO THE PAYMENT PAGE
NSLog(@"Displaying payment modal from BasketView");
_plpvc = [[PLPaymentViewController alloc]init];
_plpvc.displaySaveToAccount = true;
[[self plpvc] setDelegate:self];
//[self presentViewController:_plpvc animated:YES completion:nil];
[[self navigationController] pushViewController:_plpvc animated:YES];
// }
}
- (void)dismissModal
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)paymentAdded
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)removeItemWithCell:(PLBasketViewTableCell *)cell
{
if (cell.itemType == Plate) {
[[[PLBasketStore sharedStore] plates] removeObjectAtIndex:[cell itemIndex]];
} else if (cell.itemType == ALaCarte) {
[[[PLBasketStore sharedStore] alaCarteItems] removeObjectAtIndex:[cell itemIndex]];
} else if (cell.itemType == AddOn) {
[[[PLBasketStore sharedStore] addOns] removeObjectAtIndex:[cell itemIndex]];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"BasketUpdated" object:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end