VLCBoxTableViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*****************************************************************************
  2. * VLCBoxTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCBoxTableViewController.h"
  13. #import "VLCCloudStorageTableViewCell.h"
  14. #import "VLCBoxController.h"
  15. #import "VLCAppDelegate.h"
  16. #import "VLCProgressView.h"
  17. #import "UIBarButtonItem+Theme.h"
  18. #import <SSKeychain/SSKeychain.h>
  19. @interface VLCBoxTableViewController () <VLCCloudStorageTableViewCell, VLCBoxController, BoxAuthorizationViewControllerDelegate>
  20. {
  21. BoxFile *_selectedFile;
  22. NSString *_currentFolderId;
  23. UIBarButtonItem *_numberOfFilesBarButtonItem;
  24. UIBarButtonItem *_logoutButton;
  25. UIBarButtonItem *_progressBarButtonItem;
  26. VLCProgressView *_progressView;
  27. UIActivityIndicatorView *_activityIndicator;
  28. BOOL _authorizationInProgress;
  29. VLCBoxController *_boxController;
  30. }
  31. @end
  32. @implementation VLCBoxTableViewController
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. self.modalPresentationStyle = UIModalPresentationFormSheet;
  37. _boxController = [VLCBoxController sharedInstance];
  38. _boxController.delegate = self;
  39. [_boxController startSession];
  40. _authorizationInProgress = NO;
  41. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BoxWhite"]];
  42. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  43. UIBarButtonItem *backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  44. self.navigationItem.leftBarButtonItem = backButton;
  45. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  46. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  47. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  48. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  49. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  50. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  51. _progressView = [VLCProgressView new];
  52. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  53. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"BoxWhite"]];
  54. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  55. self.flatLoginButton.hidden = YES;
  56. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  57. } else {
  58. self.loginButton.hidden = YES;
  59. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  60. }
  61. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  62. [self _showProgressInToolbar:NO];
  63. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  64. _activityIndicator.hidesWhenStopped = YES;
  65. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.view addSubview:_activityIndicator];
  67. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  68. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  69. [self.cloudStorageLogo sizeToFit];
  70. self.cloudStorageLogo.center = self.view.center;
  71. // Handle logged in
  72. [[NSNotificationCenter defaultCenter] addObserver:self
  73. selector:@selector(boxApiTokenDidRefresh)
  74. name:BoxOAuth2SessionDidRefreshTokensNotification
  75. object:[BoxSDK sharedSDK].OAuth2Session];
  76. [[NSNotificationCenter defaultCenter] addObserver:self
  77. selector:@selector(boxApiTokenDidRefresh)
  78. name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
  79. object:[BoxSDK sharedSDK].OAuth2Session];
  80. // Handle logout
  81. [[NSNotificationCenter defaultCenter] addObserver:self
  82. selector:@selector(boxDidGetLoggedOut)
  83. name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
  84. object:[BoxSDK sharedSDK].OAuth2Session];
  85. [[NSNotificationCenter defaultCenter] addObserver:self
  86. selector:@selector(boxDidGetLoggedOut)
  87. name:BoxOAuth2SessionDidReceiveRefreshErrorNotification
  88. object:[BoxSDK sharedSDK].OAuth2Session];
  89. [[NSNotificationCenter defaultCenter] addObserver:self
  90. selector:@selector(boxAPIAuthenticationDidFail)
  91. name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
  92. object:[BoxSDK sharedSDK].OAuth2Session];
  93. [[NSNotificationCenter defaultCenter] addObserver:self
  94. selector:@selector(boxAPIInitiateLogin)
  95. name:BoxOAuth2SessionDidReceiveRefreshErrorNotification
  96. object:[BoxSDK sharedSDK].OAuth2Session];
  97. }
  98. - (UIViewController *)createAuthController
  99. {
  100. NSURL *authorizationURL = [[BoxSDK sharedSDK].OAuth2Session authorizeURL];
  101. BoxAuthorizationViewController *authorizationController = [[BoxAuthorizationViewController alloc] initWithAuthorizationURL:authorizationURL redirectURI:kVLCBoxRedirectURL];
  102. authorizationController.delegate = self;
  103. return authorizationController;
  104. }
  105. - (void)viewWillAppear:(BOOL)animated
  106. {
  107. self.navigationController.toolbarHidden = NO;
  108. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  109. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  110. _currentFolderId = @"";
  111. if([_boxController.currentListFiles count] == 0)
  112. [self _requestInformationForCurrentFolderId];
  113. [super viewWillAppear:animated];
  114. }
  115. - (void)viewWillDisappear:(BOOL)animated
  116. {
  117. self.navigationController.toolbarHidden = YES;
  118. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  119. [_boxController stopSession];
  120. [self.tableView reloadData];
  121. }
  122. [super viewWillDisappear:animated];
  123. }
  124. - (void)_showProgressInToolbar:(BOOL)value
  125. {
  126. if (!value)
  127. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  128. else {
  129. _progressView.progressBar.progress = 0.;
  130. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  131. }
  132. }
  133. - (void)_requestInformationForCurrentFolderId
  134. {
  135. [_activityIndicator startAnimating];
  136. [_boxController requestDirectoryListingWithFolderId:_currentFolderId];
  137. }
  138. - (IBAction)goBack:(id)sender
  139. {
  140. if (![_currentFolderId isEqualToString:@""] && [_currentFolderId length] > 0) {
  141. _currentFolderId = [_currentFolderId stringByDeletingLastPathComponent];
  142. [self _requestInformationForCurrentFolderId];
  143. } else
  144. [self.navigationController popViewControllerAnimated:YES];
  145. }
  146. #pragma mark - Table view data source
  147. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  148. {
  149. return _boxController.currentListFiles.count;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. static NSString *CellIdentifier = @"BoxCell";
  154. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  155. if (cell == nil)
  156. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  157. cell.boxFile = _boxController.currentListFiles[indexPath.row];
  158. cell.delegate = self;
  159. return cell;
  160. }
  161. #pragma mark - Table view delegate
  162. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  165. }
  166. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. _selectedFile = _boxController.currentListFiles[indexPath.row];
  169. if (![_selectedFile.type isEqualToString:@"folder"]) {
  170. //[_boxController streamFile:(BoxFile *)_selectedFile];
  171. [_boxController downloadFileToDocumentFolder:_selectedFile];
  172. } else {
  173. /* dive into subdirectory */
  174. if (![_currentFolderId isEqualToString:@""])
  175. _currentFolderId = [_currentFolderId stringByAppendingString:@"/"];
  176. _currentFolderId = [_currentFolderId stringByAppendingString:_selectedFile.modelID];
  177. [self _requestInformationForCurrentFolderId];
  178. }
  179. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  180. }
  181. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  182. {
  183. _selectedFile = _boxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  184. /* selected item is a proper file, ask the user if s/he wants to download it */
  185. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  186. [alert show];
  187. }
  188. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  189. {
  190. NSInteger currentOffset = scrollView.contentOffset.y;
  191. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  192. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  193. if (_boxController.hasMoreFiles && !_activityIndicator.isAnimating) {
  194. [self _requestInformationForCurrentFolderId];
  195. }
  196. }
  197. }
  198. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  199. {
  200. if (buttonIndex == 1)
  201. [_boxController downloadFileToDocumentFolder:_selectedFile];
  202. _selectedFile = nil;
  203. }
  204. #pragma mark - google drive controller delegate
  205. - (void)mediaListUpdated
  206. {
  207. [_activityIndicator stopAnimating];
  208. [self.tableView reloadData];
  209. NSUInteger count = _boxController.currentListFiles.count;
  210. if (count == 0)
  211. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  212. else if (count != 1)
  213. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  214. else
  215. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  216. }
  217. - (void)operationWithProgressInformationStarted
  218. {
  219. [self _showProgressInToolbar:YES];
  220. }
  221. - (void)updateRemainingTime:(NSString *)time
  222. {
  223. [_progressView updateTime:time];
  224. }
  225. - (void)currentProgressInformation:(CGFloat)progress {
  226. [_progressView.progressBar setProgress:progress animated:YES];
  227. }
  228. - (void)operationWithProgressInformationStopped
  229. {
  230. [self _showProgressInToolbar:NO];
  231. }
  232. #pragma mark - BoxAuthorizationViewControllerDelegate
  233. - (BOOL)authorizationViewController:(BoxAuthorizationViewController *)authorizationViewController shouldLoadReceivedOAuth2RedirectRequest:(NSURLRequest *)request
  234. {
  235. [[BoxSDK sharedSDK].OAuth2Session performAuthorizationCodeGrantWithReceivedURL:request.URL];
  236. [self.navigationController popViewControllerAnimated:YES];
  237. return NO;
  238. }
  239. - (void)authorizationViewControllerDidStartLoading:(BoxAuthorizationViewController *)authorizationViewController
  240. {
  241. //needs to be implemented
  242. }
  243. - (void)authorizationViewControllerDidFinishLoading:(BoxAuthorizationViewController *)authorizationViewController
  244. {
  245. //needs to be implemented
  246. }
  247. - (void)boxDidGetLoggedOut
  248. {
  249. [self _showLoginPanel];
  250. }
  251. - (void)boxApiTokenDidRefresh
  252. {
  253. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  254. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  255. _authorizationInProgress = YES;
  256. [self updateViewAfterSessionChange];
  257. _authorizationInProgress = NO;
  258. }
  259. - (void)boxAPIAuthenticationDidFail
  260. {
  261. //needs to be implemented
  262. }
  263. - (void)boxAPIInitiateLogin
  264. {
  265. [self _showLoginPanel];
  266. }
  267. - (void)authorizationViewControllerDidCancel:(BoxAuthorizationViewController *)authorizationViewController
  268. {
  269. [self.navigationController popViewControllerAnimated:YES];
  270. }
  271. - (void)updateViewAfterSessionChange
  272. {
  273. self.navigationItem.rightBarButtonItem = _logoutButton;
  274. if(_authorizationInProgress) {
  275. if (self.loginToCloudStorageView.superview) {
  276. [self.loginToCloudStorageView removeFromSuperview];
  277. }
  278. return;
  279. }
  280. if (![_boxController isAuthorized]) {
  281. [self _showLoginPanel];
  282. return;
  283. }
  284. //reload if we didn't come back from streaming
  285. _currentFolderId = @"";
  286. if([_boxController.currentListFiles count] == 0)
  287. [self _requestInformationForCurrentFolderId];
  288. }
  289. #pragma mark - login dialog
  290. - (void)_showLoginPanel
  291. {
  292. self.loginToCloudStorageView.frame = self.tableView.frame;
  293. self.navigationItem.rightBarButtonItem = nil;
  294. [self.view addSubview:self.loginToCloudStorageView];
  295. }
  296. - (IBAction)loginAction:(id)sender
  297. {
  298. if (![_boxController isAuthorized]) {
  299. _authorizationInProgress = YES;
  300. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  301. } else {
  302. [_boxController logout];
  303. }
  304. }
  305. - (void)logout
  306. {
  307. [_boxController logout];
  308. [self updateViewAfterSessionChange];
  309. }
  310. @end