VLCRemotePlaybackViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. * Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCRemotePlaybackViewController.h"
  13. #import "Reachability.h"
  14. #import "VLCHTTPUploaderController.h"
  15. #import "VLCMediaFileDiscoverer.h"
  16. #import "VLCRemoteBrowsingTVCell.h"
  17. #import "VLCMaskView.h"
  18. #import "CAAnimation+VLCWiggle.h"
  19. #define remotePlaybackReuseIdentifer @"remotePlaybackReuseIdentifer"
  20. @interface VLCRemotePlaybackViewController () <UICollectionViewDataSource, UICollectionViewDelegate, VLCMediaFileDiscovererDelegate>
  21. {
  22. Reachability *_reachability;
  23. NSMutableArray<NSString *> *_discoveredFiles;
  24. }
  25. @property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
  26. @end
  27. @implementation VLCRemotePlaybackViewController
  28. - (NSString *)title
  29. {
  30. return NSLocalizedString(@"WEBINTF_TITLE_ATV", nil);
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.cachedMediaCollectionView.collectionViewLayout;
  36. const CGFloat inset = 50.;
  37. flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
  38. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  39. flowLayout.minimumInteritemSpacing = 48.0;
  40. flowLayout.minimumLineSpacing = 100.0;
  41. [self.cachedMediaCollectionView registerNib:[UINib nibWithNibName:@"VLCRemoteBrowsingTVCell" bundle:nil]
  42. forCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier];
  43. _reachability = [Reachability reachabilityForLocalWiFi];
  44. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  45. [notificationCenter addObserver:self
  46. selector:@selector(reachabilityChanged)
  47. name:kReachabilityChangedNotification
  48. object:nil];
  49. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  50. discoverer.filterResultsForPlayability = NO;
  51. _discoveredFiles = [NSMutableArray array];
  52. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  53. discoverer.directoryPath = [[searchPaths firstObject] stringByAppendingPathComponent:@"Upload"];
  54. [discoverer addObserver:self];
  55. [discoverer startDiscovering];
  56. self.cachedMediaLabel.text = NSLocalizedString(@"CACHED_MEDIA", nil);
  57. self.cachedMediaLongLabel.text = NSLocalizedString(@"CACHED_MEDIA_LONG", nil);
  58. }
  59. - (void)viewDidLayoutSubviews
  60. {
  61. [super viewDidLayoutSubviews];
  62. UICollectionView *collectionView = self.cachedMediaCollectionView;
  63. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  64. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  65. /*
  66. Update the position from where the collection view's content should
  67. start to fade out. The size of the fade increases as the collection
  68. view scrolls to a maximum of half the navigation bar's height.
  69. */
  70. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  71. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  72. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  73. /*
  74. Position the mask view so that it is always fills the visible area of
  75. the collection view.
  76. */
  77. CGSize collectionViewSize = collectionView.bounds.size;
  78. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  79. }
  80. - (void)viewWillAppear:(BOOL)animated
  81. {
  82. [super viewWillAppear:animated];
  83. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  84. [_reachability startNotifier];
  85. [self updateHTTPServerAddress];
  86. }
  87. - (void)viewWillDisappear:(BOOL)animated
  88. {
  89. [super viewWillDisappear:animated];
  90. [_reachability stopNotifier];
  91. }
  92. - (void)reachabilityChanged
  93. {
  94. [self updateHTTPServerAddress];
  95. }
  96. - (void)updateHTTPServerAddress
  97. {
  98. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  99. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  100. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  101. self.httpServerLabel.text = uploadText;
  102. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  103. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  104. else
  105. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  106. }
  107. - (void)toggleHTTPServer:(id)sender
  108. {
  109. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  110. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  111. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  112. [self updateHTTPServerAddress];
  113. [[NSUserDefaults standardUserDefaults] synchronize];
  114. }
  115. #pragma mark - collection view data source
  116. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  119. return cell;
  120. }
  121. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  122. {
  123. return 1;
  124. }
  125. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. NSString *cellTitle;
  128. NSUInteger row = indexPath.row;
  129. @synchronized(_discoveredFiles) {
  130. if (_discoveredFiles.count > row) {
  131. cellTitle = [_discoveredFiles[row] lastPathComponent];
  132. }
  133. }
  134. [cell prepareForReuse];
  135. [cell setIsDirectory:NO];
  136. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  137. [cell setTitle:cellTitle];
  138. }
  139. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  140. {
  141. NSUInteger ret;
  142. @synchronized(_discoveredFiles) {
  143. ret = _discoveredFiles.count;
  144. }
  145. self.cachedMediaConeImageView.hidden = ret > 0;
  146. return ret;
  147. }
  148. -(BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context
  149. {
  150. if (self.editing) {
  151. return context.nextFocusedIndexPath == nil;
  152. }
  153. return YES;
  154. }
  155. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  156. {
  157. NSIndexPath *nextPath = context.nextFocusedIndexPath;
  158. if (!nextPath) {
  159. self.editing = NO;
  160. }
  161. self.currentlyFocusedIndexPath = nextPath;
  162. }
  163. #pragma mark - editing
  164. - (NSIndexPath *)indexPathToDelete
  165. {
  166. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  167. return indexPathToDelete;
  168. }
  169. - (NSString *)itemToDelete
  170. {
  171. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  172. if (!indexPathToDelete) {
  173. return nil;
  174. }
  175. NSString *ret;
  176. @synchronized(_discoveredFiles) {
  177. ret = _discoveredFiles[indexPathToDelete.item];
  178. }
  179. return ret;
  180. }
  181. - (void)setEditing:(BOOL)editing
  182. {
  183. [super setEditing:editing];
  184. UICollectionViewCell *focusedCell = [self.cachedMediaCollectionView cellForItemAtIndexPath:self.currentlyFocusedIndexPath];
  185. if (editing) {
  186. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimation]
  187. forKey:VLCWiggleAnimationKey];
  188. } else {
  189. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  190. }
  191. }
  192. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  193. {
  194. if (!indexPathToDelete) {
  195. return;
  196. }
  197. __block NSString *fileToDelete = nil;
  198. [self.cachedMediaCollectionView performBatchUpdates:^{
  199. @synchronized(_discoveredFiles) {
  200. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  201. [_discoveredFiles removeObject:fileToDelete];
  202. }
  203. [self.cachedMediaCollectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];
  204. } completion:^(BOOL finished) {
  205. [[NSFileManager defaultManager] removeItemAtPath:fileToDelete error:nil];
  206. self.editing = NO;
  207. }];
  208. }
  209. #pragma mark - collection view delegate
  210. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  211. {
  212. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  213. NSURL *url;
  214. @synchronized(_discoveredFiles) {
  215. url = [NSURL fileURLWithPath:_discoveredFiles[indexPath.row]];
  216. }
  217. [vpc playURL:url subtitlesFilePath:nil];
  218. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  219. animated:YES
  220. completion:nil];
  221. }
  222. #pragma mark - media file discovery
  223. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  224. {
  225. @synchronized(_discoveredFiles) {
  226. _discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  227. }
  228. [self.cachedMediaCollectionView reloadData];
  229. }
  230. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  231. {
  232. @synchronized(_discoveredFiles) {
  233. if (![_discoveredFiles containsObject:filePath]) {
  234. [_discoveredFiles addObject:filePath];
  235. }
  236. }
  237. [self.cachedMediaCollectionView reloadData];
  238. }
  239. - (void)mediaFileDeleted:(NSString *)filePath
  240. {
  241. @synchronized(_discoveredFiles) {
  242. [_discoveredFiles removeObject:filePath];
  243. }
  244. [self.cachedMediaCollectionView reloadData];
  245. }
  246. @end