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