VLCServerBrowsingTVViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "VLCPlaybackService.h"
  15. #import "VLCServerBrowsingController.h"
  16. #import "VLCMaskView.h"
  17. #import "GRKArrayDiff+UICollectionView.h"
  18. @interface VLCServerBrowsingTVViewController ()
  19. {
  20. UILabel *_nothingFoundLabel;
  21. }
  22. @property (nonatomic) VLCServerBrowsingController *browsingController;
  23. @property (nonatomic) NSArray<id <VLCNetworkServerBrowserItem>> *items;
  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. }
  37. return self;
  38. }
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  42. self.nothingFoundLabel.text = NSLocalizedString(@"FOLDER_EMPTY", nil);
  43. [self.nothingFoundLabel sizeToFit];
  44. UIView *nothingFoundView = self.nothingFoundView;
  45. [nothingFoundView sizeToFit];
  46. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  47. [self.view addSubview:nothingFoundView];
  48. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  49. attribute:NSLayoutAttributeCenterY
  50. relatedBy:NSLayoutRelationEqual
  51. toItem:self.view
  52. attribute:NSLayoutAttributeCenterY
  53. multiplier:1.0
  54. constant:0.0];
  55. [self.view addConstraint:yConstraint];
  56. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  57. attribute:NSLayoutAttributeCenterX
  58. relatedBy:NSLayoutRelationEqual
  59. toItem:self.view
  60. attribute:NSLayoutAttributeCenterX
  61. multiplier:1.0
  62. constant:0.0];
  63. [self.view addConstraint:xConstraint];
  64. }
  65. - (void)viewDidAppear:(BOOL)animated
  66. {
  67. [super viewDidAppear:animated];
  68. [self.serverBrowser update];
  69. }
  70. - (void)setSubdirectoryBrowserClass:(Class)subdirectoryBrowserClass
  71. {
  72. NSParameterAssert([subdirectoryBrowserClass isSubclassOfClass:[VLCServerBrowsingTVViewController class]]);
  73. _subdirectoryBrowserClass = subdirectoryBrowserClass;
  74. }
  75. - (Class)subdirectoryBrowserClass
  76. {
  77. return _subdirectoryBrowserClass ?: [self class];
  78. }
  79. #pragma mark -
  80. - (void)reloadData
  81. {
  82. [self.serverBrowser update];
  83. }
  84. #pragma mark - VLCNetworkServerBrowserDelegate
  85. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  86. {
  87. self.title = networkBrowser.title;
  88. NSArray *oldItems = self.items;
  89. NSArray *newItems = networkBrowser.items;
  90. GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:oldItems
  91. currentArray:newItems
  92. identityBlock:^NSString * _Nullable(id <VLCNetworkServerBrowserItem> item) {
  93. return [NSString stringWithFormat:@"%@#%@",item.URL.absoluteString ?: @"", item.name];
  94. }
  95. modifiedBlock:nil];
  96. [diff performBatchUpdatesWithCollectionView:self.collectionView
  97. section:0
  98. dataSourceUpdate:^{
  99. self.items = newItems;
  100. } completion:nil];
  101. _nothingFoundLabel.hidden = self.items.count > 0;
  102. }
  103. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  104. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  105. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  106. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  107. }
  108. #pragma mark -
  109. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  110. {
  111. if (item.isContainer) {
  112. VLCServerBrowsingTVViewController *targetViewController = [[self.subdirectoryBrowserClass alloc] initWithServerBrowser:item.containerBrowser];
  113. [self showViewController:targetViewController sender:self];
  114. } else {
  115. if (singlePlayback) {
  116. [self.browsingController streamFileForItem:item];
  117. } else {
  118. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  119. [self.browsingController configureSubtitlesInMediaList:mediaList];
  120. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  121. }
  122. }
  123. }
  124. #pragma mark - collection view data source
  125. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  126. {
  127. NSInteger count = self.items.count;
  128. self.nothingFoundView.hidden = count > 0;
  129. return count;
  130. }
  131. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. NSArray *items = self.items;
  134. NSInteger row = indexPath.row;
  135. if (row < items.count) {
  136. id<VLCNetworkServerBrowserItem> item = items[row];
  137. if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
  138. ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
  139. }
  140. if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
  141. [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
  142. }
  143. }
  144. }
  145. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. NSInteger row = indexPath.row;
  148. id<VLCNetworkServerBrowserItem> item = self.items[row];
  149. // would make sence if item came from search which isn't
  150. // currently the case on the TV
  151. const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  152. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  153. }
  154. @end