This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathPFProductTableViewController.m
249 lines (201 loc) · 8.95 KB
/
PFProductTableViewController.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
/*
* Copyright (c) 2014, Parse, LLC. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Parse.
*
* As with any software that integrates with the Parse platform, your use of
* this software is subject to the Parse Terms of Service
* [https://www.parse.com/about/terms]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#import "PFProductTableViewController.h"
#import <Parse/PFProduct.h>
#import <Parse/PFPurchase.h>
#import <Parse/PFQuery.h>
#import "PFUIAlertView.h"
#import "PFLocalization.h"
#import "PFPurchaseTableViewCell.h"
static NSString *const PFProductMetadataPriceKey = @"price";
static NSString *const PFProductMetadataPriceLocaleKey = @"priceLocale";
@interface PFProductTableViewController () <SKProductsRequestDelegate> {
NSMutableDictionary *_productMetadataDictionary;
NSMutableDictionary *_productProgressDictionary;
SKProductsRequest *_storeProductsRequest;
}
@end
@implementation PFProductTableViewController
#pragma mark -
#pragma mark NSObject
- (instancetype)initWithStyle:(UITableViewStyle)style {
if (self = [super initWithStyle:UITableViewStylePlain className:[PFProduct parseClassName]]) {
self.pullToRefreshEnabled = NO;
self.paginationEnabled = NO;
_productMetadataDictionary = [NSMutableDictionary dictionary];
_productProgressDictionary = [NSMutableDictionary dictionary];
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style className:(NSString *)className {
return [self initWithStyle:style];
}
#pragma mark -
#pragma mark UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 84.0f;
}
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
if (error) {
return;
}
[self.objects enumerateObjectsUsingBlock:^(PFProduct *product, NSUInteger idx, BOOL *stop) {
// No download for this product - just continue
if (!product.downloadName) {
return;
}
[PFPurchase addObserverForProduct:product.productIdentifier block:^(SKPaymentTransaction *transaction) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
PFPurchaseTableViewCell *cell = (PFPurchaseTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.state = PFPurchaseTableViewCellStateDownloading;
[PFPurchase downloadAssetForTransaction:transaction
completion:^(NSString *filePath, NSError *error) {
if (!error) {
cell.state = PFPurchaseTableViewCellStateDownloaded;
} else {
cell.state = PFPurchaseTableViewCellStateNormal;
NSString *title = PFLocalizedString(@"Download Error",
@"Download Error");
[PFUIAlertView presentAlertInViewController:self withTitle:title error:error];
}
}
progress:^(int percentDone) {
_productProgressDictionary[product.productIdentifier] = @(percentDone);
[cell.progressView setProgress:percentDone/100.0f animated:YES];
}];
}];
}];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFProduct *)product {
static NSString *cellIdentifier = @"PFPurchaseTableViewCell";
PFPurchaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[PFPurchaseTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier];
}
if (indexPath.row % 2 == 0) {
cell.backgroundView.backgroundColor = [UIColor colorWithWhite:242.0f/255.0f alpha:1.0f];
cell.textLabel.shadowOffset = CGSizeZero;
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.priceLabel.shadowOffset = CGSizeZero;
cell.priceLabel.shadowColor = [UIColor whiteColor];
} else {
cell.backgroundView.backgroundColor = [UIColor colorWithWhite:232.0f/255.0f alpha:1.0f];
cell.textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.priceLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
cell.priceLabel.shadowColor = [UIColor whiteColor];
}
cell.imageView.file = product.icon;
cell.textLabel.text = product.title;
cell.detailTextLabel.text = product.subtitle;
NSString *price = [self _priceForProduct:product];
if (price) {
cell.priceLabel.text = [NSString stringWithFormat:@" $%@ ", price];
}
if (product.downloadName) {
NSString *contentPath = [PFPurchase assetContentPathForProduct:product];
if (contentPath) {
cell.state = PFPurchaseTableViewCellStateDownloaded;
}
} else {
int progress = [self _downloadProgressForProduct:product];
if (progress == 0) {
cell.state = PFPurchaseTableViewCellStateNormal;
} else if (progress == 100) {
cell.state = PFPurchaseTableViewCellStateDownloaded;
} else {
cell.state = PFPurchaseTableViewCellStateDownloading;
}
}
return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < self.objects.count) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFProduct *product = self.objects[indexPath.row];
[PFPurchase buyProduct:product.productIdentifier block:^(NSError *error) {
if (error) {
NSString *title = PFLocalizedString(@"Purchase Error", @"Purchase Error");
[PFUIAlertView presentAlertInViewController:self withTitle:title error:error];
}
}];
}
}
#pragma mark -
#pragma mark Data
- (NSString *)_priceForProduct:(PFProduct *)product {
return _productMetadataDictionary[product.productIdentifier][PFProductMetadataPriceKey];
}
- (int)_downloadProgressForProduct:(PFProduct *)product {
return [_productProgressDictionary[product.productIdentifier] intValue];
}
#pragma mark -
#pragma mark PFQueryTableViewController
- (PFQuery *)queryForTable {
PFQuery *query = [super queryForTable];
[query orderByAscending:@"order"];
return query;
}
#pragma mark -
#pragma mark Querying Store
- (void)_queryStoreForProductsWithIdentifiers:(NSSet *)identifiers {
_storeProductsRequest.delegate = nil;
_storeProductsRequest = nil;
_storeProductsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
_storeProductsRequest.delegate = self;
[_storeProductsRequest start];
}
#pragma mark -
#pragma mark SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
if (request != _storeProductsRequest) {
return;
}
NSArray *validProducts = response.products;
if ([validProducts count] == 0) {
return;
}
[validProducts enumerateObjectsUsingBlock:^(SKProduct *product, NSUInteger idx, BOOL *stop) {
NSDictionary *metadata = @{ PFProductMetadataPriceKey : product.price,
PFProductMetadataPriceLocaleKey : product.priceLocale };
_productMetadataDictionary[product.productIdentifier] = metadata;
}];
[self.tableView reloadData];
_storeProductsRequest.delegate = nil;
}
- (void)requestDidFinish:(SKRequest *)request {
_storeProductsRequest.delegate = nil;
_storeProductsRequest = nil;
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
_storeProductsRequest.delegate = nil;
_storeProductsRequest = nil;
}
@end