Media Picker: Allow multi selection of pictures #301

Multi selection in the media album is done.
This commit is contained in:
manuroe
2016-08-03 14:59:40 +02:00
parent 50cb95c537
commit 0f989049bd
4 changed files with 88 additions and 4 deletions
@@ -33,6 +33,11 @@
The current list of assets retrieved from collection.
*/
PHFetchResult *assets;
/**
The currently selected media. Nil when the multiselection is not active.
*/
NSMutableArray <PHAsset*> *selectedAssets;
}
@end
@@ -81,6 +86,8 @@
self.assetsCollection = _assetsCollection;
}];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"media_picker_select", @"Vector", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSelect:)];
}
- (void)dealloc
@@ -219,7 +226,10 @@
cell.bottomLeftIcon.image = [UIImage imageNamed:@"video_icon"];
cell.bottomLeftIcon.hidden = (asset.mediaType == PHAssetMediaTypeImage);
cell.bottomRightIcon.image = [UIImage imageNamed:@"selection_tick"];
cell.bottomRightIcon.hidden = !selectedAssets || (NSNotFound == [selectedAssets indexOfObject:asset]);
// Disable user interaction in mxkImageView, in order to let collection handle user selection
cell.mxkImageView.userInteractionEnabled = NO;
}
@@ -233,7 +243,32 @@
{
if (indexPath.item < assets.count && self.delegate)
{
[self.delegate mediaAlbumContentViewController:self didSelectAsset:assets[indexPath.item]];
PHAsset *asset = assets[indexPath.item];
// Are we in multiselection mode ?
if (!selectedAssets)
{
// NO
[self.delegate mediaAlbumContentViewController:self didSelectAsset:asset];
}
else
{
// YES. Toggle the selection of the cell
MXKMediaCollectionViewCell *cell = (MXKMediaCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (NSNotFound == [selectedAssets indexOfObject:asset])
{
cell.bottomRightIcon.hidden = NO;
[selectedAssets addObject:asset];
}
else
{
cell.bottomRightIcon.hidden = YES;
[selectedAssets removeObject:asset];
}
self.navigationItem.rightBarButtonItem.enabled = (0 < selectedAssets.count);
}
}
}
@@ -261,4 +296,38 @@
return CGSizeZero;
}
#pragma mark - Actions
- (void)onSelect:(id)sender
{
selectedAssets = [NSMutableArray array];
// Update the nav buttons
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSBundle mxk_localizedStringForKey:@"send"] style:UIBarButtonItemStylePlain target:self action:@selector(onSelectionSend:)];
self.navigationItem.rightBarButtonItem.enabled = NO;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onSelectionCancel:)];
}
- (void)onSelectionSend:(id)sender
{
[self.delegate mediaAlbumContentViewController:self didSelectAssets:selectedAssets];
}
- (void)onSelectionCancel:(id)sender
{
selectedAssets = nil;
// Update the nav buttons
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"media_picker_select", @"Vector", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSelect:)];
self.navigationItem.rightBarButtonItem.enabled = YES;
self.navigationItem.leftBarButtonItem = nil;
// Do not use [UICollectionView reloadData] because it creates flickering
// Unselecting manually the cells is more efficient
for (MXKMediaCollectionViewCell *cell in self.assetsCollectionView.visibleCells)
{
cell.bottomRightIcon.hidden = YES;
}
}
@end