VLCRemotePlaybackViewController.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCRemotePlaybackViewController.h"
  12. #import "Reachability.h"
  13. #import "VLCHTTPUploaderController.h"
  14. #import "VLCMediaFileDiscoverer.h"
  15. #import "VLCRemoteBrowsingTVCell.h"
  16. #import "VLCMaskView.h"
  17. #define remotePlaybackReuseIdentifer @"remotePlaybackReuseIdentifer"
  18. @interface VLCRemotePlaybackViewController () <UICollectionViewDataSource, UICollectionViewDelegate, VLCMediaFileDiscovererDelegate>
  19. {
  20. Reachability *_reachability;
  21. NSMutableArray *_discoveredFiles;
  22. }
  23. @end
  24. @implementation VLCRemotePlaybackViewController
  25. - (NSString *)title
  26. {
  27. return NSLocalizedString(@"WEBINTF_TITLE_ATV", nil);
  28. }
  29. - (void)viewDidLoad
  30. {
  31. [super viewDidLoad];
  32. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.cachedMediaCollectionView.collectionViewLayout;
  33. const CGFloat inset = 50.;
  34. flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
  35. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  36. flowLayout.minimumInteritemSpacing = 48.0;
  37. flowLayout.minimumLineSpacing = 100.0;
  38. [self.cachedMediaCollectionView registerNib:[UINib nibWithNibName:@"VLCRemoteBrowsingTVCell" bundle:nil]
  39. forCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier];
  40. _reachability = [Reachability reachabilityForLocalWiFi];
  41. self.httpServerLabel.textColor = [UIColor VLCDarkBackgroundColor];
  42. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  43. [notificationCenter addObserver:self
  44. selector:@selector(reachabilityChanged)
  45. name:kReachabilityChangedNotification
  46. object:nil];
  47. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  48. _discoveredFiles = [NSMutableArray array];
  49. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  50. discoverer.directoryPath = [[searchPaths firstObject] stringByAppendingPathComponent:@"Upload"];
  51. [discoverer addObserver:self];
  52. [discoverer startDiscovering];
  53. }
  54. - (void)viewDidLayoutSubviews
  55. {
  56. [super viewDidLayoutSubviews];
  57. UICollectionView *collectionView = self.cachedMediaCollectionView;
  58. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  59. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  60. /*
  61. Update the position from where the collection view's content should
  62. start to fade out. The size of the fade increases as the collection
  63. view scrolls to a maximum of half the navigation bar's height.
  64. */
  65. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  66. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  67. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  68. /*
  69. Position the mask view so that it is always fills the visible area of
  70. the collection view.
  71. */
  72. CGSize collectionViewSize = collectionView.bounds.size;
  73. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  74. }
  75. - (void)viewWillAppear:(BOOL)animated
  76. {
  77. [super viewWillAppear:animated];
  78. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  79. [_reachability startNotifier];
  80. [self updateHTTPServerAddress];
  81. }
  82. - (void)viewWillDisappear:(BOOL)animated
  83. {
  84. [super viewWillDisappear:animated];
  85. [_reachability stopNotifier];
  86. }
  87. - (void)reachabilityChanged
  88. {
  89. [self updateHTTPServerAddress];
  90. }
  91. - (void)updateHTTPServerAddress
  92. {
  93. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  94. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  95. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  96. self.httpServerLabel.text = uploadText;
  97. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  98. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  99. else
  100. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  101. }
  102. - (void)toggleHTTPServer:(id)sender
  103. {
  104. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  105. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  106. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  107. [self updateHTTPServerAddress];
  108. [[NSUserDefaults standardUserDefaults] synchronize];
  109. }
  110. #pragma mark - collection view data source
  111. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  114. return cell;
  115. }
  116. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  117. {
  118. return 1;
  119. }
  120. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. NSString *cellTitle;
  123. NSUInteger row = indexPath.row;
  124. @synchronized(_discoveredFiles) {
  125. if (_discoveredFiles.count > row) {
  126. cellTitle = [_discoveredFiles[row] lastPathComponent];
  127. }
  128. }
  129. [cell prepareForReuse];
  130. [cell setIsDirectory:NO];
  131. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  132. [cell setTitle:cellTitle];
  133. }
  134. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  135. {
  136. NSUInteger ret;
  137. @synchronized(_discoveredFiles) {
  138. ret = _discoveredFiles.count;
  139. }
  140. return ret;
  141. }
  142. #pragma mark - collection view delegate
  143. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  146. NSURL *url;
  147. @synchronized(_discoveredFiles) {
  148. url = [NSURL fileURLWithPath:_discoveredFiles[indexPath.row]];
  149. }
  150. [vpc playURL:url subtitlesFilePath:nil];
  151. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  152. animated:YES
  153. completion:nil];
  154. }
  155. #pragma mark - media file discovery
  156. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  157. {
  158. @synchronized(_discoveredFiles) {
  159. _discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  160. }
  161. [self.cachedMediaCollectionView reloadData];
  162. }
  163. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  164. {
  165. @synchronized(_discoveredFiles) {
  166. if (![_discoveredFiles containsObject:filePath]) {
  167. [_discoveredFiles addObject:filePath];
  168. }
  169. }
  170. [self.cachedMediaCollectionView reloadData];
  171. }
  172. - (void)mediaFileDeleted:(NSString *)filePath
  173. {
  174. @synchronized(_discoveredFiles) {
  175. [_discoveredFiles removeObject:filePath];
  176. }
  177. [self.cachedMediaCollectionView reloadData];
  178. }
  179. @end