VLCServerBrowsingTVViewController.m 5.9 KB

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