VLCBoxTableViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "VLCBoxController.h"
  14. #import "VLCAppDelegate.h"
  15. #import <SSKeychain/SSKeychain.h>
  16. @interface VLCBoxTableViewController () <VLCCloudStorageTableViewCell, BoxAuthorizationViewControllerDelegate>
  17. {
  18. BoxFile *_selectedFile;
  19. VLCBoxController *_boxController;
  20. }
  21. @end
  22. @implementation VLCBoxTableViewController
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. _boxController = (VLCBoxController *)[VLCBoxController sharedInstance];
  27. [_boxController startSession];
  28. self.controller = _boxController;
  29. self.controller.delegate = self;
  30. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BoxWhite"]];
  31. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"BoxWhite"]];
  32. [self.cloudStorageLogo sizeToFit];
  33. self.cloudStorageLogo.center = self.view.center;
  34. // Handle logged in
  35. [[NSNotificationCenter defaultCenter] addObserver:self
  36. selector:@selector(boxApiTokenDidRefresh)
  37. name:BoxOAuth2SessionDidRefreshTokensNotification
  38. object:[BoxSDK sharedSDK].OAuth2Session];
  39. [[NSNotificationCenter defaultCenter] addObserver:self
  40. selector:@selector(boxApiTokenDidRefresh)
  41. name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
  42. object:[BoxSDK sharedSDK].OAuth2Session];
  43. // Handle logout
  44. [[NSNotificationCenter defaultCenter] addObserver:self
  45. selector:@selector(boxDidGetLoggedOut)
  46. name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
  47. object:[BoxSDK sharedSDK].OAuth2Session];
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(boxDidGetLoggedOut)
  50. name:BoxOAuth2SessionDidReceiveRefreshErrorNotification
  51. object:[BoxSDK sharedSDK].OAuth2Session];
  52. [[NSNotificationCenter defaultCenter] addObserver:self
  53. selector:@selector(boxAPIAuthenticationDidFail)
  54. name:BoxOAuth2SessionDidReceiveAuthenticationErrorNotification
  55. object:[BoxSDK sharedSDK].OAuth2Session];
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(boxAPIInitiateLogin)
  58. name:BoxOAuth2SessionDidReceiveRefreshErrorNotification
  59. object:[BoxSDK sharedSDK].OAuth2Session];
  60. }
  61. - (UIViewController *)createAuthController
  62. {
  63. NSURL *authorizationURL = [[BoxSDK sharedSDK].OAuth2Session authorizeURL];
  64. BoxAuthorizationViewController *authorizationController = [[BoxAuthorizationViewController alloc] initWithAuthorizationURL:authorizationURL redirectURI:kVLCBoxRedirectURL];
  65. authorizationController.delegate = self;
  66. return authorizationController;
  67. }
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. self.currentPath = @"";
  72. if([_boxController.currentListFiles count] == 0)
  73. [self _requestInformationForCurrentPath];
  74. }
  75. - (void)viewWillDisappear:(BOOL)animated
  76. {
  77. [super viewWillDisappear:animated];
  78. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  79. [_boxController stopSession];
  80. [self.tableView reloadData];
  81. }
  82. }
  83. #pragma mark - Table view data source
  84. - (VLCCloudStorageTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. static NSString *CellIdentifier = @"BoxCell";
  87. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  88. if (cell == nil)
  89. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  90. cell.boxFile = _boxController.currentListFiles[indexPath.row];
  91. cell.delegate = self;
  92. return cell;
  93. }
  94. #pragma mark - Table view delegate
  95. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. _selectedFile = _boxController.currentListFiles[indexPath.row];
  98. if (![_selectedFile.type isEqualToString:@"folder"]) {
  99. //[_boxController streamFile:(BoxFile *)_selectedFile];
  100. [_boxController downloadFileToDocumentFolder:_selectedFile];
  101. } else {
  102. /* dive into subdirectory */
  103. if (![self.currentPath isEqualToString:@""])
  104. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  105. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.modelID];
  106. [self _requestInformationForCurrentPath];
  107. }
  108. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  109. }
  110. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  111. {
  112. _selectedFile = _boxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  113. /* selected item is a proper file, ask the user if s/he wants to download it */
  114. 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];
  115. [alert show];
  116. }
  117. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  118. {
  119. if (buttonIndex == 1)
  120. [_boxController downloadFileToDocumentFolder:_selectedFile];
  121. _selectedFile = nil;
  122. }
  123. #pragma mark - box controller delegate
  124. #pragma mark - BoxAuthorizationViewControllerDelegate
  125. - (BOOL)authorizationViewController:(BoxAuthorizationViewController *)authorizationViewController shouldLoadReceivedOAuth2RedirectRequest:(NSURLRequest *)request
  126. {
  127. [[BoxSDK sharedSDK].OAuth2Session performAuthorizationCodeGrantWithReceivedURL:request.URL];
  128. [self.navigationController popViewControllerAnimated:YES];
  129. return NO;
  130. }
  131. - (void)authorizationViewControllerDidStartLoading:(BoxAuthorizationViewController *)authorizationViewController
  132. {
  133. //needs to be implemented
  134. }
  135. - (void)authorizationViewControllerDidFinishLoading:(BoxAuthorizationViewController *)authorizationViewController
  136. {
  137. //needs to be implemented
  138. }
  139. - (void)boxDidGetLoggedOut
  140. {
  141. [self _showLoginPanel];
  142. }
  143. - (void)boxApiTokenDidRefresh
  144. {
  145. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  146. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  147. self.authorizationInProgress = YES;
  148. [self updateViewAfterSessionChange];
  149. self.authorizationInProgress = NO;
  150. }
  151. - (void)boxAPIAuthenticationDidFail
  152. {
  153. //needs to be implemented
  154. }
  155. - (void)boxAPIInitiateLogin
  156. {
  157. [self _showLoginPanel];
  158. }
  159. - (void)authorizationViewControllerDidCancel:(BoxAuthorizationViewController *)authorizationViewController
  160. {
  161. [self.navigationController popViewControllerAnimated:YES];
  162. }
  163. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  164. {
  165. NSInteger currentOffset = scrollView.contentOffset.y;
  166. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  167. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  168. if (_boxController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  169. [self _requestInformationForCurrentPath];
  170. }
  171. }
  172. }
  173. #pragma mark - login dialog
  174. - (IBAction)loginAction:(id)sender
  175. {
  176. if (![_boxController isAuthorized]) {
  177. self.authorizationInProgress = YES;
  178. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  179. } else {
  180. [_boxController logout];
  181. }
  182. }
  183. @end