VLCRemotePlaybackViewController.m 14 KB

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