VLCRemotePlaybackViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. static NSString *const VLCWiggleAnimationKey = @"VLCWiggleAnimation";
  21. @interface VLCRemotePlaybackViewController () <UICollectionViewDataSource, UICollectionViewDelegate, VLCMediaFileDiscovererDelegate>
  22. {
  23. Reachability *_reachability;
  24. NSMutableArray<NSString *> *_discoveredFiles;
  25. }
  26. @property (nonatomic) UITapGestureRecognizer *playPausePressRecognizer;
  27. @property (nonatomic) UITapGestureRecognizer *cancelRecognizer;
  28. @property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
  29. @end
  30. @implementation VLCRemotePlaybackViewController
  31. - (NSString *)title
  32. {
  33. return NSLocalizedString(@"WEBINTF_TITLE_ATV", nil);
  34. }
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.cachedMediaCollectionView.collectionViewLayout;
  39. const CGFloat inset = 50.;
  40. flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
  41. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  42. flowLayout.minimumInteritemSpacing = 48.0;
  43. flowLayout.minimumLineSpacing = 100.0;
  44. [self.cachedMediaCollectionView registerNib:[UINib nibWithNibName:@"VLCRemoteBrowsingTVCell" bundle:nil]
  45. forCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier];
  46. _reachability = [Reachability reachabilityForLocalWiFi];
  47. self.httpServerLabel.textColor = [UIColor VLCDarkBackgroundColor];
  48. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  49. [notificationCenter addObserver:self
  50. selector:@selector(reachabilityChanged)
  51. name:kReachabilityChangedNotification
  52. object:nil];
  53. VLCMediaFileDiscoverer *discoverer = [VLCMediaFileDiscoverer sharedInstance];
  54. discoverer.filterResultsForPlayability = NO;
  55. _discoveredFiles = [NSMutableArray array];
  56. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  57. discoverer.directoryPath = [[searchPaths firstObject] stringByAppendingPathComponent:@"Upload"];
  58. [discoverer addObserver:self];
  59. [discoverer startDiscovering];
  60. UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startEditMode)];
  61. recognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
  62. recognizer.minimumPressDuration = 1.0;
  63. [self.view addGestureRecognizer:recognizer];
  64. UITapGestureRecognizer *cancelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditMode)];
  65. cancelRecognizer.allowedPressTypes = @[@(UIPressTypeSelect),@(UIPressTypeMenu)];
  66. self.cancelRecognizer = cancelRecognizer;
  67. [self.view addGestureRecognizer:cancelRecognizer];
  68. UITapGestureRecognizer *playPauseRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePlayPausePress)];
  69. playPauseRecognizer.allowedPressTypes = @[@(UIPressTypePlayPause)];
  70. self.playPausePressRecognizer = playPauseRecognizer;
  71. [self.view addGestureRecognizer:playPauseRecognizer];
  72. }
  73. - (void)viewDidLayoutSubviews
  74. {
  75. [super viewDidLayoutSubviews];
  76. UICollectionView *collectionView = self.cachedMediaCollectionView;
  77. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  78. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  79. /*
  80. Update the position from where the collection view's content should
  81. start to fade out. The size of the fade increases as the collection
  82. view scrolls to a maximum of half the navigation bar's height.
  83. */
  84. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  85. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  86. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  87. /*
  88. Position the mask view so that it is always fills the visible area of
  89. the collection view.
  90. */
  91. CGSize collectionViewSize = collectionView.bounds.size;
  92. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  93. }
  94. - (void)viewWillAppear:(BOOL)animated
  95. {
  96. [super viewWillAppear:animated];
  97. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  98. [_reachability startNotifier];
  99. [self updateHTTPServerAddress];
  100. }
  101. - (void)viewWillDisappear:(BOOL)animated
  102. {
  103. [super viewWillDisappear:animated];
  104. [_reachability stopNotifier];
  105. }
  106. - (void)reachabilityChanged
  107. {
  108. [self updateHTTPServerAddress];
  109. }
  110. - (void)updateHTTPServerAddress
  111. {
  112. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  113. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  114. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  115. self.httpServerLabel.text = uploadText;
  116. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  117. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  118. else
  119. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  120. }
  121. - (void)toggleHTTPServer:(id)sender
  122. {
  123. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  124. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  125. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  126. [self updateHTTPServerAddress];
  127. [[NSUserDefaults standardUserDefaults] synchronize];
  128. }
  129. - (void)handlePlayPausePress
  130. {
  131. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  132. if (!indexPathToDelete) {
  133. return;
  134. }
  135. NSString *fileToDelete = nil;
  136. @synchronized(_discoveredFiles) {
  137. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  138. }
  139. NSString *title = fileToDelete.lastPathComponent;
  140. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
  141. message:nil
  142. preferredStyle:UIAlertControllerStyleAlert];
  143. UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  144. style:UIAlertActionStyleDestructive
  145. handler:^(UIAlertAction * _Nonnull action) {
  146. [self deleteFileAtIndex:indexPathToDelete];
  147. }];
  148. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  149. style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  150. self.editing = NO;
  151. }];
  152. [alertController addAction:deleteAction];
  153. [alertController addAction:cancelAction];
  154. [self presentViewController:alertController animated:YES completion:nil];
  155. }
  156. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  157. {
  158. if (!indexPathToDelete) {
  159. return;
  160. }
  161. __block NSString *fileToDelete = nil;
  162. [self.cachedMediaCollectionView performBatchUpdates:^{
  163. @synchronized(_discoveredFiles) {
  164. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  165. [_discoveredFiles removeObject:fileToDelete];
  166. }
  167. [self.cachedMediaCollectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];
  168. } completion:^(BOOL finished) {
  169. [[NSFileManager defaultManager] removeItemAtPath:fileToDelete error:nil];
  170. self.editing = NO;
  171. }];
  172. }
  173. - (void)startEditMode
  174. {
  175. self.editing = YES;
  176. }
  177. - (void)endEditMode
  178. {
  179. self.editing = NO;
  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. self.cancelRecognizer.enabled = editing;
  192. self.playPausePressRecognizer.enabled = editing;
  193. }
  194. #pragma mark - collection view data source
  195. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  198. return cell;
  199. }
  200. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  201. {
  202. return 1;
  203. }
  204. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  205. {
  206. NSString *cellTitle;
  207. NSUInteger row = indexPath.row;
  208. @synchronized(_discoveredFiles) {
  209. if (_discoveredFiles.count > row) {
  210. cellTitle = [_discoveredFiles[row] lastPathComponent];
  211. }
  212. }
  213. [cell prepareForReuse];
  214. [cell setIsDirectory:NO];
  215. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  216. [cell setTitle:cellTitle];
  217. }
  218. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  219. {
  220. NSUInteger ret;
  221. @synchronized(_discoveredFiles) {
  222. ret = _discoveredFiles.count;
  223. }
  224. return ret;
  225. }
  226. -(BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context
  227. {
  228. if (self.editing) {
  229. return context.nextFocusedIndexPath == nil;
  230. }
  231. return YES;
  232. }
  233. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  234. {
  235. NSIndexPath *nextPath = context.nextFocusedIndexPath;
  236. if (!nextPath) {
  237. self.editing = NO;
  238. }
  239. self.currentlyFocusedIndexPath = nextPath;
  240. }
  241. #pragma mark - collection view delegate
  242. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  243. {
  244. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  245. NSURL *url;
  246. @synchronized(_discoveredFiles) {
  247. url = [NSURL fileURLWithPath:_discoveredFiles[indexPath.row]];
  248. }
  249. [vpc playURL:url subtitlesFilePath:nil];
  250. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  251. animated:YES
  252. completion:nil];
  253. }
  254. #pragma mark - media file discovery
  255. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  256. {
  257. @synchronized(_discoveredFiles) {
  258. _discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  259. }
  260. [self.cachedMediaCollectionView reloadData];
  261. }
  262. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  263. {
  264. @synchronized(_discoveredFiles) {
  265. if (![_discoveredFiles containsObject:filePath]) {
  266. [_discoveredFiles addObject:filePath];
  267. }
  268. }
  269. [self.cachedMediaCollectionView reloadData];
  270. }
  271. - (void)mediaFileDeleted:(NSString *)filePath
  272. {
  273. @synchronized(_discoveredFiles) {
  274. [_discoveredFiles removeObject:filePath];
  275. }
  276. [self.cachedMediaCollectionView reloadData];
  277. }
  278. @end