Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 9fffaa4

Browse files
authored
Merge pull request #303 from wordpress-mobile/issue/9504-no_results_vc_support
Adding support to use a View Controller for the empty view.
2 parents 277d61e + 685607c commit 9fffaa4

File tree

4 files changed

+144
-16
lines changed

4 files changed

+144
-16
lines changed

Pod/Classes/WPMediaPickerViewController.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@
198198
*/
199199
- (nullable UIView *)emptyViewForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;
200200

201+
/**
202+
* Asks the delegate for an empty view to show when there are no assets
203+
* to be displayed. If no empty view is required, you have to implement this
204+
* method and return `nil`.
205+
*
206+
* @param picker The controller object managing the assets picker interface.
207+
* @return An empty view controller to display or `nil` to not display any.
208+
*
209+
* If this method is not implemented, a default ViewController with a default
210+
* UILabel will be displayed.
211+
*/
212+
- (nullable UIViewController *)emptyViewControllerForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;
213+
201214
@end
202215

203216

@@ -234,7 +247,8 @@
234247
@property (nonatomic, strong, readonly, nullable) UISearchBar *searchBar;
235248

236249
/**
237-
The default empty view. When `emptyViewForMediaPickerController:` is not implemented, use this property to style the mensaje.
250+
The default empty view. When `emptyViewForMediaPickerController:` is not implemented,
251+
use this property to style the message.
238252
*/
239253
@property (nonatomic, strong, readonly, nonnull) UILabel *defaultEmptyView;
240254

@@ -293,6 +307,13 @@
293307
*/
294308
- (nonnull UIViewController *)defaultPreviewViewControllerForAsset:(nonnull id<WPMediaAsset>)asset;
295309

310+
/**
311+
Return a View Controller to present `defaultEmptyView`.
312+
313+
@return a view controller to present the empty view.
314+
*/
315+
- (UIViewController *)defaultEmptyViewController;
316+
296317
/**
297318
Calculates the appropriate cell height/width given the desired number of cells per line, desired space
298319
between cells, and total width of the frame containing the cells.

Pod/Classes/WPMediaPickerViewController.m

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ @interface WPMediaPickerViewController ()
4949

5050
@property (nonatomic, strong) UIView *emptyView;
5151
@property (nonatomic, strong) UILabel *defaultEmptyView;
52+
@property (nonatomic, strong) UIViewController *emptyViewController;
53+
@property (nonatomic, strong) UIViewController *defaultEmptyViewController;
54+
5255

5356
@property (nonatomic, strong) WPActionBar *accessoryActionBar;
5457
@property (nonatomic, strong) UIButton *selectedActionButton;
@@ -553,6 +556,8 @@ - (void)showCapture {
553556
return;
554557
}
555558

559+
#pragma mark - Empty View support
560+
556561
- (UIView *)emptyView
557562
{
558563
if (_emptyView) {
@@ -570,8 +575,12 @@ - (UIView *)emptyView
570575

571576
- (void)addEmptyViewToView
572577
{
573-
if (self.emptyView.superview == nil) {
574-
[self.collectionView addSubview:_emptyView];
578+
if ([self usingEmptyViewController]) {
579+
[self addEmptyViewControllerToView];
580+
} else {
581+
if (self.emptyView.superview == nil) {
582+
[self.collectionView addSubview:_emptyView];
583+
}
575584
}
576585
}
577586

@@ -586,6 +595,61 @@ - (UILabel *)defaultEmptyView
586595
return _defaultEmptyView;
587596
}
588597

598+
#pragma mark - Empty View Controller support
599+
600+
- (void)addEmptyViewControllerToView
601+
{
602+
if (self.emptyViewController.view.superview == nil) {
603+
[self.collectionView addSubview:self.emptyViewController.view];
604+
_emptyViewController.view.frame = self.collectionView.frame;
605+
[self addChildViewController:_emptyViewController];
606+
[_emptyViewController didMoveToParentViewController:self];
607+
[self centerEmptyView];
608+
}
609+
}
610+
611+
- (void)removeEmptyViewControllerFromView
612+
{
613+
[_emptyViewController willMoveToParentViewController:nil];
614+
[_emptyViewController.view removeFromSuperview];
615+
[_emptyViewController removeFromParentViewController];
616+
}
617+
618+
- (UIViewController *)emptyViewController
619+
{
620+
if (_emptyViewController) {
621+
return _emptyViewController;
622+
}
623+
624+
if ([self usingEmptyViewController]) {
625+
_emptyViewController = [self.mediaPickerDelegate emptyViewControllerForMediaPickerController:self];
626+
}
627+
else {
628+
_emptyViewController = self.defaultEmptyViewController;
629+
}
630+
631+
return _emptyViewController;
632+
}
633+
634+
- (UIViewController *)defaultEmptyViewController
635+
{
636+
if (_defaultEmptyViewController) {
637+
return _defaultEmptyViewController;
638+
}
639+
640+
_defaultEmptyViewController = [[UIViewController alloc] init];
641+
UILabel *emptyViewLabel = self.defaultEmptyView;
642+
emptyViewLabel.center = _defaultEmptyViewController.view.center;
643+
[[_defaultEmptyViewController view] addSubview:emptyViewLabel];
644+
645+
return _defaultEmptyViewController;
646+
}
647+
648+
- (BOOL)usingEmptyViewController
649+
{
650+
return [self.mediaPickerDelegate respondsToSelector:@selector(emptyViewControllerForMediaPickerController:)];
651+
}
652+
589653
#pragma mark - UICollectionViewDataSource
590654

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

634698
- (void)refreshDataAnimated:(BOOL)animated
635699
{
636-
[self.refreshControl beginRefreshing];
700+
// Don't show the refreshControl if emptyViewController is being displayed.
701+
if (! _emptyViewController) {
702+
[self.refreshControl beginRefreshing];
703+
}
704+
637705
self.collectionView.allowsSelection = NO;
638706
self.collectionView.allowsMultipleSelection = NO;
639707
self.collectionView.scrollEnabled = NO;
@@ -782,11 +850,25 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
782850
[self.mediaPickerDelegate mediaPickerController:self didUpdateSearchWithAssetCount:numberOfAssets];
783851
}
784852

785-
[self.emptyView setHidden:(numberOfAssets != 0)];
853+
[self toggleEmptyViewFor:numberOfAssets];
786854

787855
return numberOfAssets;
788856
}
789857

858+
- (void)toggleEmptyViewFor:(NSInteger)numberOfAssets
859+
{
860+
if ([self usingEmptyViewController]) {
861+
if (numberOfAssets > 0) {
862+
[self removeEmptyViewControllerFromView];
863+
} else {
864+
[self addEmptyViewControllerToView];
865+
}
866+
} else {
867+
[self.emptyView setHidden:(numberOfAssets != 0)];
868+
}
869+
}
870+
871+
790872
- (id<WPMediaAsset>)assetForPosition:(NSIndexPath *)indexPath
791873
{
792874
NSInteger itemPosition = indexPath.item;
@@ -1319,9 +1401,10 @@ - (void)keyboardWillShowNotification:(NSNotification *)notification
13191401
self.collectionView.contentInset = contentInset;
13201402
self.collectionView.scrollIndicatorInsets = contentInset;
13211403

1322-
[self centerEmptyView];
1323-
1324-
[self.collectionView.collectionViewLayout invalidateLayout];
1404+
[UIView animateWithDuration:0.2 animations:^{
1405+
[self centerEmptyView];
1406+
[self.collectionView.collectionViewLayout invalidateLayout];
1407+
}];
13251408
}
13261409

13271410
- (void)keyboardWillHideNotification:(NSNotification *)notification
@@ -1337,27 +1420,44 @@ - (void)keyboardWillHideNotification:(NSNotification *)notification
13371420
self.collectionView.contentInset = contentInset;
13381421
self.collectionView.scrollIndicatorInsets = contentInset;
13391422

1340-
[self centerEmptyView];
1341-
1342-
[self.collectionView.collectionViewLayout invalidateLayout];
1423+
[UIView animateWithDuration:0.2 animations:^{
1424+
[self centerEmptyView];
1425+
[self.collectionView.collectionViewLayout invalidateLayout];
1426+
}];
13431427
}
13441428

1345-
13461429
/**
13471430
Centers the empty view taking into account the collection view height and content insets.
13481431
*/
13491432
- (void)centerEmptyView
13501433
{
1351-
self.emptyView.center = self.collectionView.center;
1434+
if (self.emptyViewController) {
1435+
CGRect emptyViewFrame = [self getEmptyViewFrame];
1436+
emptyViewFrame.origin.y -= self.searchBar.frame.size.height/2;
1437+
_emptyViewController.view.frame = emptyViewFrame;
1438+
} else {
1439+
self.emptyView.center = self.collectionView.center;
1440+
self.emptyView.frame = [self getEmptyViewFrame];
1441+
}
1442+
}
1443+
1444+
- (CGRect)getEmptyViewFrame
1445+
{
1446+
CGRect emptyViewFrame;
1447+
1448+
if (_emptyViewController) {
1449+
emptyViewFrame = self.collectionView.frame;
1450+
} else {
1451+
emptyViewFrame = self.emptyView.frame;
1452+
}
13521453

1353-
CGRect emptyViewFrame = self.emptyView.frame;
13541454
CGFloat superviewHeight = self.collectionView.frame.size.height;
13551455
CGFloat totalInsets = self.collectionView.contentInset.top + self.collectionView.contentInset.bottom;
13561456

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

1360-
self.emptyView.frame = emptyViewFrame;
1460+
return emptyViewFrame;
13611461
}
13621462

13631463
#pragma mark - UIViewControllerPreviewingDelegate

Pod/Classes/WPNavigationMediaPickerViewController.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ - (UIView *)emptyViewForMediaPickerController:(WPMediaPickerViewController *)pic
200200
return picker.defaultEmptyView;
201201
}
202202

203+
- (UIViewController *)emptyViewControllerForMediaPickerController:(WPMediaPickerViewController *)picker {
204+
if ([self.delegate respondsToSelector:@selector(emptyViewControllerForMediaPickerController:)]) {
205+
return [self.delegate emptyViewControllerForMediaPickerController:picker];
206+
}
207+
return picker.defaultEmptyViewController;
208+
}
209+
203210
- (void)mediaPickerController:(nonnull WPMediaPickerViewController *)picker didFinishPickingAssets:(nonnull NSArray<WPMediaAsset> *)assets {
204211
if ([self.delegate respondsToSelector:@selector(mediaPickerController:didFinishPickingAssets:)]) {
205212
[self.delegate mediaPickerController:picker didFinishPickingAssets:assets];

WPMediaPicker.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WPMediaPicker"
3-
s.version = "1.1"
3+
s.version = "1.2"
44
s.summary = "WPMediaPicker is an iOS controller that allows capture and picking of media assets."
55
s.description = <<-DESC
66
WPMediaPicker is an iOS controller that allows capture and picking of media assets.

0 commit comments

Comments
 (0)