VLCBoxTableViewController.m 9.0 KB

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