VLCBoxTableViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. else {
  101. /* dive into subdirectory */
  102. if (![self.currentPath isEqualToString:@""])
  103. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  104. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.modelID];
  105. [self _requestInformationForCurrentPath];
  106. }
  107. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  108. }
  109. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  110. {
  111. _selectedFile = _boxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  112. /* selected item is a proper file, ask the user if s/he wants to download it */
  113. 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];
  114. [alert show];
  115. }
  116. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  117. {
  118. if (buttonIndex == 1)
  119. [_boxController downloadFileToDocumentFolder:_selectedFile];
  120. _selectedFile = nil;
  121. }
  122. #pragma mark - box controller delegate
  123. #pragma mark - BoxAuthorizationViewControllerDelegate
  124. - (BOOL)authorizationViewController:(BoxAuthorizationViewController *)authorizationViewController shouldLoadReceivedOAuth2RedirectRequest:(NSURLRequest *)request
  125. {
  126. [[BoxSDK sharedSDK].OAuth2Session performAuthorizationCodeGrantWithReceivedURL:request.URL];
  127. [self.navigationController popViewControllerAnimated:YES];
  128. return NO;
  129. }
  130. - (void)authorizationViewControllerDidStartLoading:(BoxAuthorizationViewController *)authorizationViewController
  131. {
  132. //needs to be implemented
  133. }
  134. - (void)authorizationViewControllerDidFinishLoading:(BoxAuthorizationViewController *)authorizationViewController
  135. {
  136. //needs to be implemented
  137. }
  138. - (void)boxDidGetLoggedOut
  139. {
  140. [self _showLoginPanel];
  141. }
  142. - (void)boxApiTokenDidRefresh
  143. {
  144. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  145. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  146. self.authorizationInProgress = YES;
  147. [self updateViewAfterSessionChange];
  148. self.authorizationInProgress = NO;
  149. }
  150. - (void)boxAPIAuthenticationDidFail
  151. {
  152. //needs to be implemented
  153. }
  154. - (void)boxAPIInitiateLogin
  155. {
  156. [self _showLoginPanel];
  157. }
  158. - (void)authorizationViewControllerDidCancel:(BoxAuthorizationViewController *)authorizationViewController
  159. {
  160. [self.navigationController popViewControllerAnimated:YES];
  161. }
  162. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  163. {
  164. NSInteger currentOffset = scrollView.contentOffset.y;
  165. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  166. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  167. if (_boxController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  168. [self _requestInformationForCurrentPath];
  169. }
  170. }
  171. }
  172. #pragma mark - login dialog
  173. - (IBAction)loginAction:(id)sender
  174. {
  175. if (![_boxController isAuthorized]) {
  176. self.authorizationInProgress = YES;
  177. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  178. } else {
  179. [_boxController logout];
  180. }
  181. }
  182. @end