From 12b802d1704d162179823122332a513c50b637e8 Mon Sep 17 00:00:00 2001 From: Artal Druk Date: Wed, 8 Mar 2017 16:49:37 +0200 Subject: [PATCH] =?UTF-8?q?when=20selection=20is=20changed,=20allow=20the?= =?UTF-8?q?=20state=20to=20automatically=20sync=20and=20update=20the=20sel?= =?UTF-8?q?ection=20state=20so=20it=20won=E2=80=99t=20be=20necessary=20to?= =?UTF-8?q?=20call=20refreshGalleryView=20to=20sync=20at=20while=20loading?= =?UTF-8?q?=20the=20whole=20collection=20view.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CKGalleryViewManager.m | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m b/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m index 702c3c4..1bab532 100644 --- a/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m +++ b/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m @@ -36,6 +36,7 @@ @property (nonatomic, strong) NSNumber *minimumInteritemSpacing; @property (nonatomic, strong) NSNumber *columnCount; @property (nonatomic, strong) NSNumber *getUrlOnTapImage; +@property (nonatomic, strong) NSNumber *autoSyncSelection; @property (nonatomic, copy) RCTDirectEventBlock onTapImage; @@ -310,8 +311,6 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; __block CKGalleryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellReuseIdentifier forIndexPath:indexPath]; cell.disableSelectionIcons = self.disableSelectionIcons ? self.disableSelectionIcons.boolValue : false; - cell.isSelected = ((NSNumber*)assetDictionary[@"isSelected"]).boolValue; - if (self.supportedFileTypesArray) { cell.isSupported = [self.supportedFileTypesArray containsObject:[MIMETypeString lowercaseString]]; @@ -372,10 +371,47 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; } } +- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath { + if([cell isKindOfClass:[CKGalleryCollectionViewCell class]]) { + CKGalleryCollectionViewCell *ckCell = (CKGalleryCollectionViewCell*)cell; + + NSInteger galleryDataIndex = indexPath.row; + if (self.customButtonStyle) { + galleryDataIndex--; + } + NSDictionary *assetDictionary = (NSDictionary*)self.galleryData.data[galleryDataIndex]; + ((CKGalleryCollectionViewCell*)cell).isSelected = ((NSNumber*)assetDictionary[@"isSelected"]).boolValue; + } +} + +-(BOOL)isSelectionDirty:(NSMutableArray *)newSelectedImages { + if(![self.autoSyncSelection boolValue]) { + return NO; + } + NSArray *mergedArray = [newSelectedImages arrayByAddingObjectsFromArray:self.selectedImages]; + NSArray *arrayWithoutDuplicates = [[NSOrderedSet orderedSetWithArray:mergedArray] array]; + return (arrayWithoutDuplicates.count > 0); +} + -(void)setSelectedImages:(NSMutableArray *)selectedImages { + BOOL selectionDirty = [self isSelectionDirty:selectedImages]; if (selectedImages) { _selectedImages = selectedImages; } + + if(selectionDirty && [self.autoSyncSelection boolValue]) { + //sync visible cells + for (CKGalleryCollectionViewCell *cell in [self.collectionView visibleCells]) { + if([cell respondsToSelector:@selector(representedAssetIdentifier)]) { + cell.isSelected = ([selectedImages indexOfObject:cell.representedAssetIdentifier] != NSNotFound); + } + } + //sync data + for (NSMutableDictionary *dataDic in self.galleryData.data) { + PHAsset *asset = dataDic[@"asset"]; + dataDic[@"isSelected"] = @([selectedImages indexOfObject:asset.localIdentifier] != NSNotFound); + } + } } @@ -459,6 +495,7 @@ RCT_EXPORT_VIEW_PROPERTY(disableSelectionIcons, NSNumber); RCT_EXPORT_VIEW_PROPERTY(customButtonStyle, NSDictionary); RCT_EXPORT_VIEW_PROPERTY(onCustomButtonPress, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(getUrlOnTapImage, NSNumber); +RCT_EXPORT_VIEW_PROPERTY(autoSyncSelection, NSNumber); RCT_EXPORT_VIEW_PROPERTY(selection, NSDictionary);