VLCSearchableServerBrowsingTVViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2016 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Vincent L. Cone <vincent.l.cone # gmail.com>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCSearchableServerBrowsingTVViewController.h"
  12. #import "VLCNetworkServerSearchBrowser.h"
  13. #import "VLCSearchController.h"
  14. static NSString * const VLCSearchableServerBrowsingTVViewControllerSectionHeaderKey = @"VLCSearchableServerBrowsingTVViewControllerSectionHeader";
  15. @interface VLCSearchableServerBrowsingTVViewControllerHeader : UICollectionReusableView
  16. @property (nonatomic) UISearchBar *searchBar;
  17. @end
  18. @interface VLCSearchableServerBrowsingTVViewController() <UISearchControllerDelegate, UISearchResultsUpdating>
  19. @property (nonatomic) UISearchController *searchController;
  20. @property (nonatomic) VLCNetworkServerSearchBrowser *searchBrowser;
  21. @end
  22. @implementation VLCSearchableServerBrowsingTVViewController
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. VLCNetworkServerSearchBrowser *searchBrowser = [[VLCNetworkServerSearchBrowser alloc] initWithServerBrowser:self.serverBrowser];
  27. VLCServerBrowsingTVViewController *resultBrowsingViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:searchBrowser];
  28. resultBrowsingViewController.subdirectoryBrowserClass = [self class];
  29. _searchBrowser = searchBrowser;
  30. UISearchController *searchController = [[VLCSearchController alloc] initWithSearchResultsController:resultBrowsingViewController];
  31. searchController.searchResultsUpdater = self;
  32. searchController.delegate = self;
  33. searchController.hidesNavigationBarDuringPresentation = NO;
  34. _searchController = searchController;
  35. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
  36. flowLayout.headerReferenceSize = searchController.searchBar.bounds.size;
  37. [self.collectionView registerClass:[VLCSearchableServerBrowsingTVViewControllerHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:VLCSearchableServerBrowsingTVViewControllerSectionHeaderKey];
  38. }
  39. #pragma mark - UICollectionViewDataSource
  40. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  41. {
  42. UICollectionReusableView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:VLCSearchableServerBrowsingTVViewControllerSectionHeaderKey forIndexPath:indexPath];
  43. VLCSearchableServerBrowsingTVViewControllerHeader *header = [supplementaryView isKindOfClass:[VLCSearchableServerBrowsingTVViewControllerHeader class]] ? (id)supplementaryView : nil;
  44. UISearchController *searchController = self.searchController;
  45. header.searchBar = searchController.searchBar;
  46. if (!searchController.active) {
  47. [header addSubview:searchController.searchBar];
  48. }
  49. return supplementaryView;
  50. }
  51. #pragma mark - VLCNetworkServerBrowserDelegate
  52. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  53. {
  54. [super networkServerBrowserDidUpdate:networkBrowser];
  55. if (self.searchController.active) {
  56. [self.searchBrowser networkServerBrowserDidUpdate:networkBrowser];
  57. }
  58. }
  59. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error
  60. {
  61. if (self.searchController.active) {
  62. [self.searchBrowser networkServerBrowser:networkBrowser requestDidFailWithError:error];
  63. } else {
  64. [super networkServerBrowser:networkBrowser requestDidFailWithError:error];
  65. }
  66. }
  67. #pragma mark - UISearchControllerDelegate
  68. - (void)willPresentSearchController:(UISearchController *)searchController
  69. {
  70. [self.searchBrowser networkServerBrowserDidUpdate:self.serverBrowser];
  71. }
  72. #pragma mark - UISearchResultsUpdating
  73. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  74. {
  75. self.searchBrowser.searchText = searchController.searchBar.text;
  76. }
  77. @end
  78. @implementation VLCSearchableServerBrowsingTVViewControllerHeader
  79. - (void)setSearchBar:(UISearchBar *)searchBar {
  80. _searchBar = searchBar;
  81. }
  82. - (void)layoutSubviews
  83. {
  84. [super layoutSubviews];
  85. // UISearchController 'steals' the search bar from us when it's active.
  86. if (self.searchBar.superview == self) {
  87. self.searchBar.center = self.center;
  88. }
  89. }
  90. @end