VLCRemotePlaybackViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  59. * wearing a Father Xmas hat.
  60. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  61. * and should not be confused with the idea of religious statements or propagation there off
  62. */
  63. NSCalendar *gregorian =
  64. [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  65. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[NSDate date]];
  66. if (dayOfYear >= 354)
  67. self.cachedMediaConeImageView.image = [UIImage imageNamed:@"xmas-cone"];
  68. }
  69. - (void)viewDidLayoutSubviews
  70. {
  71. [super viewDidLayoutSubviews];
  72. UICollectionView *collectionView = self.cachedMediaCollectionView;
  73. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  74. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  75. /*
  76. Update the position from where the collection view's content should
  77. start to fade out. The size of the fade increases as the collection
  78. view scrolls to a maximum of half the navigation bar's height.
  79. */
  80. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  81. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  82. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  83. /*
  84. Position the mask view so that it is always fills the visible area of
  85. the collection view.
  86. */
  87. CGSize collectionViewSize = collectionView.bounds.size;
  88. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  89. }
  90. - (void)viewWillAppear:(BOOL)animated
  91. {
  92. [super viewWillAppear:animated];
  93. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  94. [_reachability startNotifier];
  95. [self updateHTTPServerAddress];
  96. }
  97. - (void)viewWillDisappear:(BOOL)animated
  98. {
  99. [super viewWillDisappear:animated];
  100. [_reachability stopNotifier];
  101. }
  102. - (void)reachabilityChanged
  103. {
  104. [self updateHTTPServerAddress];
  105. }
  106. - (void)updateHTTPServerAddress
  107. {
  108. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  109. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  110. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  111. self.httpServerLabel.text = uploadText;
  112. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  113. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  114. else
  115. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  116. }
  117. - (void)toggleHTTPServer:(id)sender
  118. {
  119. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  120. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  121. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  122. [self updateHTTPServerAddress];
  123. [[NSUserDefaults standardUserDefaults] synchronize];
  124. }
  125. #pragma mark - collection view data source
  126. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  129. return cell;
  130. }
  131. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  132. {
  133. return 1;
  134. }
  135. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. NSString *cellTitle;
  138. NSUInteger row = indexPath.row;
  139. @synchronized(_discoveredFiles) {
  140. if (_discoveredFiles.count > row) {
  141. cellTitle = [_discoveredFiles[row] lastPathComponent];
  142. }
  143. }
  144. [cell prepareForReuse];
  145. [cell setIsDirectory:NO];
  146. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  147. [cell setTitle:cellTitle];
  148. }
  149. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  150. {
  151. NSUInteger ret;
  152. @synchronized(_discoveredFiles) {
  153. ret = _discoveredFiles.count;
  154. }
  155. self.cachedMediaConeImageView.hidden = ret > 0;
  156. return ret;
  157. }
  158. -(BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context
  159. {
  160. if (self.editing) {
  161. return context.nextFocusedIndexPath == nil;
  162. }
  163. return YES;
  164. }
  165. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  166. {
  167. NSIndexPath *nextPath = context.nextFocusedIndexPath;
  168. if (!nextPath) {
  169. self.editing = NO;
  170. }
  171. self.currentlyFocusedIndexPath = nextPath;
  172. }
  173. #pragma mark - editing
  174. - (NSIndexPath *)indexPathToDelete
  175. {
  176. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  177. return indexPathToDelete;
  178. }
  179. - (NSString *)itemToDelete
  180. {
  181. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  182. if (!indexPathToDelete) {
  183. return nil;
  184. }
  185. NSString *ret = nil;
  186. @synchronized(_discoveredFiles) {
  187. NSInteger index = indexPathToDelete.item;
  188. if (index < _discoveredFiles.count) {
  189. ret = _discoveredFiles[index];
  190. }
  191. }
  192. return ret;
  193. }
  194. - (void)setEditing:(BOOL)editing
  195. {
  196. [super setEditing:editing];
  197. UICollectionViewCell *focusedCell = [self.cachedMediaCollectionView cellForItemAtIndexPath:self.currentlyFocusedIndexPath];
  198. if (editing) {
  199. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimationwithSoftMode:NO]
  200. forKey:VLCWiggleAnimationKey];
  201. } else {
  202. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  203. }
  204. }
  205. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  206. {
  207. [super deleteFileAtIndex:indexPathToDelete];
  208. if (!indexPathToDelete) {
  209. return;
  210. }
  211. __block NSString *fileToDelete = nil;
  212. [self.cachedMediaCollectionView performBatchUpdates:^{
  213. @synchronized(_discoveredFiles) {
  214. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  215. [_discoveredFiles removeObject:fileToDelete];
  216. }
  217. [self.cachedMediaCollectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];
  218. } completion:^(BOOL finished) {
  219. [[NSFileManager defaultManager] removeItemAtPath:fileToDelete error:nil];
  220. self.editing = NO;
  221. }];
  222. }
  223. #pragma mark - collection view delegate
  224. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  225. {
  226. NSURL *url;
  227. @synchronized(_discoveredFiles) {
  228. url = [NSURL fileURLWithPath:_discoveredFiles[indexPath.row]];
  229. }
  230. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  231. [medialist addMedia:[VLCMedia mediaWithURL:url]];
  232. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  233. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  234. animated:YES
  235. completion:nil];
  236. }
  237. #pragma mark - media file discovery
  238. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  239. {
  240. @synchronized(_discoveredFiles) {
  241. _discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  242. }
  243. [self.cachedMediaCollectionView reloadData];
  244. }
  245. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  246. {
  247. @synchronized(_discoveredFiles) {
  248. if (![_discoveredFiles containsObject:filePath]) {
  249. [_discoveredFiles addObject:filePath];
  250. }
  251. }
  252. [self.cachedMediaCollectionView reloadData];
  253. }
  254. - (void)mediaFileDeleted:(NSString *)filePath
  255. {
  256. @synchronized(_discoveredFiles) {
  257. [_discoveredFiles removeObject:filePath];
  258. }
  259. [self.cachedMediaCollectionView reloadData];
  260. }
  261. @end