Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions Pod/Classes/WPMediaPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ @interface WPMediaPickerViewController ()

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *internalSelectedAssets;
@property (nonatomic, strong) id<WPMediaAsset> capturedAsset;
@property (nonatomic, strong) WPMediaCapturePreviewCollectionView *captureCell;
@property (nonatomic, strong) UIRefreshControl *refreshControl;
@property (nonatomic, strong) NSObject *changesObserver;
Expand All @@ -44,6 +45,7 @@ - (instancetype)initWithOptions:(WPMediaPickerOptions *)options {
if (self) {
_layout = layout;
_internalSelectedAssets = [[NSMutableArray alloc] init];
_capturedAsset = nil;
_options = [options copy];
_refreshGroupFirstTime = YES;
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressOnAsset:)];
Expand Down Expand Up @@ -300,30 +302,27 @@ - (void)refreshDataAnimated:(BOOL)animated
__typeof__(self) strongSelf = weakSelf;
BOOL refreshGroupFirstTime = strongSelf.refreshGroupFirstTime;
strongSelf.refreshGroupFirstTime = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.collectionView.allowsSelection = YES;
strongSelf.collectionView.allowsMultipleSelection = strongSelf.options.allowMultipleSelection;
strongSelf.collectionView.scrollEnabled = YES;
[strongSelf refreshSelection];
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.collectionView.allowsSelection = YES;
strongSelf.collectionView.allowsMultipleSelection = strongSelf.options.allowMultipleSelection;
strongSelf.collectionView.scrollEnabled = YES;
[strongSelf.collectionView reloadData];
[strongSelf.collectionView reloadData];

if (animated) {
if (animated) {
[strongSelf.refreshControl endRefreshing];
} else {
[UIView performWithoutAnimation:^{
[strongSelf.refreshControl endRefreshing];
} else {
[UIView performWithoutAnimation:^{
[strongSelf.refreshControl endRefreshing];
}];
}
}];
}

// Scroll to the correct position
if (refreshGroupFirstTime){
[strongSelf scrollToStart:NO];
}
// Scroll to the correct position
if (refreshGroupFirstTime){
[strongSelf scrollToStart:NO];
}

[strongSelf informDelegateDidEndLoadingData];
});

[strongSelf informDelegateDidEndLoadingData];
});
} failure:^(NSError *error) {
__typeof__(self) strongSelf = weakSelf;
Expand Down Expand Up @@ -402,6 +401,18 @@ - (void)refreshSelection
[stillExistingSeletedAssets addObject:asset];
}
}
if (self.capturedAsset != nil) {
NSString *assetIdentifier = [self.capturedAsset identifier];
if ([self.dataSource mediaWithIdentifier:assetIdentifier]) {
[stillExistingSeletedAssets addObject:self.capturedAsset];
}
NSInteger positionToUpdate = self.options.showMostRecentFirst ? 0 : self.dataSource.numberOfAssets-1;
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:positionToUpdate inSection:0]
animated:NO
scrollPosition:UICollectionViewScrollPositionNone];
self.capturedAsset = nil;
}

self.internalSelectedAssets = stillExistingSeletedAssets;
if ([self.mediaPickerDelegate respondsToSelector:@selector(mediaPickerController:selectionChanged:)]) {
[self.mediaPickerDelegate mediaPickerController:self selectionChanged:[self.internalSelectedAssets copy]];
Expand Down Expand Up @@ -730,12 +741,12 @@ - (void)addMedia:(id<WPMediaAsset>)asset animated:(BOOL)animated
BOOL willBeSelected = YES;
if ([self.mediaPickerDelegate respondsToSelector:@selector(mediaPickerController:shouldSelectAsset:)]) {
if ([self.mediaPickerDelegate mediaPickerController:self shouldSelectAsset:asset]) {
[self.internalSelectedAssets addObject:asset];
self.capturedAsset = asset;
} else {
willBeSelected = NO;
}
} else {
[self.internalSelectedAssets addObject:asset];
self.capturedAsset = asset;
}

if (!willBeSelected) {
Expand All @@ -749,10 +760,6 @@ - (void)addMedia:(id<WPMediaAsset>)asset animated:(BOOL)animated
[self.mediaPickerDelegate mediaPickerController:self didFinishPickingAssets:[self.internalSelectedAssets copy]];
}
}
NSInteger positionToUpdate = self.options.showMostRecentFirst ? 0 : self.dataSource.numberOfAssets-1;
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:positionToUpdate inSection:0]
animated:YES
scrollPosition:UICollectionViewScrollPositionNone];
}

#pragma mark - UIImagePickerControllerDelegate
Expand Down