VLCBoxTableViewController.m 9.1 KB

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