Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
23 changes: 22 additions & 1 deletion Pod/Classes/WPMediaPickerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@
*/
- (nullable UIView *)emptyViewForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;

/**
* Asks the delegate for an empty view to show when there are no assets
* to be displayed. If no empty view is required, you have to implement this
* method and return `nil`.
*
* @param picker The controller object managing the assets picker interface.
* @return An empty view controller to display or `nil` to not display any.
*
* If this method is not implemented, a default ViewController with a default
* UILabel will be displayed.
*/
- (nullable UIViewController *)emptyViewControllerForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;

@end


Expand Down Expand Up @@ -234,7 +247,8 @@
@property (nonatomic, strong, readonly, nullable) UISearchBar *searchBar;

/**
The default empty view. When `emptyViewForMediaPickerController:` is not implemented, use this property to style the mensaje.
The default empty view. When `emptyViewForMediaPickerController:` is not implemented,
use this property to style the message.
*/
@property (nonatomic, strong, readonly, nonnull) UILabel *defaultEmptyView;

Expand Down Expand Up @@ -293,6 +307,13 @@
*/
- (nonnull UIViewController *)defaultPreviewViewControllerForAsset:(nonnull id<WPMediaAsset>)asset;

/**
Return a View Controller to present `defaultEmptyView`.

@return a view controller to present the empty view.
*/
- (UIViewController *)defaultEmptyViewController;

/**
Calculates the appropriate cell height/width given the desired number of cells per line, desired space
between cells, and total width of the frame containing the cells.
Expand Down
128 changes: 114 additions & 14 deletions Pod/Classes/WPMediaPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ @interface WPMediaPickerViewController ()

@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, strong) UILabel *defaultEmptyView;
@property (nonatomic, strong) UIViewController *emptyViewController;
@property (nonatomic, strong) UIViewController *defaultEmptyViewController;


@property (nonatomic, strong) WPActionBar *accessoryActionBar;
@property (nonatomic, strong) UIButton *selectedActionButton;
Expand Down Expand Up @@ -553,6 +556,8 @@ - (void)showCapture {
return;
}

#pragma mark - Empty View support

- (UIView *)emptyView
{
if (_emptyView) {
Expand All @@ -570,8 +575,12 @@ - (UIView *)emptyView

- (void)addEmptyViewToView
{
if (self.emptyView.superview == nil) {
[self.collectionView addSubview:_emptyView];
if ([self usingEmptyViewController]) {
[self addEmptyViewControllerToView];
} else {
if (self.emptyView.superview == nil) {
[self.collectionView addSubview:_emptyView];
}
}
}

Expand All @@ -586,6 +595,61 @@ - (UILabel *)defaultEmptyView
return _defaultEmptyView;
}

#pragma mark - Empty View Controller support

- (void)addEmptyViewControllerToView
{
if (self.emptyViewController.view.superview == nil) {
[self.collectionView addSubview:self.emptyViewController.view];
_emptyViewController.view.frame = self.collectionView.frame;
[self addChildViewController:_emptyViewController];
[_emptyViewController didMoveToParentViewController:self];
[self centerEmptyView];
}
}

- (void)removeEmptyViewControllerFromView
{
[_emptyViewController willMoveToParentViewController:nil];
[_emptyViewController.view removeFromSuperview];
[_emptyViewController removeFromParentViewController];
}

- (UIViewController *)emptyViewController
{
if (_emptyViewController) {
return _emptyViewController;
}

if ([self usingEmptyViewController]) {
_emptyViewController = [self.mediaPickerDelegate emptyViewControllerForMediaPickerController:self];
}
else {
_emptyViewController = self.defaultEmptyViewController;
}

return _emptyViewController;
}

- (UIViewController *)defaultEmptyViewController
{
if (_defaultEmptyViewController) {
return _defaultEmptyViewController;
}

_defaultEmptyViewController = [[UIViewController alloc] init];
UILabel *emptyViewLabel = self.defaultEmptyView;
emptyViewLabel.center = _defaultEmptyViewController.view.center;
[[_defaultEmptyViewController view] addSubview:emptyViewLabel];

return _defaultEmptyViewController;
}

- (BOOL)usingEmptyViewController
{
return [self.mediaPickerDelegate respondsToSelector:@selector(emptyViewControllerForMediaPickerController:)];
}

#pragma mark - UICollectionViewDataSource

- (void)updateDataWithRemoved:(NSIndexSet *)removed inserted:(NSIndexSet *)inserted changed:(NSIndexSet *)changed moved:(NSArray<id<WPMediaMove>> *)moves {
Expand Down Expand Up @@ -633,7 +697,11 @@ - (void)refreshData

- (void)refreshDataAnimated:(BOOL)animated
{
[self.refreshControl beginRefreshing];
// Don't show the refreshControl if emptyViewController is being displayed.
if (! _emptyViewController) {
[self.refreshControl beginRefreshing];
}

self.collectionView.allowsSelection = NO;
self.collectionView.allowsMultipleSelection = NO;
self.collectionView.scrollEnabled = NO;
Expand Down Expand Up @@ -782,11 +850,25 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
[self.mediaPickerDelegate mediaPickerController:self didUpdateSearchWithAssetCount:numberOfAssets];
}

[self.emptyView setHidden:(numberOfAssets != 0)];
[self toggleEmptyViewFor:numberOfAssets];

return numberOfAssets;
}

- (void)toggleEmptyViewFor:(NSInteger)numberOfAssets
{
if ([self usingEmptyViewController]) {
if (numberOfAssets > 0) {
[self removeEmptyViewControllerFromView];
} else {
[self addEmptyViewControllerToView];
}
} else {
[self.emptyView setHidden:(numberOfAssets != 0)];
}
}


- (id<WPMediaAsset>)assetForPosition:(NSIndexPath *)indexPath
{
NSInteger itemPosition = indexPath.item;
Expand Down Expand Up @@ -1319,9 +1401,10 @@ - (void)keyboardWillShowNotification:(NSNotification *)notification
self.collectionView.contentInset = contentInset;
self.collectionView.scrollIndicatorInsets = contentInset;

[self centerEmptyView];

[self.collectionView.collectionViewLayout invalidateLayout];
[UIView animateWithDuration:0.2 animations:^{
[self centerEmptyView];
[self.collectionView.collectionViewLayout invalidateLayout];
}];
}

- (void)keyboardWillHideNotification:(NSNotification *)notification
Expand All @@ -1337,27 +1420,44 @@ - (void)keyboardWillHideNotification:(NSNotification *)notification
self.collectionView.contentInset = contentInset;
self.collectionView.scrollIndicatorInsets = contentInset;

[self centerEmptyView];

[self.collectionView.collectionViewLayout invalidateLayout];
[UIView animateWithDuration:0.2 animations:^{
[self centerEmptyView];
[self.collectionView.collectionViewLayout invalidateLayout];
}];
}


/**
Centers the empty view taking into account the collection view height and content insets.
*/
- (void)centerEmptyView
{
self.emptyView.center = self.collectionView.center;
if (self.emptyViewController) {
CGRect emptyViewFrame = [self getEmptyViewFrame];
emptyViewFrame.origin.y -= self.searchBar.frame.size.height/2;
_emptyViewController.view.frame = emptyViewFrame;
} else {
self.emptyView.center = self.collectionView.center;
self.emptyView.frame = [self getEmptyViewFrame];
}
}

- (CGRect)getEmptyViewFrame
{
CGRect emptyViewFrame;

if (_emptyViewController) {
emptyViewFrame = self.collectionView.frame;
} else {
emptyViewFrame = self.emptyView.frame;
}

CGRect emptyViewFrame = self.emptyView.frame;
CGFloat superviewHeight = self.collectionView.frame.size.height;
CGFloat totalInsets = self.collectionView.contentInset.top + self.collectionView.contentInset.bottom;

superviewHeight = superviewHeight - totalInsets > 0 ? superviewHeight - totalInsets : superviewHeight;
emptyViewFrame.origin.y = (superviewHeight / 2.0) - (emptyViewFrame.size.height / 2.0) + self.collectionView.frame.origin.y;

self.emptyView.frame = emptyViewFrame;
return emptyViewFrame;
}

#pragma mark - UIViewControllerPreviewingDelegate
Expand Down
7 changes: 7 additions & 0 deletions Pod/Classes/WPNavigationMediaPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ - (UIView *)emptyViewForMediaPickerController:(WPMediaPickerViewController *)pic
return picker.defaultEmptyView;
}

- (UIViewController *)emptyViewControllerForMediaPickerController:(WPMediaPickerViewController *)picker {
if ([self.delegate respondsToSelector:@selector(emptyViewControllerForMediaPickerController:)]) {
return [self.delegate emptyViewControllerForMediaPickerController:picker];
}
return picker.defaultEmptyViewController;
}

- (void)mediaPickerController:(nonnull WPMediaPickerViewController *)picker didFinishPickingAssets:(nonnull NSArray<WPMediaAsset> *)assets {
if ([self.delegate respondsToSelector:@selector(mediaPickerController:didFinishPickingAssets:)]) {
[self.delegate mediaPickerController:picker didFinishPickingAssets:assets];
Expand Down
2 changes: 1 addition & 1 deletion WPMediaPicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WPMediaPicker"
s.version = "1.1"
s.version = "1.2"
s.summary = "WPMediaPicker is an iOS controller that allows capture and picking of media assets."
s.description = <<-DESC
WPMediaPicker is an iOS controller that allows capture and picking of media assets.
Expand Down