VLCServerBrowsingTVViewController.m 5.7 KB

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