VLCRemotePlaybackViewController.m 11 KB

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