VLCServerBrowsingTVViewController.m 5.9 KB

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