VLCBoxTableViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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+VLC.h"
  17. @interface VLCBoxTableViewController () <VLCCloudStorageTableViewCell, BoxAuthorizationViewControllerDelegate, VLCCloudStorageDelegate>
  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. NSString *redirectURLString = [[BoxSDK sharedSDK].OAuth2Session redirectURIString];
  66. BoxAuthorizationViewController *authorizationController = [[BoxAuthorizationViewController alloc] initWithAuthorizationURL:authorizationURL redirectURI:redirectURLString];
  67. authorizationController.delegate = self;
  68. return authorizationController;
  69. }
  70. - (void)viewWillAppear:(BOOL)animated
  71. {
  72. [super viewWillAppear:animated];
  73. self.currentPath = @"";
  74. if([_boxController.currentListFiles count] == 0)
  75. [self _requestInformationForCurrentPath];
  76. }
  77. - (void)viewWillDisappear:(BOOL)animated
  78. {
  79. [super viewWillDisappear:animated];
  80. if ((VLCAppDelegate*)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  81. [_boxController stopSession];
  82. [self.tableView reloadData];
  83. }
  84. }
  85. #pragma mark - Table view data source
  86. - (VLCCloudStorageTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. static NSString *CellIdentifier = @"BoxCell";
  89. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  90. if (cell == nil)
  91. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  92. cell.boxFile = _boxController.currentListFiles[indexPath.row];
  93. cell.delegate = self;
  94. return cell;
  95. }
  96. #pragma mark - Table view delegate
  97. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. _selectedFile = _boxController.currentListFiles[indexPath.row];
  100. if (![_selectedFile.type isEqualToString:@"folder"])
  101. [_boxController streamFile:(BoxFile *)_selectedFile];
  102. else {
  103. /* dive into subdirectory */
  104. if (![self.currentPath isEqualToString:@""])
  105. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  106. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.modelID];
  107. [self _requestInformationForCurrentPath];
  108. }
  109. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  110. }
  111. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  112. {
  113. _selectedFile = _boxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  114. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  115. /* selected item is a proper file, ask the user if s/he wants to download it */
  116. 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];
  117. [alert show];
  118. } else {
  119. 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];
  120. [alert show];
  121. }
  122. }
  123. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  124. {
  125. if (buttonIndex == 1)
  126. [_boxController downloadFileToDocumentFolder:_selectedFile];
  127. _selectedFile = nil;
  128. }
  129. #pragma mark - box controller delegate
  130. #pragma mark - BoxAuthorizationViewControllerDelegate
  131. - (BOOL)authorizationViewController:(BoxAuthorizationViewController *)authorizationViewController shouldLoadReceivedOAuth2RedirectRequest:(NSURLRequest *)request
  132. {
  133. [[BoxSDK sharedSDK].OAuth2Session performAuthorizationCodeGrantWithReceivedURL:request.URL];
  134. [self.navigationController popViewControllerAnimated:YES];
  135. return NO;
  136. }
  137. - (void)authorizationViewControllerDidStartLoading:(BoxAuthorizationViewController *)authorizationViewController
  138. {
  139. //needs to be implemented
  140. }
  141. - (void)authorizationViewControllerDidFinishLoading:(BoxAuthorizationViewController *)authorizationViewController
  142. {
  143. //needs to be implemented
  144. }
  145. - (void)boxDidGetLoggedOut
  146. {
  147. [self _showLoginPanel];
  148. }
  149. - (void)boxApiTokenDidRefresh
  150. {
  151. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  152. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  153. self.authorizationInProgress = YES;
  154. [self updateViewAfterSessionChange];
  155. self.authorizationInProgress = NO;
  156. }
  157. - (void)boxAPIAuthenticationDidFail
  158. {
  159. //needs to be implemented
  160. }
  161. - (void)boxAPIInitiateLogin
  162. {
  163. [self _showLoginPanel];
  164. }
  165. - (void)authorizationViewControllerDidCancel:(BoxAuthorizationViewController *)authorizationViewController
  166. {
  167. [self.navigationController popViewControllerAnimated:YES];
  168. }
  169. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  170. {
  171. NSInteger currentOffset = scrollView.contentOffset.y;
  172. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  173. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  174. if (_boxController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  175. [self _requestInformationForCurrentPath];
  176. }
  177. }
  178. }
  179. #pragma mark - login dialog
  180. - (IBAction)loginAction:(id)sender
  181. {
  182. if (![_boxController isAuthorized]) {
  183. self.authorizationInProgress = YES;
  184. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  185. } else {
  186. [_boxController logout];
  187. }
  188. }
  189. @end