VLCRemotePlaybackViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. @property (nonatomic) NSTimer *hintTimer;
  30. @end
  31. @implementation VLCRemotePlaybackViewController
  32. - (NSString *)title
  33. {
  34. return NSLocalizedString(@"WEBINTF_TITLE_ATV", nil);
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.cachedMediaCollectionView.collectionViewLayout;
  40. const CGFloat inset = 50.;
  41. flowLayout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
  42. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  43. flowLayout.minimumInteritemSpacing = 48.0;
  44. flowLayout.minimumLineSpacing = 100.0;
  45. [self.cachedMediaCollectionView registerNib:[UINib nibWithNibName:@"VLCRemoteBrowsingTVCell" bundle:nil]
  46. forCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier];
  47. _reachability = [Reachability reachabilityForLocalWiFi];
  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. self.cachedMediaLabel.text = NSLocalizedString(@"CACHED_MEDIA", nil);
  73. self.cachedMediaLongLabel.text = NSLocalizedString(@"CACHED_MEDIA_LONG", nil);
  74. }
  75. - (void)viewDidLayoutSubviews
  76. {
  77. [super viewDidLayoutSubviews];
  78. UICollectionView *collectionView = self.cachedMediaCollectionView;
  79. VLCMaskView *maskView = (VLCMaskView *)collectionView.maskView;
  80. maskView.maskEnd = self.topLayoutGuide.length * 0.8;
  81. /*
  82. Update the position from where the collection view's content should
  83. start to fade out. The size of the fade increases as the collection
  84. view scrolls to a maximum of half the navigation bar's height.
  85. */
  86. CGFloat maximumMaskStart = maskView.maskEnd + (self.topLayoutGuide.length * 0.5);
  87. CGFloat verticalScrollPosition = MAX(0, collectionView.contentOffset.y + collectionView.contentInset.top);
  88. maskView.maskStart = MIN(maximumMaskStart, maskView.maskEnd + verticalScrollPosition);
  89. /*
  90. Position the mask view so that it is always fills the visible area of
  91. the collection view.
  92. */
  93. CGSize collectionViewSize = collectionView.bounds.size;
  94. maskView.frame = CGRectMake(0, collectionView.contentOffset.y, collectionViewSize.width, collectionViewSize.height);
  95. }
  96. - (void)viewWillAppear:(BOOL)animated
  97. {
  98. [super viewWillAppear:animated];
  99. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  100. [_reachability startNotifier];
  101. [self updateHTTPServerAddress];
  102. }
  103. - (void)viewWillDisappear:(BOOL)animated
  104. {
  105. [super viewWillDisappear:animated];
  106. [_reachability stopNotifier];
  107. }
  108. - (void)reachabilityChanged
  109. {
  110. [self updateHTTPServerAddress];
  111. }
  112. - (void)updateHTTPServerAddress
  113. {
  114. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  115. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  116. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  117. self.httpServerLabel.text = uploadText;
  118. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  119. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  120. else
  121. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  122. }
  123. - (void)toggleHTTPServer:(id)sender
  124. {
  125. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  126. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  127. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  128. [self updateHTTPServerAddress];
  129. [[NSUserDefaults standardUserDefaults] synchronize];
  130. }
  131. #pragma mark - editing
  132. - (void)handlePlayPausePress
  133. {
  134. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  135. if (!indexPathToDelete) {
  136. return;
  137. }
  138. NSString *fileToDelete = nil;
  139. @synchronized(_discoveredFiles) {
  140. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  141. }
  142. NSString *title = fileToDelete.lastPathComponent;
  143. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
  144. message:nil
  145. preferredStyle:UIAlertControllerStyleAlert];
  146. UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  147. style:UIAlertActionStyleDestructive
  148. handler:^(UIAlertAction * _Nonnull action) {
  149. [self deleteFileAtIndex:indexPathToDelete];
  150. }];
  151. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  152. style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  153. self.editing = NO;
  154. }];
  155. [alertController addAction:deleteAction];
  156. [alertController addAction:cancelAction];
  157. [self presentViewController:alertController animated:YES completion:nil];
  158. }
  159. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  160. {
  161. if (!indexPathToDelete) {
  162. return;
  163. }
  164. __block NSString *fileToDelete = nil;
  165. [self.cachedMediaCollectionView performBatchUpdates:^{
  166. @synchronized(_discoveredFiles) {
  167. fileToDelete = _discoveredFiles[indexPathToDelete.item];
  168. [_discoveredFiles removeObject:fileToDelete];
  169. }
  170. [self.cachedMediaCollectionView deleteItemsAtIndexPaths:@[indexPathToDelete]];
  171. } completion:^(BOOL finished) {
  172. [[NSFileManager defaultManager] removeItemAtPath:fileToDelete error:nil];
  173. self.editing = NO;
  174. }];
  175. }
  176. - (void)animateDeletHintToVisibility:(BOOL)visible
  177. {
  178. const NSTimeInterval duration = 0.5;
  179. UIView *hintView = self.deleteHintView;
  180. if (hintView.hidden) {
  181. hintView.alpha = 0.0;
  182. }
  183. if (hintView.alpha == 0.0) {
  184. hintView.hidden = NO;
  185. }
  186. const CGFloat targetAlpha = visible ? 1.0 : 0.0;
  187. [UIView animateWithDuration:duration
  188. delay:0
  189. options:UIViewAnimationOptionBeginFromCurrentState
  190. animations:^{
  191. hintView.alpha = targetAlpha;
  192. }
  193. completion:^(BOOL finished) {
  194. if (hintView.alpha == 0.0) {
  195. hintView.hidden = YES;
  196. }
  197. }];
  198. }
  199. - (void)hintTimerFired:(NSTimer *)timer
  200. {
  201. const NSTimeInterval waitUntilHideInterval = 5.0;
  202. NSNumber *userInfo = [timer userInfo];
  203. BOOL shouldShow = [userInfo isKindOfClass:[NSNumber class]] && [userInfo boolValue];
  204. [self animateDeletHintToVisibility:shouldShow];
  205. if (shouldShow) {
  206. [self.hintTimer invalidate];
  207. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:waitUntilHideInterval target:self selector:@selector(hintTimerFired:) userInfo:@(NO) repeats:NO];
  208. }
  209. }
  210. - (void)startEditMode
  211. {
  212. self.editing = YES;
  213. }
  214. - (void)endEditMode
  215. {
  216. self.editing = NO;
  217. }
  218. - (void)setEditing:(BOOL)editing
  219. {
  220. [super setEditing:editing];
  221. UICollectionViewCell *focusedCell = [self.cachedMediaCollectionView cellForItemAtIndexPath:self.currentlyFocusedIndexPath];
  222. if (editing) {
  223. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimation]
  224. forKey:VLCWiggleAnimationKey];
  225. [self.hintTimer invalidate];
  226. self.hintTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(hintTimerFired:) userInfo:@(YES) repeats:NO];
  227. } else {
  228. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  229. [self.hintTimer invalidate];
  230. self.hintTimer = nil;
  231. [self animateDeletHintToVisibility:NO];
  232. }
  233. self.cancelRecognizer.enabled = editing;
  234. self.playPausePressRecognizer.enabled = editing;
  235. }
  236. #pragma mark - collection view data source
  237. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  240. return cell;
  241. }
  242. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  243. {
  244. return 1;
  245. }
  246. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(VLCRemoteBrowsingTVCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  247. {
  248. NSString *cellTitle;
  249. NSUInteger row = indexPath.row;
  250. @synchronized(_discoveredFiles) {
  251. if (_discoveredFiles.count > row) {
  252. cellTitle = [_discoveredFiles[row] lastPathComponent];
  253. }
  254. }
  255. [cell prepareForReuse];
  256. [cell setIsDirectory:NO];
  257. [cell setThumbnailImage:[UIImage imageNamed:@"blank"]];
  258. [cell setTitle:cellTitle];
  259. }
  260. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  261. {
  262. NSUInteger ret;
  263. @synchronized(_discoveredFiles) {
  264. ret = _discoveredFiles.count;
  265. }
  266. return ret;
  267. }
  268. -(BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context
  269. {
  270. if (self.editing) {
  271. return context.nextFocusedIndexPath == nil;
  272. }
  273. return YES;
  274. }
  275. - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
  276. {
  277. NSIndexPath *nextPath = context.nextFocusedIndexPath;
  278. if (!nextPath) {
  279. self.editing = NO;
  280. }
  281. self.currentlyFocusedIndexPath = nextPath;
  282. }
  283. #pragma mark - collection view delegate
  284. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  285. {
  286. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  287. NSURL *url;
  288. @synchronized(_discoveredFiles) {
  289. url = [NSURL fileURLWithPath:_discoveredFiles[indexPath.row]];
  290. }
  291. [vpc playURL:url subtitlesFilePath:nil];
  292. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  293. animated:YES
  294. completion:nil];
  295. }
  296. #pragma mark - media file discovery
  297. - (void)mediaFilesFoundRequiringAdditionToStorageBackend:(NSArray<NSString *> *)foundFiles
  298. {
  299. @synchronized(_discoveredFiles) {
  300. _discoveredFiles = [NSMutableArray arrayWithArray:foundFiles];
  301. }
  302. [self.cachedMediaCollectionView reloadData];
  303. }
  304. - (void)mediaFileAdded:(NSString *)filePath loading:(BOOL)isLoading
  305. {
  306. @synchronized(_discoveredFiles) {
  307. if (![_discoveredFiles containsObject:filePath]) {
  308. [_discoveredFiles addObject:filePath];
  309. }
  310. }
  311. [self.cachedMediaCollectionView reloadData];
  312. }
  313. - (void)mediaFileDeleted:(NSString *)filePath
  314. {
  315. @synchronized(_discoveredFiles) {
  316. [_discoveredFiles removeObject:filePath];
  317. }
  318. [self.cachedMediaCollectionView reloadData];
  319. }
  320. @end