-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathELCAssetTablePicker.m
262 lines (205 loc) · 7.08 KB
/
ELCAssetTablePicker.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
//
// ELCAssetTablePicker.m
//
// Created by ELC on 2/15/11.
// Copyright 2011 ELC Technologies. All rights reserved.
//
#import "ELCAssetTablePicker.h"
#import "ELCAssetCell.h"
#import "ELCAsset.h"
#import "ELCAlbumPickerController.h"
#import "ELCConsole.h"
@interface ELCAssetTablePicker ()
@property (nonatomic, assign) int columns;
@end
@implementation ELCAssetTablePicker
//Using auto synthesizers
- (id)init
{
self = [super init];
if (self) {
//Sets a reasonable default bigger then 0 for columns
//So that we don't have a divide by 0 scenario
self.columns = 4;
}
return self;
}
- (void)viewDidLoad
{
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setAllowsSelection:NO];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.elcAssets = tempArray;
if (self.immediateReturn) {
} else {
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)];
[self.navigationItem setRightBarButtonItem:doneButtonItem];
//minha edicao
[doneButtonItem setTitle:@"Pronto"];
//fim de edicao
[self.navigationItem setTitle:@"Loading..."];
}
[self performSelectorInBackground:@selector(preparePhotos) withObject:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.columns = self.view.bounds.size.width / 80;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[ELCConsole mainConsole] removeAllIndex];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.columns = self.view.bounds.size.width / 80;
[self.tableView reloadData];
}
- (void)preparePhotos
{
@autoreleasepool {
[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result == nil) {
return;
}
ELCAsset *elcAsset = [[ELCAsset alloc] initWithAsset:result];
[elcAsset setParent:self];
BOOL isAssetFiltered = NO;
if (self.assetPickerFilterDelegate &&
[self.assetPickerFilterDelegate respondsToSelector:@selector(assetTablePicker:isAssetFilteredOut:)])
{
isAssetFiltered = [self.assetPickerFilterDelegate assetTablePicker:self isAssetFilteredOut:(ELCAsset*)elcAsset];
}
if (!isAssetFiltered) {
[self.elcAssets addObject:elcAsset];
}
}];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
// scroll to bottom
long section = [self numberOfSectionsInTableView:self.tableView] - 1;
long row = [self tableView:self.tableView numberOfRowsInSection:section] - 1;
if (section >= 0 && row >= 0) {
NSIndexPath *ip = [NSIndexPath indexPathForRow:row
inSection:section];
[self.tableView scrollToRowAtIndexPath:ip
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
[self.navigationItem setTitle:self.singleSelection ? @"Pick Photo" : @"Escolha as fotos"];
});
}
}
- (void)doneAction:(id)sender
{
NSMutableArray *selectedAssetsImages = [[NSMutableArray alloc] init];
for (ELCAsset *elcAsset in self.elcAssets) {
if ([elcAsset selected]) {
[selectedAssetsImages addObject:elcAsset];
}
}
if ([[ELCConsole mainConsole] onOrder]) {
[selectedAssetsImages sortUsingSelector:@selector(compareWithIndex:)];
}
// NSLog(@"%@",selectedAssetsImages);
[self.parent selectedAssets:selectedAssetsImages];
}
- (BOOL)shouldSelectAsset:(ELCAsset *)asset
{
NSUInteger selectionCount = 0;
for (ELCAsset *elcAsset in self.elcAssets) {
if (elcAsset.selected) selectionCount++;
}
BOOL shouldSelect = YES;
if ([self.parent respondsToSelector:@selector(shouldSelectAsset:previousCount:)]) {
shouldSelect = [self.parent shouldSelectAsset:asset previousCount:selectionCount];
}
return shouldSelect;
}
- (void)assetSelected:(ELCAsset *)asset
{
if (self.singleSelection) {
for (ELCAsset *elcAsset in self.elcAssets) {
if (asset != elcAsset) {
elcAsset.selected = NO;
}
}
}
if (self.immediateReturn) {
NSArray *singleAssetArray = @[asset];
[(NSObject *)self.parent performSelector:@selector(selectedAssets:) withObject:singleAssetArray afterDelay:0];
}
}
- (BOOL)shouldDeselectAsset:(ELCAsset *)asset
{
if (self.immediateReturn){
return NO;
}
return YES;
}
- (void)assetDeselected:(ELCAsset *)asset
{
if (self.singleSelection) {
for (ELCAsset *elcAsset in self.elcAssets) {
if (asset != elcAsset) {
elcAsset.selected = NO;
}
}
}
if (self.immediateReturn) {
NSArray *singleAssetArray = @[asset.asset];
[(NSObject *)self.parent performSelector:@selector(selectedAssets:) withObject:singleAssetArray afterDelay:0];
}
}
#pragma mark UITableViewDataSource Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.columns <= 0) { //Sometimes called before we know how many columns we have
self.columns = 1;
}
NSInteger numRows = ceil([self.elcAssets count] / (float)self.columns);
return numRows;
}
- (NSArray *)assetsForIndexPath:(NSIndexPath *)path
{
long index = path.row * self.columns;
long length = MIN(self.columns, [self.elcAssets count] - index);
return [self.elcAssets subarrayWithRange:NSMakeRange(index, length)];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ELCAssetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell setAssets:[self assetsForIndexPath:indexPath]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 79;
}
- (int)totalSelectedAssets
{
int count = 0;
for (ELCAsset *asset in self.elcAssets) {
if (asset.selected) {
count++;
}
}
return count;
}
@end