-
Notifications
You must be signed in to change notification settings - Fork 5
/
BLCartViewController.m
281 lines (222 loc) · 10.6 KB
/
BLCartViewController.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
//
// BLCartViewController.m
// ShoppingCart
//
// Created by Bennett Lee on 7/11/14.
// Copyright (c) 2014 Bennett Lee. All rights reserved.
//
#import "BLCartViewController.h"
#import "BLCartItem.h"
#import "BLCartItemCell.h"
#import "BLCartItemCountCell.h"
#import "BLCartTotalCell.h"
#import "BLPickerView.h"
#import "BLCheckoutViewController.h"
@interface BLCartViewController () <UITableViewDelegate, UITableViewDataSource, BLPickerViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) BLPickerView *pickerView;
@property (nonatomic, strong) BLCartItemCountCell *itemCountCell;
@property (nonatomic, strong) BLCartTotalCell *totalCell;
@property (nonatomic, strong) NSMutableDictionary *cachedImages;
@end
@implementation BLCartViewController
#pragma mark Initialization
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder: aDecoder];
if (self) {
_cart = [BLCart sharedCart];
_pickerView = [[BLPickerView alloc] init];
_pickerView.delegate = self;
_cachedImages = [[NSMutableDictionary alloc] init];
}
return self;
}
// When view is loaded
- (void)viewDidLoad{
[super viewDidLoad];
[self.view addSubview:self.pickerView];
}
#pragma mark - Table view data source
// Return the number of sections.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
// Return the number of rows in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return self.cart.items.count; // BLCartItemCells
} else{
return 3; // BLCartItemCountCell, BLCartTotalCell, and checkoutCell
}
}
// Return cell at index path
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0){
// Section 0: Cart cells
static NSString *cartCellIdentifier = @"cartCell";
// Deque or instantiate a BLCartItemCell
BLCartItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cartCellIdentifier];
if (cell == nil) {
cell = [[BLCartItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cartCellIdentifier];
}
// Grab the cart item from cart
BLCartItem *cartItem = [self.cart.items objectAtIndex:indexPath.row];
// Set the cell properties
cell.title.text = cartItem.title;
cell.price.text = [NSString stringWithFormat:@"$ %.2f", [cartItem totalPrice]];
cell.quantityBtn.tag = indexPath.row; // quantityBtn keeps track on which cell it resides in via tag property
[cell.quantityBtn setTitle:[NSString stringWithFormat:@"%i", cartItem.quantity] forState:UIControlStateNormal];
// Styling for quantityBtn
cell.quantityBtn.backgroundColor = [UIColor whiteColor];
cell.quantityBtn.layer.borderColor = [UIColor colorWithRed:236.0f/255.0f green:240.0f/255.0f blue:241.0f/255.0f alpha:1.0f].CGColor;
cell.quantityBtn.layer.borderWidth = 1.0f;
cell.quantityBtn.layer.cornerRadius = 5.0f;
// Handle images. Get back from cache if image exist, else retrieve it from web
// NSString *imageIdentifier = [NSString stringWithFormat:@"IMAGE_%@", cartItem.title];
// if ([self.cachedImages objectForKey:imageIdentifier] != nil){
// cell.imgView.image = [self.cachedImages valueForKey:imageIdentifier];
// } else{
// char const * s = [imageIdentifier UTF8String];
// dispatch_queue_t queue = dispatch_queue_create(s, 0);
//
// dispatch_async(queue, ^{
// NSData *image_data = [[NSData alloc] initWithContentsOfURL:cartItem.image];
// UIImage *image = [UIImage imageWithData:image_data];
//
// // Return back into main queue
// dispatch_async(dispatch_get_main_queue(), ^{
// [self.cachedImages setValue:image forKey:imageIdentifier];
cell.imgView.image = cartItem.image;
// });
// });
// }
return cell;
} else if (indexPath.section == 1){
// Section 1: Car total/Checkout Cell
if (indexPath.row == 0){
// BLCartItemCountCell
static NSString *cartItemCountIdentifier = @"cartItemCountCell";
// Deque or instantiate a BLCartItemCountCell
BLCartItemCountCell *cell = [tableView dequeueReusableCellWithIdentifier:cartItemCountIdentifier];
if (cell == nil) {
cell = [[BLCartItemCountCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cartItemCountIdentifier];
}
self.itemCountCell = cell; // keep a reference to BLCartItemCountCell
// Set cell property
cell.count.text = [NSString stringWithFormat:@"%i", [self.cart totalItems]];
return cell;
} else if (indexPath.row == 1){
// BLCartTotalCell
static NSString *cartTotalIdentifier = @"cartTotalCell";
// Deque or instantiate a BLCartTotalCell
BLCartTotalCell *cell = [tableView dequeueReusableCellWithIdentifier:cartTotalIdentifier];
if (cell == nil) {
cell = [[BLCartTotalCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cartTotalIdentifier];
}
// Set cell property
cell.totalAmount.text = [NSString stringWithFormat:@"$ %.2f", [self.cart totalAmount]];
self.totalCell = cell; // keep a reference to BLCartTotalCell
return cell;
} if (indexPath.row == 2){
// CheckoutCell
static NSString *cartCheckoutIdentifier = @"checkoutCell";
// Deque or instantiate a checkoutCell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cartCheckoutIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cartCheckoutIdentifier];
}
return cell;
}
}
return nil;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// Only allow editing in the first section
return (indexPath.section == 0) ? YES : NO;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self removeItemAtIndexPath:indexPath];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Return height for cells
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return (indexPath.section == 0) ? 105.0 : 55.0;
}
// Return title for sections
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return (section == 0) ? @"Items" : @"Summary";
}
// Deselect cell when it's selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO];
}
#pragma mark Actions
// Update quantity button has been pressed
- (IBAction)quantityBtnPressed:(UIButton *)button {
// Get the cells index path
UITableViewCell* cell = (UITableViewCell*)button.superview.superview.superview;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
BLCartItem *cartItem = [self.cart.items objectAtIndex:indexPath.row];
// Show picker with inventory count
[self.pickerView showWithCartItem:cartItem cellIndex:indexPath.row];
}
#pragma mark Instance Methods
// Re-calcuate subtotal of view and number of items
- (void)refreshSummary{
// Set item count if it's not nil
if (self.itemCountCell){
self.itemCountCell.count.text = [NSString stringWithFormat:@"%i", [self.cart totalItems]];
}
// Set total if not total cell is not nil
if (self.totalCell){
self.totalCell.totalAmount.text = [NSString stringWithFormat:@"$ %.2f", [self.cart totalAmount]];
}
}
#pragma mark - Scroll View Delegate
// Hide BLPickerView whenever the user starts scrolling
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.pickerView hide];
}
#pragma mark - Picker view delegate
// Remove index path
- (void)removeItemAtIndexPath:(NSIndexPath *)indexPath{
// Delete the row from the cart items
[self.cart.items removeObjectAtIndex:indexPath.row];
// Delete the row from view
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Refersh summary
[self refreshSummary];
// Save cart changes to disk
[self.cart saveToDisk];
}
// Update cart item and reload table data
- (void)updateCartItemAtIndex:(NSInteger)index withQuantity:(NSInteger)quantity{
[self.cart updateItemAtIndex:index withQuantity:quantity];
// Update the cell button with new quantity
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
// Update cart quantity, item total, and cart total
BLCartItem *cartItem = [self.cart.items objectAtIndex:index];
BLCartItemCell *cartItemCell = (BLCartItemCell *) [self.tableView cellForRowAtIndexPath:indexPath];
[cartItemCell.quantityBtn setTitle:[NSString stringWithFormat:@"%li", (long)quantity] forState:UIControlStateNormal];
cartItemCell.price.text = [NSString stringWithFormat:@"$ %.2f", [cartItem totalPrice]];
// Refresh summary
[self refreshSummary];
// Save cart to disk
[self.cart saveToDisk];
}
#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
// Set BLCheckoutViewController's checkout url to shopify's url
if ([segue.identifier isEqual: @"checkoutSegue"]){
BLCheckoutViewController *bcvc = [segue destinationViewController];
bcvc.url = [self.cart checkoutURL];
}
}
@end