-
Notifications
You must be signed in to change notification settings - Fork 3
/
ListingDetailsViewController.m
335 lines (276 loc) · 10.1 KB
/
ListingDetailsViewController.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// ListingDetailsViewController.m
// My eBay App
//
// Created by Jon Reid on 11/1/10, adapted by William Yang on 11/4/24.
// Copyright 2010 eBay Inc. All rights reserved.
//
#import "ListingDetailsViewController.h"
#import "ImageLoader.h"
#import "PriceFormatter.h"
#import "ProblemLogger.h"
#import "TimeLeft.h"
@interface ListingDetailsViewController ()
@property (nonatomic, retain) NSString *loadedItemID;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *priceLabel;
@property (nonatomic, retain) UILabel *priceTypeLabel;
@property (nonatomic, retain) UILabel *itemIDLabel;
@property (nonatomic, retain) UILabel *startTimeLabel;
@property (nonatomic, retain) UILabel *endTimeLabel;
@property (nonatomic, retain) UILabel *timeLeftLabel;
@property (nonatomic, retain) UILabel *conditionLabel;
@property (nonatomic, retain) UILabel *listingTypeLabel;
@property (nonatomic, retain) UILabel *shippingCostLabel;
@property (nonatomic, retain) UILabel *locationLabel;
@property (nonatomic, retain) UILabel *paymentMethodLabel;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) ImageLoader *imageLoader;
@property (nonatomic, retain) Shopping_GetSingleItemRequestType *currentGetSingleItemRequest;
- (void)getSingleItem:(NSString *)itemID;
- (void)clearDisplayWhileWaitingForData;
@end
@implementation ListingDetailsViewController
@synthesize selectedItem;
@synthesize isFixedPrice;
@synthesize temporaryImage;
@synthesize loadedItemID;
@synthesize titleLabel;
@synthesize priceTypeLabel;
@synthesize priceLabel;
@synthesize itemIDLabel;
@synthesize startTimeLabel;
@synthesize endTimeLabel;
@synthesize timeLeftLabel;
@synthesize conditionLabel;
@synthesize listingTypeLabel;
@synthesize shippingCostLabel;
@synthesize locationLabel;
@synthesize paymentMethodLabel;
@synthesize imageView;
@synthesize imageLoader;
@synthesize currentGetSingleItemRequest;
#pragma mark -
#pragma mark Initialize & Cleanup
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil)
{
[self setTitle:@"Details"];
}
return self;
}
- (void)dealloc
{
[selectedItem release];
[temporaryImage release];
[loadedItemID release];
[titleLabel release];
[priceTypeLabel release];
[priceLabel release];
[itemIDLabel release];
[startTimeLabel release];
[endTimeLabel release];
[timeLeftLabel release];
[conditionLabel release];
[listingTypeLabel release];
[shippingCostLabel release];
[locationLabel release];
[paymentMethodLabel release];
[imageView release];
[imageLoader release];
[currentGetSingleItemRequest release];
[super dealloc];
}
#pragma mark -
#pragma mark View Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
imageLoader = [[ImageLoader alloc] initWithImageView:imageView];
[imageLoader setKeepImageWhileURLIsLoading:YES];
}
- (void)viewDidUnload
{
[self setTemporaryImage:nil];
[self setLoadedItemID:nil];
[self setTitleLabel:nil];
[self setPriceTypeLabel:nil];
[self setPriceLabel:nil];
[self setItemIDLabel:nil];
[self setStartTimeLabel:nil];
[self setEndTimeLabel:nil];
[self setTimeLeftLabel:nil];
[self setConditionLabel:nil];
[self setListingTypeLabel:nil];
[self setShippingCostLabel:nil];
[self setLocationLabel:nil];
[self setPaymentMethodLabel:nil];
[self setImageView:nil];
[self setImageLoader:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Set the fields we already have
[titleLabel setText:selectedItem.title];
FindingService_Amount *initialCurrentPrice = selectedItem.sellingStatus.currentPrice;
[priceLabel setText:[PriceFormatter stringFromValue:initialCurrentPrice.value
currency:initialCurrentPrice.currencyId]];
[itemIDLabel setText:selectedItem.itemId];
[conditionLabel setText:selectedItem.condition.conditionDisplayName];
[listingTypeLabel setText:selectedItem.listingInfo.listingType];
FindingService_Amount *shippingServiceCost = selectedItem.shippingInfo.shippingServiceCost;
[shippingCostLabel setText:[PriceFormatter stringFromValue:shippingServiceCost.value
currency:shippingServiceCost.currencyId]];
[paymentMethodLabel setText:[selectedItem.paymentMethod componentsJoinedByString:@", "]];
if (isFixedPrice)
{
[priceTypeLabel setText:@"Buy It Now: "];
}
else
{
[priceTypeLabel setText:@"Current Bid:"];
}
// Request the remaining fields we are missing or want to update
if (![loadedItemID isEqualToString:selectedItem.itemId])
{
[self clearDisplayWhileWaitingForData];
}
[self getSingleItem:selectedItem.itemId];
}
- (void)viewWillDisappear:(BOOL)animated
{
[imageLoader cancelDownload];
if (currentGetSingleItemRequest != nil)
{
EBayServiceInvoker *serviceInvoker = [EBayServiceInvokerFactory serviceForType:kEBayServiceTypeShopping];
[serviceInvoker cancel:currentGetSingleItemRequest];
[self setCurrentGetSingleItemRequest:nil];
}
[super viewWillDisappear:animated];
}
#pragma mark -
#pragma mark API Call & Application Logic
/**
Invokes request to eBay Shopping service for GetSingleItem
The response is delivered to didReceiveResponse:forRequest: below.
*/
- (void)getSingleItem:(NSString *)itemID
{
@try
{
// Get service invoker
EBayServiceInvoker *serviceInvoker = [EBayServiceInvokerFactory serviceForType:kEBayServiceTypeShopping];
serviceInvoker.debugMode = YES;
// Create request
Shopping_GetSingleItemRequestType *request = [[[Shopping_GetSingleItemRequestType alloc] init] autorelease];
request.itemID = itemID;
request.includeSelector = @"Details,ShippingCosts";
[self setCurrentGetSingleItemRequest:request]; // Remember request so -viewWillDisappear: can cancel it
// Invoke request
[serviceInvoker invoke:request withDelegate:self];
}
@catch (EBayException *exception)
{
[ProblemLogger logEBayException:exception];
}
}
/// Updates display using data in GetSingleItem response
- (void)handleGetSingleItemResponse:(Shopping_GetSingleItemResponseType *)response
{
Shopping_SimpleItemType *item = response.item;
[self setLoadedItemID:item.itemID];
[priceLabel setText:[PriceFormatter stringFromValue:item.currentPrice.value
currency:item.currentPrice.currencyID]];
static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
[dateFormatter setTimeStyle:kCFDateFormatterMediumStyle];
}
[startTimeLabel setText:[dateFormatter stringFromDate:item.startTime]];
[endTimeLabel setText:[dateFormatter stringFromDate:item.endTime]];
TimeLeft *timeLeft = [[TimeLeft alloc] initWithISODuration:item.timeLeft];
[timeLeftLabel setText:[timeLeft shortString]];
[timeLeftLabel setTextColor:[timeLeft textColor]];
[timeLeft release];
[locationLabel setText:item.location];
[imageLoader setImageURL:[item.pictureURL objectAtIndex:0]];
}
/// Clears information that will be filled in by -handleGetSingleItemResponse:
- (void)clearDisplayWhileWaitingForData
{
[imageView setImage:temporaryImage];
[startTimeLabel setText:nil];
[endTimeLabel setText:nil];
[timeLeftLabel setText:nil];
[locationLabel setText:nil];
}
- (IBAction)viewOnMobileWebByItemID:(id)sender
{
@try
{
EBayAffiliateTracking *affiliateTracking = [EBayAffiliateTracking sharedInstance];
// construct roverirzed URL with specific item id and launch the URL in external browser.
[affiliateTracking launchViewItemPageWithItem:[selectedItem itemId]];
}
@catch (NSException *exception)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Affiliate Tracking error"
message:[exception reason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (IBAction)viewOnMobileWebByRoverizedURL:(id)sender
{
@try
{
EBayAffiliateTracking *affiliateTracking = [EBayAffiliateTracking sharedInstance];
// launch already roverized URL returned by API call
[affiliateTracking launchNonMobileRoverizedURLString:[selectedItem viewItemURL]];
}
@catch (NSException *exception)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Affiliate Tracking error"
message:[exception reason]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
#pragma mark -
#pragma mark EBayServiceDelegate
- (void)didReceiveResponse:(id)response forRequest:(id)request
{
if ([response isKindOfClass:[Shopping_GetSingleItemResponseType class]])
{
[self setCurrentGetSingleItemRequest:nil];
[self handleGetSingleItemResponse:response];
}
}
- (void)didFailWithErrors:(NSArray*)errors forRequest:(id)request
{
if ([request isKindOfClass:[Shopping_GetSingleItemRequestType class]])
{
[self setCurrentGetSingleItemRequest:nil];
}
[ProblemLogger logEBayErrors:errors];
}
- (void)didCancelRequest:(id)request
{
if ([request isKindOfClass:[Shopping_GetSingleItemRequestType class]])
{
[self setCurrentGetSingleItemRequest:nil];
}
}
@end