Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correction in tapping control in a cell when the maximum number of selec... #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Classes/ELCImagePicker/ELCAlbumPickerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@property (nonatomic, weak) id<ELCAssetSelectionDelegate> parent;
@property (nonatomic, strong) NSMutableArray *assetGroups;
@property (nonatomic, strong) NSArray *mediaTypes;
@property (strong, nonatomic) ELCAssetTablePicker *picker;

// optional, can be used to filter the assets displayed
@property (nonatomic, weak) id<ELCAssetPickerFilterDelegate> assetPickerFilterDelegate;
Expand Down
243 changes: 190 additions & 53 deletions Classes/ELCImagePicker/ELCAlbumPickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@interface ELCAlbumPickerController ()

@property (nonatomic, strong) ALAssetsLibrary *library;
@property (nonatomic, strong) NSString *loadedGroupID;
@property (nonatomic, strong) NSMutableArray *tempAssetGroups;

@end

Expand All @@ -37,68 +39,193 @@ - (void)viewDidLoad

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
self.library = assetLibrary;

// Load Albums into assetGroups
dispatch_async(dispatch_get_main_queue(), ^
{
@autoreleasepool {

// Group enumerator Block
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil) {
return;
}

// added fix for camera albums order
NSString *sGroupPropertyName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];

if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
[self.assetGroups insertObject:group atIndex:0];
}
else {
[self.assetGroups addObject:group];
}

// Reload albums
[self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
};

// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil];
[alert show];

NSLog(@"A problem occured %@", [error description]);
};

// Enumerate Albums
[self.library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];

}
@autoreleasepool {

// Group enumerator Block
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil) {
[self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
return;
}

// added fix for camera albums order
NSString *sGroupPropertyName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];

if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
[self.assetGroups insertObject:group atIndex:0];
}
else {
[self.assetGroups addObject:group];
}
};

// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil];
[alert show];

NSLog(@"A problem occurred %@", [error description]);
};

// Enumerate Albums
[self.library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];

}
});

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkAlbums) name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView) name:ALAssetsLibraryChangedNotification object:nil];
self.loadedGroupID = @"";
[self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
- (void)didReceiveMemoryWarning {

[[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)dealloc {

[[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)reloadTableView
- (void)checkAlbums
{
[self.tableView reloadData];
[self.navigationItem setTitle:NSLocalizedString(@"Select an Album", nil)];
if (!self.tempAssetGroups) {
self.tempAssetGroups = [[NSMutableArray alloc] initWithCapacity:0];
}
[self.tempAssetGroups removeAllObjects];

// Load Albums into assetGroups
dispatch_async(dispatch_get_main_queue(), ^
{
@autoreleasepool {

// Group enumerator Block
void (^assetGroupEnumeratorCheck)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil) { // End of the enumeration

NSMutableArray *arrayTemp = [[NSMutableArray alloc] initWithCapacity:1];

// Check if an album has been added
for (ALAssetsGroup *group1 in self.tempAssetGroups) {
BOOL albumExists = NO;
NSString *groupID1 = (NSString *)[group1 valueForProperty:ALAssetsGroupPropertyPersistentID];

for (ALAssetsGroup *group2 in self.assetGroups) {
NSString *groupID2 = (NSString *)[group2 valueForProperty:ALAssetsGroupPropertyPersistentID];

if ([groupID1 isEqualToString:groupID2]) {
albumExists = YES;
break;
}
}
if (!albumExists) {
[arrayTemp addObject: group1];
}
}
//Add the new albums
if (arrayTemp.count > 0) {
[self.assetGroups addObjectsFromArray:arrayTemp];
}
[arrayTemp removeAllObjects];

// Check if an album has been deleted
for (ALAssetsGroup *group1 in self.assetGroups) {
BOOL albumExists = NO;
NSString *groupID1 = (NSString *)[group1 valueForProperty:ALAssetsGroupPropertyPersistentID];

for (ALAssetsGroup *group2 in self.tempAssetGroups) {
NSString *groupID2 = (NSString *)[group2 valueForProperty:ALAssetsGroupPropertyPersistentID];

if ([groupID1 isEqualToString:groupID2]) {
albumExists = YES;
break;
}
}
if (!albumExists) {
[arrayTemp addObject:group1];
}
}

//Remove the deleted albums
if (arrayTemp.count > 0) {
[self.assetGroups removeObjectsInArray:arrayTemp];
}

// If an album is loaded, check if it has been deleted to get back
if (self.loadedGroupID.length > 0) {
BOOL albumExists = NO;
for (ALAssetsGroup *group in self.tempAssetGroups) {
NSString *groupID = (NSString *)[group valueForProperty:ALAssetsGroupPropertyPersistentID];
if ([groupID isEqualToString:self.loadedGroupID]) {
albumExists = YES;
break;
}
}

// If the loaded album has been deleted pop the picker viewcontroller
if (!albumExists) {
[self.picker returnBack];
}
}else {
[self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
}

return;
}

// added fix for camera albums order
NSString *sGroupPropertyName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];

if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
[self.tempAssetGroups insertObject:group atIndex:0];
}
else {
[self.tempAssetGroups addObject:group];
}
};

// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil];
[alert show];

NSLog(@"A problem occured %@", [error description]);
};

if (!self.library) {
self.library = [[ALAssetsLibrary alloc] init];
}

// Enumerate Albums
[self.library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumeratorCheck
failureBlock:assetGroupEnumberatorFailure];

}
});
}

- (void)reloadTableView {

[self.tableView reloadData];
[self.navigationItem setTitle:NSLocalizedString(@"Select an Album", nil)];
}

- (BOOL)shouldSelectAsset:(ELCAsset *)asset previousCount:(NSUInteger)previousCount
Expand All @@ -111,6 +238,15 @@ - (BOOL)shouldDeselectAsset:(ELCAsset *)asset previousCount:(NSUInteger)previous
return [self.parent shouldDeselectAsset:asset previousCount:previousCount];
}

- (BOOL)isSelectableIndexNumber:(NSUInteger)indexNumber {

if ([self.parent respondsToSelector:@selector(isSelectableIndexNumber:)]) {
return [self.parent isSelectableIndexNumber:indexNumber];
}else {
return NO;
}
}

- (void)selectedAssets:(NSArray*)assets
{
[_parent selectedAssets:assets];
Expand Down Expand Up @@ -176,15 +312,16 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ELCAssetTablePicker *picker = [[ELCAssetTablePicker alloc] initWithNibName: nil bundle: nil];
picker.parent = self;

picker.assetGroup = [self.assetGroups objectAtIndex:indexPath.row];
[picker.assetGroup setAssetsFilter:[self assetFilter]];

picker.assetPickerFilterDelegate = self.assetPickerFilterDelegate;
self.picker = [[ELCAssetTablePicker alloc] initWithNibName: nil bundle: nil];
self.picker.parent = self;
self.picker.assetGroup = [self.assetGroups objectAtIndex:indexPath.row];
[self.picker.assetGroup setAssetsFilter:[self assetFilter]];
self.picker.assetPickerFilterDelegate = self.assetPickerFilterDelegate;

[self.navigationController pushViewController:picker animated:YES];
// Store the persistentID of the album to load
self.loadedGroupID = (NSString *)[self.picker.assetGroup valueForProperty:ALAssetsGroupPropertyPersistentID];

[self.navigationController pushViewController:self.picker animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down
2 changes: 2 additions & 0 deletions Classes/ELCImagePicker/ELCAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@optional
- (void)assetSelected:(ELCAsset *)asset;
- (BOOL)shouldSelectAsset:(ELCAsset *)asset;
- (BOOL)isSelectable;
- (void)assetDeselected:(ELCAsset *)asset;
- (BOOL)shouldDeselectAsset:(ELCAsset *)asset;
@end
Expand All @@ -25,6 +26,7 @@
@property (nonatomic, strong) ALAsset *asset;
@property (nonatomic, weak) id<ELCAssetDelegate> parent;
@property (nonatomic, assign) BOOL selected;
@property (nonatomic, readonly, getter = isSelectable) BOOL selectable;
@property (nonatomic,assign) int index;

- (id)initWithAsset:(ALAsset *)asset;
Expand Down
9 changes: 9 additions & 0 deletions Classes/ELCImagePicker/ELCAsset.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ - (void)setSelected:(BOOL)selected
}
}

- (BOOL)isSelectable {

if ([_parent respondsToSelector:@selector(isSelectable)]) {
return [_parent isSelectable];
}else {
return NO;
}
}

- (NSComparisonResult)compareWithIndex:(ELCAsset *)_ass
{
if (self.index > _ass.index) {
Expand Down
23 changes: 16 additions & 7 deletions Classes/ELCImagePicker/ELCAssetCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,30 @@ - (void)cellTapped:(UITapGestureRecognizer *)tapRecognizer

CGRect frame = CGRectMake(startX, 2, 75, 75);

for (int i = 0; i < [_rowAssets count]; ++i) {
for (int i = 0; i < [_rowAssets count]; ++i) {
if (CGRectContainsPoint(frame, point)) {
ELCAsset *asset = [_rowAssets objectAtIndex:i];
asset.selected = !asset.selected;
ELCOverlayImageView *overlayView = [_overlayViewArray objectAtIndex:i];
overlayView.hidden = !asset.selected;
if (asset.selected) {

// If the asset is not selected check if it could be selected
if (!asset.selected && [asset isSelectable]) {
asset.selected = !asset.selected;
overlayView.hidden = !asset.selected;

asset.index = [[ELCConsole mainConsole] numOfSelectedElements];
[overlayView setIndex:asset.index+1];
[[ELCConsole mainConsole] addIndex:asset.index];
}
else
{
}else if(asset.selected) { // If the asset is going to be unselected
asset.selected = !asset.selected;
overlayView.hidden = !asset.selected;

asset.index = 0;
[overlayView setIndex:asset.index];
int lastElement = [[ELCConsole mainConsole] numOfSelectedElements] - 1;
[[ELCConsole mainConsole] removeIndex:lastElement];
}else if (!asset.selected && ![asset isSelectable]) {
// If the asset isn't selected and is not selectable, try to select to show a message to the user
asset.selected = !asset.selected;
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions Classes/ELCImagePicker/ELCAssetSelectionDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
- (void)selectedAssets:(NSArray *)assets;
- (BOOL)shouldSelectAsset:(ELCAsset *)asset previousCount:(NSUInteger)previousCount;
- (BOOL)shouldDeselectAsset:(ELCAsset *)asset previousCount:(NSUInteger)previousCount;
- (BOOL)isSelectableIndexNumber:(NSUInteger)indexNumber;

@end
2 changes: 1 addition & 1 deletion Classes/ELCImagePicker/ELCAssetTablePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
// optional, can be used to filter the assets displayed
@property(nonatomic, weak) id<ELCAssetPickerFilterDelegate> assetPickerFilterDelegate;

- (void)returnBack;
- (int)totalSelectedAssets;
- (void)preparePhotos;

- (void)doneAction:(id)sender;

Expand Down
Loading