VLCServerBrowsingTVViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. }
  21. @property (nonatomic, readonly) id<VLCNetworkServerBrowser>serverBrowser;
  22. @property (nonatomic) VLCServerBrowsingController *browsingController;
  23. @end
  24. @implementation VLCServerBrowsingTVViewController
  25. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
  26. {
  27. self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  28. if (self) {
  29. _serverBrowser = serverBrowser;
  30. serverBrowser.delegate = self;
  31. _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:serverBrowser];
  32. self.title = serverBrowser.title;
  33. self.downloadArtwork = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDownloadArtwork];
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. self.nothingFoundLabel.text = NSLocalizedString(@"FOLDER_EMPTY", nil);
  41. [self.nothingFoundLabel sizeToFit];
  42. UIView *nothingFoundView = self.nothingFoundView;
  43. [nothingFoundView sizeToFit];
  44. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  45. [self.view addSubview:nothingFoundView];
  46. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  47. attribute:NSLayoutAttributeCenterY
  48. relatedBy:NSLayoutRelationEqual
  49. toItem:self.view
  50. attribute:NSLayoutAttributeCenterY
  51. multiplier:1.0
  52. constant:0.0];
  53. [self.view addConstraint:yConstraint];
  54. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  55. attribute:NSLayoutAttributeCenterX
  56. relatedBy:NSLayoutRelationEqual
  57. toItem:self.view
  58. attribute:NSLayoutAttributeCenterX
  59. multiplier:1.0
  60. constant:0.0];
  61. [self.view addConstraint:xConstraint];
  62. }
  63. - (void)viewDidAppear:(BOOL)animated
  64. {
  65. [super viewDidAppear:animated];
  66. [self.serverBrowser update];
  67. }
  68. #pragma mark -
  69. - (void)reloadData
  70. {
  71. [self.serverBrowser update];
  72. }
  73. #pragma mark -
  74. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  75. {
  76. self.title = networkBrowser.title;
  77. [self.collectionView reloadData];
  78. _nothingFoundLabel.hidden = [self.serverBrowser items].count > 0;
  79. }
  80. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  81. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  82. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  83. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  84. }
  85. #pragma mark -
  86. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  87. {
  88. if (item.isContainer) {
  89. VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:item.containerBrowser];
  90. [self.navigationController pushViewController:targetViewController animated:YES];
  91. } else {
  92. if (singlePlayback) {
  93. [self.browsingController streamFileForItem:item];
  94. } else {
  95. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  96. [self.browsingController configureSubtitlesInMediaList:mediaList];
  97. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  98. }
  99. }
  100. }
  101. #pragma mark - collection view data source
  102. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  103. {
  104. NSInteger count = [self.serverBrowser items].count;
  105. self.nothingFoundView.hidden = count > 0;
  106. return count;
  107. }
  108. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. NSArray *items = self.serverBrowser.items;
  111. NSInteger row = indexPath.row;
  112. if (row < items.count) {
  113. id<VLCNetworkServerBrowserItem> item = items[row];
  114. if ([cell isKindOfClass:[VLCRemoteBrowsingTVCell class]]) {
  115. ((VLCRemoteBrowsingTVCell *) cell).downloadArtwork = self.downloadArtwork;
  116. }
  117. if ([cell conformsToProtocol:@protocol(VLCRemoteBrowsingCell)]) {
  118. [self.browsingController configureCell:(id<VLCRemoteBrowsingCell>)cell withItem:item];
  119. }
  120. }
  121. }
  122. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  123. {
  124. NSInteger row = indexPath.row;
  125. id<VLCNetworkServerBrowserItem> item = self.serverBrowser.items[row];
  126. // would make sence if item came from search which isn't
  127. // currently the case on the TV
  128. const BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  129. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  130. }
  131. @end