VLCServerBrowsingTVViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, readonly) id<VLCNetworkServerBrowser>serverBrowser;
  24. @property (nonatomic) VLCServerBrowsingController *browsingController;
  25. @end
  26. @implementation VLCServerBrowsingTVViewController
  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. #pragma mark -
  73. - (void)reloadData
  74. {
  75. [self.serverBrowser update];
  76. }
  77. #pragma mark -
  78. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  79. {
  80. self.title = networkBrowser.title;
  81. [self.collectionView reloadData];
  82. _nothingFoundLabel.hidden = [self.serverBrowser items].count > 0;
  83. }
  84. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  85. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  86. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  87. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  88. }
  89. #pragma mark -
  90. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  91. {
  92. if (item.isContainer) {
  93. VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:item.containerBrowser];
  94. [self.navigationController pushViewController:targetViewController animated:YES];
  95. } else {
  96. if (singlePlayback) {
  97. [self.browsingController streamFileForItem:item];
  98. } else {
  99. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  100. [self.browsingController configureSubtitlesInMediaList:mediaList];
  101. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  102. }
  103. }
  104. }
  105. #pragma mark - collection view data source
  106. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  107. {
  108. NSInteger count = [self.serverBrowser items].count;
  109. self.nothingFoundView.hidden = count > 0;
  110. return count;
  111. }
  112. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  113. {
  114. NSArray *items = self.serverBrowser.items;
  115. NSInteger row = indexPath.row;
  116. if (row < items.count) {
  117. id<VLCNetworkServerBrowserItem> item = items[row];
  118. if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
  119. ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
  120. }
  121. if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
  122. [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
  123. }
  124. }
  125. }
  126. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. NSInteger row = indexPath.row;
  129. id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[row];
  130. // would make sence if item came from search which isn't
  131. // currently the case on the TV
  132. const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  133. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  134. }
  135. #pragma mark - focus recovery workaround
  136. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  137. {
  138. if (context.previouslyFocusedIndexPath == nil && context.nextFocusedIndexPath != nil)
  139. [self setNeedsFocusUpdate];
  140. if (context.previouslyFocusedIndexPath != nil && context.nextFocusedIndexPath != nil)
  141. _currentFocus = context.nextFocusedIndexPath;
  142. _focusChangeAllowed = context.nextFocusedIndexPath != nil;
  143. }
  144. - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. return _focusChangeAllowed || [_currentFocus compare:indexPath] == NSOrderedSame;
  147. }
  148. - (NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView
  149. {
  150. return _currentFocus;
  151. }
  152. @end