VLCBoxTableViewController.m 8.7 KB

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