VLCServerBrowsingTVViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCServerBrowsingTVViewController.h"
  12. #import "VLCRemoteBrowsingTVCell.h"
  13. #import "VLCPlayerDisplayController.h"
  14. #import "VLCPlaybackController.h"
  15. #import "VLCServerBrowsingController.h"
  16. #import "VLCMaskView.h"
  17. #import "GRKArrayDiff+UICollectionView.h"
  18. @interface VLCServerBrowsingTVViewController ()
  19. {
  20. UILabel *_nothingFoundLabel;
  21. NSIndexPath *_currentFocus;
  22. BOOL _focusChangeAllowed;
  23. }
  24. @property (nonatomic) VLCServerBrowsingController *browsingController;
  25. @property (nonatomic) NSArray<id <VLCNetworkServerBrowserItem>> *items;
  26. @end
  27. @implementation VLCServerBrowsingTVViewController
  28. @synthesize subdirectoryBrowserClass = _subdirectoryBrowserClass;
  29. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
  30. {
  31. self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  32. if (self) {
  33. _serverBrowser = serverBrowser;
  34. serverBrowser.delegate = self;
  35. _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:serverBrowser];
  36. self.title = serverBrowser.title;
  37. self.downloadArtwork = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDownloadArtwork];
  38. _currentFocus = [NSIndexPath indexPathForRow:0 inSection:0];
  39. _focusChangeAllowed = YES;
  40. }
  41. return self;
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. self.nothingFoundLabel.text = NSLocalizedString(@"FOLDER_EMPTY", nil);
  47. [self.nothingFoundLabel sizeToFit];
  48. UIView *nothingFoundView = self.nothingFoundView;
  49. [nothingFoundView sizeToFit];
  50. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  51. [self.view addSubview:nothingFoundView];
  52. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  53. attribute:NSLayoutAttributeCenterY
  54. relatedBy:NSLayoutRelationEqual
  55. toItem:self.view
  56. attribute:NSLayoutAttributeCenterY
  57. multiplier:1.0
  58. constant:0.0];
  59. [self.view addConstraint:yConstraint];
  60. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  61. attribute:NSLayoutAttributeCenterX
  62. relatedBy:NSLayoutRelationEqual
  63. toItem:self.view
  64. attribute:NSLayoutAttributeCenterX
  65. multiplier:1.0
  66. constant:0.0];
  67. [self.view addConstraint:xConstraint];
  68. }
  69. - (void)viewDidAppear:(BOOL)animated
  70. {
  71. [super viewDidAppear:animated];
  72. [self.serverBrowser update];
  73. }
  74. - (void)setSubdirectoryBrowserClass:(Class)subdirectoryBrowserClass
  75. {
  76. NSParameterAssert([subdirectoryBrowserClass isSubclassOfClass:[VLCServerBrowsingTVViewController class]]);
  77. _subdirectoryBrowserClass = subdirectoryBrowserClass;
  78. }
  79. - (Class)subdirectoryBrowserClass
  80. {
  81. return _subdirectoryBrowserClass ?: [self class];
  82. }
  83. #pragma mark -
  84. - (void)reloadData
  85. {
  86. [self.serverBrowser update];
  87. }
  88. #pragma mark - VLCNetworkServerBrowserDelegate
  89. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  90. {
  91. self.title = networkBrowser.title;
  92. NSArray *oldItems = self.items;
  93. NSArray *newItems = networkBrowser.items;
  94. GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:oldItems
  95. currentArray:newItems
  96. identityBlock:^NSString * _Nullable(id <VLCNetworkServerBrowserItem> item) {
  97. return [NSString stringWithFormat:@"%@#%@",item.URL.absoluteString ?: @"", item.name];
  98. }
  99. modifiedBlock:nil];
  100. [diff performBatchUpdatesWithCollectionView:self.collectionView
  101. section:0
  102. dataSourceUpdate:^{
  103. self.items = newItems;
  104. } completion:nil];
  105. _nothingFoundLabel.hidden = self.items.count > 0;
  106. }
  107. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  108. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  109. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  110. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  111. }
  112. #pragma mark -
  113. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  114. {
  115. if (item.isContainer) {
  116. VLCServerBrowsingTVViewController *targetViewController = [[self.subdirectoryBrowserClass alloc] initWithServerBrowser:item.containerBrowser];
  117. [self showViewController:targetViewController sender:self];
  118. } else {
  119. if (singlePlayback) {
  120. [self.browsingController streamFileForItem:item];
  121. } else {
  122. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  123. [self.browsingController configureSubtitlesInMediaList:mediaList];
  124. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  125. }
  126. }
  127. }
  128. #pragma mark - collection view data source
  129. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  130. {
  131. NSInteger count = self.items.count;
  132. self.nothingFoundView.hidden = count > 0;
  133. return count;
  134. }
  135. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. NSArray *items = self.items;
  138. NSInteger row = indexPath.row;
  139. if (row < items.count) {
  140. id<VLCNetworkServerBrowserItem> item = items[row];
  141. if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
  142. ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
  143. }
  144. if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
  145. [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
  146. }
  147. }
  148. }
  149. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. NSInteger row = indexPath.row;
  152. id<VLCNetworkServerBrowserItem> item = self.items[row];
  153. // would make sence if item came from search which isn't
  154. // currently the case on the TV
  155. const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  156. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  157. }
  158. #pragma mark - focus recovery workaround
  159. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  160. {
  161. if (context.previouslyFocusedIndexPath == nil && context.nextFocusedIndexPath != nil)
  162. [self setNeedsFocusUpdate];
  163. if (context.previouslyFocusedIndexPath != nil && context.nextFocusedIndexPath != nil)
  164. _currentFocus = context.nextFocusedIndexPath;
  165. _focusChangeAllowed = context.nextFocusedIndexPath != nil;
  166. }
  167. - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath
  168. {
  169. return _focusChangeAllowed || [_currentFocus compare:indexPath] == NSOrderedSame;
  170. }
  171. - (NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView
  172. {
  173. return _currentFocus;
  174. }
  175. @end