Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Add auto-deselect
Browse files Browse the repository at this point in the history
  • Loading branch information
nek023 committed Apr 6, 2015
1 parent 6dfea43 commit 631dbaf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion QBImagePicker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.0.2</string>
<string>3.1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
60 changes: 51 additions & 9 deletions QBImagePicker/QBAssetsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ @interface QBAssetsViewController () <PHPhotoLibraryChangeObserver, UICollection

@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;

@property (nonatomic, strong) PHFetchResult *fetchResult;

@property (nonatomic, strong) PHCachingImageManager *imageManager;
@property (nonatomic, assign) CGRect previousPreheatRect;

@property (nonatomic, strong) PHFetchResult *fetchResult;
@property (nonatomic, assign) BOOL disableScrollToBottom;
@property (nonatomic, strong) NSIndexPath *lastSelectedItemIndexPath;

@end

Expand All @@ -73,8 +75,6 @@ - (void)viewDidLoad
{
[super viewDidLoad];

self.imageManager = [PHCachingImageManager new];

[self setUpToolbarItems];
[self resetCachedAssets];

Expand All @@ -100,7 +100,7 @@ - (void)viewWillAppear:(BOOL)animated
[self.navigationItem setRightBarButtonItem:nil animated:NO];
}

[self updateControlState];
[self updateDoneButtonState];
[self updateSelectionInfo];

// Scroll to bottom
Expand Down Expand Up @@ -157,6 +157,21 @@ - (void)setAssetCollection:(PHAssetCollection *)assetCollection
[self.collectionView reloadData];
}

- (PHCachingImageManager *)imageManager
{
if (_imageManager == nil) {
_imageManager = [PHCachingImageManager new];
}

return _imageManager;
}

- (BOOL)isAutoDeselectEnabled
{
return (self.imagePickerController.maximumNumberOfSelection == 1
&& self.imagePickerController.maximumNumberOfSelection >= self.imagePickerController.minimumNumberOfSelection);
}


#pragma mark - Actions

Expand Down Expand Up @@ -230,6 +245,13 @@ - (void)updateFetchRequest
}

self.fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];

if ([self isAutoDeselectEnabled] && self.imagePickerController.selectedAssets.count > 0) {
// Get index of previous selected asset
PHAsset *asset = [self.imagePickerController.selectedAssets firstObject];
NSInteger assetIndex = [self.fetchResult indexOfObject:asset];
self.lastSelectedItemIndexPath = [NSIndexPath indexPathForItem:assetIndex inSection:0];
}
} else {
self.fetchResult = nil;
}
Expand All @@ -254,7 +276,7 @@ - (BOOL)isMaximumSelectionLimitReached
return NO;
}

- (void)updateControlState
- (void)updateDoneButtonState
{
self.doneButton.enabled = [self isMinimumSelectionLimitFulfilled];
}
Expand Down Expand Up @@ -525,20 +547,37 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
return [self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController shouldSelectAsset:asset];
}

if ([self isAutoDeselectEnabled]) {
return YES;
}

return ![self isMaximumSelectionLimitReached];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
QBImagePickerController *imagePickerController = self.imagePickerController;
NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets;

PHAsset *asset = self.fetchResult[indexPath.item];

if (imagePickerController.allowsMultipleSelection) {
if ([self isAutoDeselectEnabled] && selectedAssets.count > 0) {
// Remove previous selected asset from set
[selectedAssets removeObjectAtIndex:0];

// Deselect previous selected asset
if (self.lastSelectedItemIndexPath) {
[collectionView deselectItemAtIndexPath:self.lastSelectedItemIndexPath animated:NO];
}
}

// Add asset to set
NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets;
[selectedAssets addObject:asset];

[self updateControlState];
self.lastSelectedItemIndexPath = indexPath;

[self updateDoneButtonState];

if (imagePickerController.showsNumberOfSelectedAssets) {
[self updateSelectionInfo];
Expand Down Expand Up @@ -568,11 +607,14 @@ - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndex
QBImagePickerController *imagePickerController = self.imagePickerController;
NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets;

// Remove asset from set
PHAsset *asset = self.fetchResult[indexPath.item];

// Remove asset from set
[selectedAssets removeObject:asset];

[self updateControlState];
self.lastSelectedItemIndexPath = nil;

[self updateDoneButtonState];

if (imagePickerController.showsNumberOfSelectedAssets) {
[self updateSelectionInfo];
Expand Down
2 changes: 1 addition & 1 deletion QBImagePickerController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "QBImagePickerController"
s.version = "3.0.2"
s.version = "3.1.0"
s.summary = "A clone of UIImagePickerController with multiple selection support."
s.homepage = "https://github.com/questbeat/QBImagePicker"
s.license = "MIT"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ A clone of UIImagePickerController with multiple selection support.

## Requirements

- iOS 8.0 or later
- Version `>= 3.0.0` : iOS 8 or later (Using PhotoKit)
- Version `< 3.0.0` : iOS 6 or later (Using AssetsLibrary)




Expand Down

0 comments on commit 631dbaf

Please sign in to comment.