sync gallery between js and native

add selected image on cell - not finish
This commit is contained in:
Ran Greenberg 2016-07-04 01:01:29 +03:00
parent 9802580c98
commit bbb960b7ac
6 changed files with 65 additions and 40 deletions

View File

@ -10,6 +10,9 @@
@interface CKGalleryCollectionViewCell : UICollectionViewCell
+(void)base64Image:(NSString*)base64;
@property (nonatomic, strong) UIImage *thumbnailImage;
@property (nonatomic, copy) NSString *representedAssetIdentifier;

View File

@ -7,6 +7,7 @@
//
#import "CKGalleryCollectionViewCell.h"
#import "GalleryData.h"
#define BADGE_SIZE 22
#define BADGE_MARGIN 5
@ -19,8 +20,11 @@ blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0 \
alpha:1.0]
@interface CKGalleryCollectionViewCell ()
static UIImage *unSelectedImage = nil;
@interface CKGalleryCollectionViewCell ()
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIButton *button;
@ -31,6 +35,9 @@ alpha:1.0]
@implementation CKGalleryCollectionViewCell
+(void)unSelectedImage:(UIImage*)image {
unSelectedImage = image;
}
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];

View File

@ -35,10 +35,10 @@
@property (nonatomic) CGSize cellSize;
@property (nonatomic, strong) NSMutableArray *selectedAssets;
@property (nonatomic, strong) NSMutableArray *selectedImagesUrls;
@property (nonatomic, strong) PHImageRequestOptions *imageRequestOptions;
@property (nonatomic, strong) PHFetchOptions *fetchOptions;
@property (nonatomic, strong) NSString *selectedBase64Image;
@end
@ -101,7 +101,8 @@ static NSString * const CellReuseIdentifier = @"Cell";
#pragma mark Collection view layout things
#pragma mark - Collection view layout things
@ -120,19 +121,40 @@ static NSString * const CellReuseIdentifier = @"Cell";
return self.minimumLineSpacing ? self.minimumLineSpacing.floatValue : DEFAULT_MINIMUM_INTERITEM_SPACING;
}
-(void)upadateCollectionView:(PHFetchResult*)fetchResults animated:(BOOL)animated {
self.galleryData = [[GalleryData alloc] initWithFetchResults:fetchResults selectedImagesIds:self.selectedAssets];
if (animated) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^ {
[self.collectionView reloadData];
});
}
}
-(void)setSelectedBase64Image:(NSString *)selectedBase64Image {
[CKGalleryCollectionViewCell base64Image:selectedBase64Image];
}
-(void)setAlbumName:(NSString *)albumName {
if ([albumName caseInsensitiveCompare:@"all photos"] == NSOrderedSame || !albumName || [albumName isEqualToString:@""]) {
PHFetchResult *allPhotosFetchResults = [PHAsset fetchAssetsWithOptions:self.fetchOptions];
self.galleryData = [[GalleryData alloc] initWithFetchResults:allPhotosFetchResults];
[self.collectionView reloadData];
PHFetchResult *allPhotosFetchResults = [PHAsset fetchAssetsWithOptions:self.fetchOptions];
[self upadateCollectionView:allPhotosFetchResults animated:(self.galleryData != nil)];
return;
}
PHFetchResult *collections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
[collections enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL * _Nonnull stop) {
@ -140,12 +162,12 @@ static NSString * const CellReuseIdentifier = @"Cell";
if ([collection.localizedTitle isEqualToString:albumName]) {
PHFetchResult *collectionFetchResults = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
self.galleryData = [[GalleryData alloc] initWithFetchResults:collectionFetchResults];
[self.collectionView reloadData];
[self upadateCollectionView:collectionFetchResults animated:(self.galleryData != nil)];
*stop = YES;
return;
}
}];
[self.collectionView reloadData];
}
@ -167,7 +189,7 @@ static NSString * const CellReuseIdentifier = @"Cell";
cell.representedAssetIdentifier = asset.localIdentifier;
[self.imageManager requestImageForAsset:asset
targetSize:CGSizeMake(self.cellSize.width*0.95, self.cellSize.height*0.95)
targetSize:CGSizeMake(self.cellSize.width, self.cellSize.height)
contentMode:PHImageContentModeDefault
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
@ -217,24 +239,10 @@ static NSString * const CellReuseIdentifier = @"Cell";
}
-(NSMutableArray*)removeAssetInfoFromSelectedAssets:(PHAsset*)asset {
NSMutableArray *newSelectedAssets = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in self.selectedAssets) {
if (dictionary[@"asset"] != asset) {
[newSelectedAssets addObject:dictionary];
}
}
return newSelectedAssets;
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
}
-(void)refreshGalleryView {
-(void)refreshGalleryView:(NSArray*)selectedImages {
self.selectedAssets = selectedImages;
[self setAlbumName:self.albumName];
}
@ -276,6 +284,7 @@ RCT_EXPORT_VIEW_PROPERTY(minimumLineSpacing, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(minimumInteritemSpacing, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(columnCount, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(onSelected, RCTDirectEventBlock);
//RCT_EXPORT_VIEW_PROPERTY(selectedImage, UIImage);
RCT_EXPORT_METHOD(getSelectedImages:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
@ -327,9 +336,12 @@ RCT_EXPORT_METHOD(getSelectedImages:(RCTPromiseResolveBlock)resolve
}
RCT_EXPORT_METHOD(refreshGalleryView:(RCTPromiseResolveBlock)resolve
RCT_EXPORT_METHOD(refreshGalleryView:(NSArray*)selectedImages
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
[self.galleryView refreshGalleryView];
NSLog(@"selectedImages:%@", selectedImages);
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:selectedImages];
[self.galleryView refreshGalleryView:newArray];
if (resolve)
resolve(@YES);

View File

@ -11,8 +11,10 @@
@interface GalleryData : NSObject
-(instancetype)initWithFetchResults:(PHFetchResult*)fetchResults;
-(instancetype)initWithFetchResults:(PHFetchResult*)fetchResults selectedImagesIds:(NSArray*)selectedImagesIds;
@property (nonatomic, strong, readonly) NSArray *data;
@end

View File

@ -18,27 +18,26 @@
@implementation GalleryData
-(instancetype)initWithFetchResults:(PHFetchResult*)fetchResults {
-(instancetype)initWithFetchResults:(PHFetchResult*)fetchResults selectedImagesIds:(NSArray*)selectedImagesIds{
self = [super init];
if (self) {
self.fetchResults = fetchResults;
self.data = [self arrayWithFetchResults:self.fetchResults];
self.data = [self arrayWithFetchResults:self.fetchResults selectedImagesIds:selectedImagesIds];
}
return self;
}
-(NSArray*)arrayWithFetchResults:(PHFetchResult*)fetchResults {
-(NSArray*)arrayWithFetchResults:(PHFetchResult*)fetchResults selectedImagesIds:(NSArray*)selectedImagesIds{
NSMutableArray *array = [[NSMutableArray alloc] init];
for (PHAsset *asset in fetchResults) {
NSMutableDictionary *assetDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
asset, @"asset",
[NSNumber numberWithBool:NO], @"isSelected",
nil];
BOOL isSelected = ([selectedImagesIds containsObject:asset.localIdentifier]) ? YES : NO;
NSMutableDictionary *assetDictionary = [@{@"asset": asset, @"isSelected": @(isSelected)} mutableCopy];
[array addObject:assetDictionary];
}

View File

@ -2,9 +2,11 @@ import React, {Component} from 'react';
import {
requireNativeComponent,
NativeModules,
processColor
processColor,
} from 'react-native';
const GalleryView = requireNativeComponent('CKGalleryView', null);
const GalleryViewManager = NativeModules.CKGalleryViewManager;
const ALL_PHOTOS = 'All Photos';
@ -26,8 +28,8 @@ export default class CameraKitGalleryView extends Component {
}
async refreshGalleryView() {
const isSuccess = await GalleryViewManager.refreshGalleryView();
async refreshGalleryView(selectedImages = []) {
const isSuccess = await GalleryViewManager.refreshGalleryView(selectedImages);
return isSuccess;
}
}