VLCBoxTableViewController.m 9.1 KB

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