Skip to content

Commit

Permalink
Merge branch 'release/0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaoj committed May 20, 2014
2 parents f83ccbe + d8e207f commit b53220a
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 78 deletions.
19 changes: 10 additions & 9 deletions BSImagePicker/Category/UIViewController+MultipleImagePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,9 @@
#import <UIKit/UIKit.h>

@class ALAsset;
typedef NS_ENUM(NSInteger, BSImageReset) {
BSImageResetCancel,
BSImageResetAlbum,
BSImageResetDone
};

typedef void (^BSImageToggleBlock)(ALAsset *asset, BOOL select);
typedef void (^BSImageResetBlock)(NSArray *assets, BSImageReset reset);
typedef void (^BSImageGroupBlock)(NSArray *assets);

@class BSImagePickerController;
@interface UIViewController (MultipleImagePicker)
Expand All @@ -42,9 +37,15 @@ typedef void (^BSImageResetBlock)(NSArray *assets, BSImageReset reset);
* @param flag Present with animation or not
* @param completion Presention completion handler or nil
* @param toggle Image toggle handler block (select/deselect) or nil
* @param reset Image reset handler block (cancel/album change/done) or nil
* @discussion Blocks will allways be called on the main thread
* @param cancelBlock Block that gets called when user cancels, contains assets that was selected when user canceled. Can be nil
* @param finishBlock Block that gets called when user finishes, contains asset that was selected when user finished. Can be nil
* @discussion Blocks will allways be called on the main thread
*/
- (void)presentImagePickerController:(BSImagePickerController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion toggle:(BSImageToggleBlock)toggle reset:(BSImageResetBlock)reset;
- (void)presentImagePickerController:(BSImagePickerController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion
toggle:(BSImageToggleBlock)toggleBlock
cancel:(BSImageGroupBlock)cancelBlock
finish:(BSImageGroupBlock)finishBlock;

@end
12 changes: 9 additions & 3 deletions BSImagePicker/Category/UIViewController+MultipleImagePicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@

@implementation UIViewController (MultipleImagePicker)

- (void)presentImagePickerController:(BSImagePickerController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion toggle:(BSImageToggleBlock)toggle reset:(BSImageResetBlock)reset
- (void)presentImagePickerController:(BSImagePickerController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion
toggle:(BSImageToggleBlock)toggleBlock
cancel:(BSImageGroupBlock)cancelBlock
finish:(BSImageGroupBlock)finishBlock
{
if([viewControllerToPresent isKindOfClass:[BSImagePickerController class]]) {
[viewControllerToPresent setToggleBlock:toggle];
[viewControllerToPresent setResetBlock:reset];
[viewControllerToPresent setToggleBlock:toggleBlock];
[viewControllerToPresent setCancelBlock:cancelBlock];
[viewControllerToPresent setFinishBlock:finishBlock];
}

[self presentViewController:viewControllerToPresent animated:flag completion:completion];
Expand Down
18 changes: 5 additions & 13 deletions BSImagePicker/Controller/BSAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,11 @@ - (void)doBSImagePicker:(id)sender
NSLog(@"Image deselected");
}
}
reset:^(NSArray *assets, BSImageReset reset) {
switch (reset) {
case BSImageResetCancel:
NSLog(@"Image picker canceled");
break;
case BSImageResetAlbum:
NSLog(@"Image picker changed album");
break;
case BSImageResetDone:
NSLog(@"Image picker done");
break;
}
}];
cancel:^(NSArray *assets) {
NSLog(@"User canceled...!");
} finish:^(NSArray *assets) {
NSLog(@"User finished :)!");
}];
}

-(UIStatusBarStyle)preferredStatusBarStyle
Expand Down
9 changes: 7 additions & 2 deletions BSImagePicker/Controller/BSImagePickerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
@property (nonatomic, copy) BSImageToggleBlock toggleBlock;

/**
* Block that gets called on cancel/done
* Block that gets called on cancel
*/
@property (nonatomic, copy) BSImageResetBlock resetBlock;
@property (nonatomic, copy) BSImageGroupBlock cancelBlock;

/**
* Block that gets called on finish
*/
@property (nonatomic, copy) BSImageGroupBlock finishBlock;

/**
* Set to YES to disable preview on long press
Expand Down
2 changes: 2 additions & 0 deletions BSImagePicker/Controller/BSImagePickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//Default to shitloads of images
_maximumNumberOfImages = NSUIntegerMax;

[self.view setBackgroundColor:[UIColor whiteColor]];

[self pushViewController:self.imagePicker animated:NO];
}
return self;
Expand Down
1 change: 1 addition & 0 deletions BSImagePicker/Controller/BSImagePreviewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@

@property (nonatomic, assign) NSUInteger currentAssetIndex;
@property (nonatomic, weak) ALAssetsGroup *photos;
@property (nonatomic, weak) NSMutableArray *selectedPhotos;

@end
91 changes: 88 additions & 3 deletions BSImagePicker/Controller/BSImagePreviewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@

#import "BSImagePreviewController.h"
#import "BSPreviewCell.h"
#import "BSImagePickerController.h"
#import <AssetsLibrary/AssetsLibrary.h>

static NSString *kPreviewCellIdentifier = @"PreviewCellIdentifier";

@interface BSImagePreviewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIBarButtonItem *deselectButton;
@property (nonatomic, strong) UIBarButtonItem *selectButton;
@property (nonatomic, strong, readonly) BSImagePickerController *navigationController;

- (void)registerCollectionViewCellIdentifiers;
- (void)toggleSelectionButtonPressed:(UIBarButtonItem *)sender;
- (void)setupRightButton;

@end

Expand All @@ -40,7 +46,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self setTitle:NSLocalizedString(@"Preview", @"Preview navigation title")];
[self setAutomaticallyAdjustsScrollViewInsets:NO];

[self.view addSubview:self.collectionView];
Expand All @@ -52,14 +57,16 @@ - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[self.collectionView reloadData];

//Scroll to the correct image
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentAssetIndex inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:NO];

[self setupRightButton];
}



#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
Expand Down Expand Up @@ -102,6 +109,15 @@ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UIColl
return UIEdgeInsetsZero;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self setCurrentAssetIndex:scrollView.contentOffset.x / scrollView.frame.size.width];

[self setupRightButton];
}

#pragma mark - Lazy load

- (UICollectionView *)collectionView
Expand All @@ -128,11 +144,80 @@ - (UICollectionView *)collectionView
return _collectionView;
}

- (UIBarButtonItem *)deselectButton
{
if(!_deselectButton) {
_deselectButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Deselect", @"Deselect button title")
style:UIBarButtonItemStylePlain
target:self
action:@selector(toggleSelectionButtonPressed:)];
}

return _deselectButton;
}

- (UIBarButtonItem *)selectButton
{
if(!_selectButton) {
_selectButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Select", @"Select button title")
style:UIBarButtonItemStylePlain
target:self
action:@selector(toggleSelectionButtonPressed:)];
}

return _selectButton;
}

#pragma mark - Private

- (void)registerCollectionViewCellIdentifiers
{
[self.collectionView registerClass:[BSPreviewCell class] forCellWithReuseIdentifier:kPreviewCellIdentifier];
}

- (void)toggleSelectionButtonPressed:(UIBarButtonItem *)sender
{
[self.photos enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:self.currentAssetIndex]
options:0
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
if([sender isEqual:self.selectButton]) {
[self.selectedPhotos addObject:result];

if(self.navigationController.toggleBlock) {
self.navigationController.toggleBlock(result, YES);
}
} else {
[self.selectedPhotos removeObject:result];

if(self.navigationController.toggleBlock) {
self.navigationController.toggleBlock(result, NO);
}
}

[self setupRightButton];
}
}];
}

- (void)setupRightButton
{
[self.photos enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:self.currentAssetIndex]
options:0
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
if([self.selectedPhotos containsObject:result]) {
[self.navigationItem setRightBarButtonItem:self.deselectButton];
} else {
[self.navigationItem setRightBarButtonItem:self.selectButton];
}
}
}];
}

- (BSImagePickerController *)navigationController
{
return (BSImagePickerController *)[super navigationController];
}

@end
Loading

0 comments on commit b53220a

Please sign in to comment.