From a32ceeb9c69ca8e63b3112f1c7ce28e9eab6710f Mon Sep 17 00:00:00 2001 From: Artal Druk Date: Fri, 8 Dec 2017 11:30:01 +0200 Subject: [PATCH] Add support for horizontal layout --- .../CKGalleryCollectionViewCell.m | 1 + .../CKGalleryCustomCollectionViewCell.m | 1 + .../CKGalleryViewManager.m | 64 ++++++++++++++++--- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/ios/lib/ReactNativeCameraKit/CKGalleryCollectionViewCell.m b/ios/lib/ReactNativeCameraKit/CKGalleryCollectionViewCell.m index 92c6611..c7122c4 100644 --- a/ios/lib/ReactNativeCameraKit/CKGalleryCollectionViewCell.m +++ b/ios/lib/ReactNativeCameraKit/CKGalleryCollectionViewCell.m @@ -169,6 +169,7 @@ static NSString *remoteDownloadIndicatorType = REMOTE_DOWNLOAD_INDICATOR_TYPE_SP } self.imageView = [[UIImageView alloc] initWithFrame:imageViewFrame]; + self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.imageView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); self.imageView.backgroundColor = [UIColor clearColor]; self.imageView.contentMode = UIViewContentModeScaleAspectFill; diff --git a/ios/lib/ReactNativeCameraKit/CKGalleryCustomCollectionViewCell.m b/ios/lib/ReactNativeCameraKit/CKGalleryCustomCollectionViewCell.m index 7a8cd2f..3c55863 100644 --- a/ios/lib/ReactNativeCameraKit/CKGalleryCustomCollectionViewCell.m +++ b/ios/lib/ReactNativeCameraKit/CKGalleryCustomCollectionViewCell.m @@ -25,6 +25,7 @@ if (imageProp) { UIImage *image = [RCTConvert UIImage:imageProp]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; + imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; imageView.frame = self.bounds; imageView.contentMode = UIViewContentModeCenter; [self addSubview:imageView]; diff --git a/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m b/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m index 6a6893c..f4f4f86 100644 --- a/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m +++ b/ios/lib/ReactNativeCameraKit/CKGalleryViewManager.m @@ -60,6 +60,8 @@ typedef void (^CompletionBlock)(BOOL success); @property (nonatomic, strong) NSDictionary *selection; @property (nonatomic) UIEdgeInsets contentInset; @property (nonatomic) BOOL alwaysBounce; +@property (nonatomic) BOOL isHorizontal; +@property (nonatomic) BOOL cellSizeInvalidated; @property (nonatomic, strong) UIColor *remoteDownloadIndicatorColor; @property (nonatomic, strong) NSString *remoteDownloadIndicatorType; @property (nonatomic, strong) NSNumber *iCloudDownloadSimulateTime; @@ -94,6 +96,9 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; self.imageRequestOptions.synchronous = YES; self.contentInset = UIEdgeInsetsZero; + + self.isHorizontal = NO; + self.cellSizeInvalidated = NO; } return self; @@ -101,8 +106,14 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; -(CGSize)cellSize { - if (CGSizeEqualToSize(_cellSize, CGSizeZero)) { - CGFloat minSize = (MAX(self.bounds.size.width - ((self.columnCount.floatValue-1.0f)*self.minimumInteritemSpacing.floatValue),0))/self.columnCount.floatValue; + if (CGSizeEqualToSize(_cellSize, CGSizeZero) || self.cellSizeInvalidated) { + CGFloat minSize; + CGFloat spacing = (self.columnCount.floatValue - 1.0f) * self.minimumInteritemSpacing.floatValue; + if (self.isHorizontal) { + minSize = MIN(self.bounds.size.width, (self.bounds.size.height - spacing) / self.columnCount.floatValue); + } else { + minSize = (MAX(self.bounds.size.width - spacing, 0)) / self.columnCount.floatValue; + } _cellSize = CGSizeMake(minSize, minSize); } return _cellSize; @@ -133,14 +144,10 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; if (CGRectIsEmpty(frame)) return; if (!self.collectionView) { - UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init]; - flowLayout.itemSize = self.cellSize; - [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; - - self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout]; + self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:[self getCollectionViewFlowLayout:self.isHorizontal]]; self.collectionView.contentInset = self.contentInset; self.collectionView.scrollIndicatorInsets = self.contentInset; - self.collectionView.alwaysBounceVertical = self.alwaysBounce; + [self handleSetAlwaysBounce:self.alwaysBounce isHorizontal:self.isHorizontal]; self.collectionView.delegate = self; self.collectionView.dataSource = self; @@ -157,6 +164,12 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; #pragma mark - Collection view layout things +-(UICollectionViewFlowLayout*)getCollectionViewFlowLayout:(BOOL)isHorizontal { + UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init]; + flowLayout.itemSize = self.cellSize; + [flowLayout setScrollDirection:isHorizontal ? UICollectionViewScrollDirectionHorizontal : UICollectionViewScrollDirectionVertical]; + return flowLayout; +} - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { @@ -225,13 +238,45 @@ static NSString * const CustomCellReuseIdentifier = @"CustomCell"; [CKGalleryCollectionViewCell setRemoteDownloadIndicatorType:remoteDownloadIndicatorType]; } +-(void)handleSetAlwaysBounce:(BOOL)alwaysBounce isHorizontal:(BOOL)isHorizontal { + if (isHorizontal) { + self.collectionView.alwaysBounceHorizontal = alwaysBounce; + self.collectionView.alwaysBounceVertical = NO; + } else { + self.collectionView.alwaysBounceVertical = alwaysBounce; + self.collectionView.alwaysBounceHorizontal = NO; + } +} + -(void)setAlwaysBounce:(BOOL)alwaysBounce { if(self.collectionView) { - self.collectionView.alwaysBounceVertical = alwaysBounce; + [self handleSetAlwaysBounce:alwaysBounce isHorizontal:self.isHorizontal]; } _alwaysBounce = alwaysBounce; } +-(void)setIsHorizontal:(BOOL)isHorizontal { + _isHorizontal = isHorizontal; + if (self.collectionView) { + if ([self.collectionView.collectionViewLayout isKindOfClass:[UICollectionViewFlowLayout class]]) { + UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout; + BOOL needsLayoutSwitch = (isHorizontal && flowLayout.scrollDirection == UICollectionViewScrollDirectionVertical) || + (!isHorizontal && flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal); + if (needsLayoutSwitch) { + self.cellSizeInvalidated = YES; + [self handleSetAlwaysBounce:self.alwaysBounce isHorizontal:isHorizontal]; + [self.collectionView reloadData]; + [self.collectionView performBatchUpdates:^{ + [self.collectionView.collectionViewLayout invalidateLayout]; + [self.collectionView setCollectionViewLayout:[self getCollectionViewFlowLayout:isHorizontal] animated:YES]; + } completion:^(BOOL finished) { + self.cellSizeInvalidated = NO; + }]; + } + } + } +} + -(void)setFileTypeSupport:(NSDictionary *)supported { _fileTypeSupport = supported; @@ -667,6 +712,7 @@ RCT_EXPORT_VIEW_PROPERTY(selection, NSDictionary); RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets); RCT_EXPORT_VIEW_PROPERTY(imageQualityOnTap, NSString); RCT_EXPORT_VIEW_PROPERTY(alwaysBounce, BOOL); +RCT_EXPORT_VIEW_PROPERTY(isHorizontal, BOOL); RCT_EXPORT_VIEW_PROPERTY(remoteDownloadIndicatorColor, UIColor); RCT_EXPORT_VIEW_PROPERTY(remoteDownloadIndicatorType, NSString); RCT_EXPORT_VIEW_PROPERTY(onRemoteDownloadChanged, RCTDirectEventBlock);