VLCBoxCollectionViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*****************************************************************************
  2. * VLCBoxCollectionViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCBoxCollectionViewController.h"
  14. #import "VLCBoxController.h"
  15. #import <SSKeychain/SSKeychain.h>
  16. #import "VLCPlaybackController.h"
  17. #import "VLCRemoteBrowsingTVCell+CloudStorage.h"
  18. @interface VLCBoxCollectionViewController () <VLCCloudStorageDelegate, NSURLConnectionDataDelegate>
  19. {
  20. BoxFile *_selectedFile;
  21. VLCBoxController *_boxController;
  22. NSArray *_listOfFiles;
  23. }
  24. @end
  25. @implementation VLCBoxCollectionViewController
  26. - (instancetype)initWithPath:(NSString *)path
  27. {
  28. self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  29. if (self) {
  30. self.currentPath = path;
  31. }
  32. return self;
  33. }
  34. - (void)dealloc
  35. {
  36. [[NSNotificationCenter defaultCenter] removeObserver:self];
  37. }
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. _boxController = [VLCBoxController sharedInstance];
  41. self.controller = _boxController;
  42. self.controller.delegate = self;
  43. self.title = @"Box";
  44. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  45. [defaultCenter addObserver:self
  46. selector:@selector(boxApiTokenDidRefresh)
  47. name:BoxOAuth2SessionDidRefreshTokensNotification
  48. object:[BoxSDK sharedSDK].OAuth2Session];
  49. [defaultCenter addObserver:self
  50. selector:@selector(boxApiTokenDidRefresh)
  51. name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
  52. object:[BoxSDK sharedSDK].OAuth2Session];
  53. }
  54. - (void)viewWillAppear:(BOOL)animated
  55. {
  56. [super viewWillAppear:animated];
  57. _boxController = [VLCBoxController sharedInstance];
  58. self.controller = _boxController;
  59. self.controller.delegate = self;
  60. if (!_listOfFiles || _listOfFiles.count == 0)
  61. [self requestInformationForCurrentPath];
  62. }
  63. - (void)viewWillDisappear:(BOOL)animated
  64. {
  65. [super viewWillDisappear:animated];
  66. if ([UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  67. [_boxController stopSession];
  68. [self.collectionView reloadData];
  69. }
  70. }
  71. - (void)mediaListUpdated
  72. {
  73. _listOfFiles = [[VLCBoxController sharedInstance].currentListFiles copy];
  74. [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  75. }
  76. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. VLCRemoteBrowsingTVCell *cell = (VLCRemoteBrowsingTVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  79. NSUInteger index = indexPath.row;
  80. if (_listOfFiles) {
  81. if (index < _listOfFiles.count) {
  82. cell.boxFile = _listOfFiles[index];
  83. }
  84. }
  85. return cell;
  86. }
  87. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  88. {
  89. return _listOfFiles.count;
  90. }
  91. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. if (indexPath.row >= _listOfFiles.count)
  94. return;
  95. _selectedFile = _listOfFiles[indexPath.row];
  96. if (![_selectedFile.type isEqualToString:@"folder"])
  97. [self streamFile:(BoxFile *)_selectedFile];
  98. else {
  99. /* dive into subdirectory */
  100. NSString *path = self.currentPath;
  101. if (![path isEqualToString:@""])
  102. path = [path stringByAppendingString:@"/"];
  103. path = [path stringByAppendingString:_selectedFile.modelID];
  104. VLCBoxCollectionViewController *targetViewController = [[VLCBoxCollectionViewController alloc] initWithPath:path];
  105. [self.navigationController pushViewController:targetViewController animated:YES];
  106. }
  107. }
  108. - (void)streamFile:(BoxFile *)file
  109. {
  110. /* the Box API requires us to set an HTTP header to get the actual URL:
  111. * curl -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer ACCESS_TOKEN"
  112. *
  113. * ... however, libvlc does not support setting custom HTTP headers, so we are resolving the redirect ourselves with a NSURLConnection
  114. * and pass the final location to libvlc, which does not require a custom HTTP header */
  115. NSURL *baseURL = [[[BoxSDK sharedSDK] filesManager] URLWithResource:@"files"
  116. ID:file.modelID
  117. subresource:@"content"
  118. subID:nil];
  119. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:baseURL
  120. cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
  121. timeoutInterval:60];
  122. [urlRequest setValue:[NSString stringWithFormat:@"Bearer %@", [BoxSDK sharedSDK].OAuth2Session.accessToken] forHTTPHeaderField:@"Authorization"];
  123. NSURLConnection *theTestConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
  124. [theTestConnection start];
  125. }
  126. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
  127. {
  128. if (response != nil) {
  129. /* we have 1 redirect from the original URL, so as soon as we'd do that,
  130. * we grab the URL and cancel the connection */
  131. NSURL *theActualURL = request.URL;
  132. [connection cancel];
  133. /* now ask VLC to stream the URL we were just passed */
  134. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  135. [vpc playURL:theActualURL successCallback:nil errorCallback:nil];
  136. VLCFullscreenMovieTVViewController *movieVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  137. [self presentViewController:movieVC
  138. animated:YES
  139. completion:nil];
  140. }
  141. return request;
  142. }
  143. #pragma mark - BoxAuthorizationViewControllerDelegate
  144. - (void)boxApiTokenDidRefresh
  145. {
  146. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  147. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  148. NSUbiquitousKeyValueStore *ubiquitousStore = [NSUbiquitousKeyValueStore defaultStore];
  149. [ubiquitousStore setString:token forKey:kVLCStoreBoxCredentials];
  150. [ubiquitousStore synchronize];
  151. self.authorizationInProgress = YES;
  152. [self updateViewAfterSessionChange];
  153. self.authorizationInProgress = NO;
  154. }
  155. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  156. {
  157. NSInteger currentOffset = scrollView.contentOffset.y;
  158. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  159. if (_boxController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  160. [self requestInformationForCurrentPath];
  161. }
  162. }
  163. @end