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