VLCBoxTableViewController.m 8.9 KB

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