Permalink
Browse files

Disable selecting videos when photos are selected and the other way r…

…ound.
  • Loading branch information...
1 parent 0186056 commit 4cda870a8721e85bf460e7dbafe7d44042a48433 @brutella committed Jan 3, 2013
@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
+#import "ELCAssetTablePicker.h"
@interface ELCAlbumPickerController : UITableViewController {
View
@@ -20,6 +20,7 @@
@property (nonatomic, assign) id parent;
-(id)initWithAsset:(ALAsset*)_asset;
--(BOOL)selected;
+-(BOOL) selected;
+- (void)setDisabled:(BOOL)disabledFlag;
@end
View
@@ -8,6 +8,12 @@
#import "ELCAsset.h"
#import "ELCAssetTablePicker.h"
+@interface ELCAsset ()
+
+@property (nonatomic, strong) UIView *disabledView;
+
+@end
+
@implementation ELCAsset
@synthesize asset;
@@ -82,17 +88,16 @@ -(id)initWithAsset:(ALAsset*)_asset {
}
-(void)toggleSelection {
+ if([(ELCAssetTablePicker*)self.parent canSelectAsset:self.asset])
+ {
+ overlayView.hidden = !overlayView.hidden;
+ if (!overlayView.hidden) {
+ [self.parent didSelectAsset:self.asset];
+ } else {
+ [self.parent didDeselectAsset:self.asset];
+ }
- overlayView.hidden = !overlayView.hidden;
-
-// if([(ELCAssetTablePicker*)self.parent totalSelectedAssets] >= 10) {
-//
-// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Maximum Reached" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
-// [alert show];
-// [alert release];
-//
-// [(ELCAssetTablePicker*)self.parent doneAction:nil];
-// }
+ }
}
-(BOOL)selected {
@@ -105,13 +110,35 @@ -(void)setSelected:(BOOL)_selected {
[overlayView setHidden:!_selected];
}
-- (void)dealloc
-{
+#define kDisabledTag 1
+
+- (void)setDisabled:(BOOL)disabledFlag
+{
+ [[self viewWithTag:kDisabledTag] removeFromSuperview];
+ if (disabledFlag) {
+ _disabledView = [[UIView alloc] initWithFrame:CGRectZero];
+ _disabledView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.7f];
+ _disabledView.tag = kDisabledTag;
+ [self addSubview:_disabledView];
+ }
+}
+
+- (void)layoutSubviews
+{
+ [super layoutSubviews];
+
+ _disabledView.frame = self.bounds;
+}
+
+- (void)dealloc
+{
self.asset = nil;
[overlayView release];
[super dealloc];
}
+#pragma mark - Private
+
- (CGSize)cameraImageSize
{
// width 38, height 20 <= retina
@@ -27,7 +27,9 @@
-(int)totalSelectedAssets;
-(void)preparePhotos;
-
-(void)doneAction:(id)sender;
+- (BOOL)canSelectAsset:(ALAsset *)asset;
+- (void)didDeselectAsset:(ALAsset *)asset;
+- (void)didSelectAsset:(ALAsset *)asset;
@end
@@ -10,6 +10,11 @@
#import "ELCAsset.h"
#import "ELCAlbumPickerController.h"
+@interface ELCAssetTablePicker ()
+
+@property (nonatomic, strong) NSMutableSet *selectedAssets;
+
+@end
@implementation ELCAssetTablePicker
@@ -18,7 +23,7 @@ @implementation ELCAssetTablePicker
@synthesize assetGroup, elcAssets;
-(void)viewDidLoad {
-
+ _selectedAssets = [[NSMutableSet alloc] init];
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setAllowsSelection:NO];
@@ -135,15 +140,22 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+ NSArray *assets = [self assetsForIndexPath:indexPath];
if (cell == nil)
{
- cell = [[[ELCAssetCell alloc] initWithAssets:[self assetsForIndexPath:indexPath] reuseIdentifier:CellIdentifier] autorelease];
+ cell = [[[ELCAssetCell alloc] initWithAssets:assets reuseIdentifier:CellIdentifier] autorelease];
}
else
- {
- [cell setAssets:[self assetsForIndexPath:indexPath]];
+ {
+ [cell setAssets:assets];
}
+ // Show to user that assets of different type than the selected ones
+ // can't be selected.
+ for(ELCAsset *asset in assets) {
+ [asset setDisabled:![self canSelectAsset:asset.asset]];
+ }
+
return cell;
}
@@ -167,6 +179,34 @@ - (int)totalSelectedAssets {
return count;
}
+- (BOOL)canSelectAsset:(ALAsset *)asset
+{
+ ALAsset *lastSelectedAsset = [_selectedAssets anyObject];
+ if (lastSelectedAsset && [lastSelectedAsset valueForProperty:ALAssetPropertyType] != [asset valueForProperty:ALAssetPropertyType]) {
+ return NO;
+ }
+
+ return YES;
+}
+
+- (void)didDeselectAsset:(ALAsset *)asset
+{
+ [_selectedAssets removeObject:asset];
+
+ if ([_selectedAssets count] == 0) {
+ [self.tableView reloadData];
+ }
+}
+
+- (void)didSelectAsset:(ALAsset *)asset
+{
+ BOOL firstSelection = [_selectedAssets count] == 0;
+ [_selectedAssets addObject:asset];
+ if (firstSelection) {
+ [self.tableView reloadData];
+ }
+}
+
- (void)dealloc
{
[elcAssets release];
@@ -33,7 +33,8 @@ -(void)selectedAssets:(NSArray*)_assets {
[workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]] forKey:@"UIImagePickerControllerOriginalImage"];
[workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
- [returnArray addObject:workingDictionary];
+ [returnArray addObject:asset];
+// [returnArray addObject:workingDictionary];
[workingDictionary release];
}

0 comments on commit 4cda870

Please sign in to comment.