@@ -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
0 commit comments