VLCRemotePlaybackViewController.m 12 KB

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