VLCServerBrowsingTVViewController.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. @interface VLCServerBrowsingTVViewController ()
  18. {
  19. UILabel *_nothingFoundLabel;
  20. NSIndexPath *_currentFocus;
  21. BOOL _focusChangeAllowed;
  22. }
  23. @property (nonatomic) VLCServerBrowsingController *browsingController;
  24. @end
  25. @implementation VLCServerBrowsingTVViewController
  26. @synthesize subdirectoryBrowserClass = _subdirectoryBrowserClass;
  27. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
  28. {
  29. self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  30. if (self) {
  31. _serverBrowser = serverBrowser;
  32. serverBrowser.delegate = self;
  33. _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:serverBrowser];
  34. self.title = serverBrowser.title;
  35. self.downloadArtwork = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDownloadArtwork];
  36. _currentFocus = [NSIndexPath indexPathForRow:0 inSection:0];
  37. _focusChangeAllowed = YES;
  38. }
  39. return self;
  40. }
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  44. self.nothingFoundLabel.text = NSLocalizedString(@"FOLDER_EMPTY", nil);
  45. [self.nothingFoundLabel sizeToFit];
  46. UIView *nothingFoundView = self.nothingFoundView;
  47. [nothingFoundView sizeToFit];
  48. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  49. [self.view addSubview:nothingFoundView];
  50. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  51. attribute:NSLayoutAttributeCenterY
  52. relatedBy:NSLayoutRelationEqual
  53. toItem:self.view
  54. attribute:NSLayoutAttributeCenterY
  55. multiplier:1.0
  56. constant:0.0];
  57. [self.view addConstraint:yConstraint];
  58. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  59. attribute:NSLayoutAttributeCenterX
  60. relatedBy:NSLayoutRelationEqual
  61. toItem:self.view
  62. attribute:NSLayoutAttributeCenterX
  63. multiplier:1.0
  64. constant:0.0];
  65. [self.view addConstraint:xConstraint];
  66. }
  67. - (void)viewDidAppear:(BOOL)animated
  68. {
  69. [super viewDidAppear:animated];
  70. [self.serverBrowser update];
  71. }
  72. - (void)setSubdirectoryBrowserClass:(Class)subdirectoryBrowserClass
  73. {
  74. NSParameterAssert([subdirectoryBrowserClass isSubclassOfClass:[VLCServerBrowsingTVViewController class]]);
  75. _subdirectoryBrowserClass = subdirectoryBrowserClass;
  76. }
  77. - (Class)subdirectoryBrowserClass
  78. {
  79. return _subdirectoryBrowserClass ?: [self class];
  80. }
  81. #pragma mark -
  82. - (void)reloadData
  83. {
  84. [self.serverBrowser update];
  85. }
  86. #pragma mark - VLCNetworkServerBrowserDelegate
  87. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  88. {
  89. self.title = networkBrowser.title;
  90. [self.collectionView reloadData];
  91. _nothingFoundLabel.hidden = [self.serverBrowser items].count > 0;
  92. }
  93. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  94. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  95. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  96. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  97. }
  98. #pragma mark -
  99. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  100. {
  101. if (item.isContainer) {
  102. VLCServerBrowsingTVViewController *targetViewController = [[self.subdirectoryBrowserClass alloc] initWithServerBrowser:item.containerBrowser];
  103. [self showViewController:targetViewController sender:self];
  104. } else {
  105. if (singlePlayback) {
  106. [self.browsingController streamFileForItem:item];
  107. } else {
  108. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  109. [self.browsingController configureSubtitlesInMediaList:mediaList];
  110. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  111. }
  112. }
  113. }
  114. #pragma mark - collection view data source
  115. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  116. {
  117. NSInteger count = [self.serverBrowser items].count;
  118. self.nothingFoundView.hidden = count > 0;
  119. return count;
  120. }
  121. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. NSArray *items = self.serverBrowser.items;
  124. NSInteger row = indexPath.row;
  125. if (row < items.count) {
  126. id<VLCNetworkServerBrowserItem> item = items[row];
  127. if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
  128. ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
  129. }
  130. if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
  131. [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
  132. }
  133. }
  134. }
  135. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. NSInteger row = indexPath.row;
  138. id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[row];
  139. // would make sence if item came from search which isn't
  140. // currently the case on the TV
  141. const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  142. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  143. }
  144. #pragma mark - focus recovery workaround
  145. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  146. {
  147. if (context.previouslyFocusedIndexPath == nil && context.nextFocusedIndexPath != nil)
  148. [self setNeedsFocusUpdate];
  149. if (context.previouslyFocusedIndexPath != nil && context.nextFocusedIndexPath != nil)
  150. _currentFocus = context.nextFocusedIndexPath;
  151. _focusChangeAllowed = context.nextFocusedIndexPath != nil;
  152. }
  153. - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. return _focusChangeAllowed || [_currentFocus compare:indexPath] == NSOrderedSame;
  156. }
  157. - (NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView
  158. {
  159. return _currentFocus;
  160. }
  161. @end