-
Notifications
You must be signed in to change notification settings - Fork 95
/
ProductDetailsViewController.m
executable file
·173 lines (141 loc) · 4.74 KB
/
ProductDetailsViewController.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
//
// ProductDetailsViewController.m
// Super Checkout
//
// Created by Brandon Alexander on 3/8/11.
// Copyright 2011 While This, Inc. All rights reserved.
//
#import "ProductDetailsViewController.h"
#import "QuantityCell.h"
#import "SuperCheckoutAPIEngine.h"
@implementation ProductDetailsViewController
@synthesize selectedProduct;
@synthesize productDetailsHeader;
@synthesize productImage;
@synthesize productNameLabel;
@synthesize productPriceLabel;
@synthesize addToBasketCell;
@synthesize quantityCell;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
apiEngine = [[SuperCheckoutAPIEngine alloc] initWithDelegate:self];
}
return self;
}
- (void)dealloc
{
[selectedProduct release];
[productDetailsHeader release];
[productImage release];
[productNameLabel release];
[productPriceLabel release];
[addToBasketCell release];
[quantityCell release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return 0;
} else {
return 1;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if(section == 0) {
return [selectedProduct objectForKey:@"description"];
} else {
return nil;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath section] == 1) {
return quantityCell;
} else {
return addToBasketCell;
}
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Add item to cart here.....
[apiEngine buyProduct:[selectedProduct objectForKey:@"id"] withQuantity:[NSNumber numberWithInt:[quantityCell quantity]]];
}
- (NSIndexPath *)tableView:(UITableView *)tableview willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath section] == 2) {
return indexPath;
} else {
return nil;
}
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(section == 0) {
return productDetailsHeader;
} else {
return nil;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 0) {
return [productDetailsHeader frame].size.height;
} else {
return 0.0;
}
}
#pragma mark - SuperCheckoutAPIEngineDelegate Methods
- (void)requestSucceeded:(NSString *)connectionIdentifier {
}
- (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"An error occurred adding this item" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
}
-(void) cartContentsReceived:(NSDictionary *)cart forRequest:(NSString *)connectionIdentifier {
NSLog(@"Shopping cart contents: %@", cart);
NSNotification *note = [NSNotification notificationWithName:@"CartUpdated" object:[NSNumber numberWithInt:[[cart objectForKey:@"items"] count]]];
[[NSNotificationCenter defaultCenter] postNotification:note];
[self.navigationController popViewControllerAnimated:YES];
}
-(void) imageReceived:(UIImage *)image forRequest:(NSString *)connectionIdentifier {
[productImage setImage:image];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setTitle:[selectedProduct objectForKey:@"name"]];
[productNameLabel setText:[selectedProduct objectForKey:@"name"]];
[productPriceLabel setText:[NSString stringWithFormat:@"$%1.2f", [[selectedProduct objectForKey:@"price"] floatValue]]];
[apiEngine getImageForProduct:[selectedProduct objectForKey:@"image"]];
}
- (void)viewDidUnload
{
[self setProductDetailsHeader:nil];
[self setProductImage:nil];
[self setProductNameLabel:nil];
[self setProductPriceLabel:nil];
[self setAddToBasketCell:nil];
[self setQuantityCell:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end