VLCDropboxTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*****************************************************************************
  2. * VLCDropboxTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. * Fabio Ritrovato <sephiroth87 # videolan.org>
  12. * Tamas Timar <ttimar.vlc # gmail.com>
  13. *
  14. * Refer to the COPYING file of the official project for license.
  15. *****************************************************************************/
  16. #import "VLCDropboxTableViewController.h"
  17. #import "VLCCloudStorageTableViewCell.h"
  18. #import "VLCDropboxController.h"
  19. #import "VLCAppDelegate.h"
  20. #import "VLCPlaylistViewController.h"
  21. #import "VLCDropboxConstants.h"
  22. #import "UIBarButtonItem+Theme.h"
  23. #import <DropboxSDK/DropboxSDK.h>
  24. #import "VLCProgressView.h"
  25. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCDropboxController>
  26. {
  27. VLCDropboxController *_dropboxController;
  28. NSString *_currentPath;
  29. UIBarButtonItem *_numberOfFilesBarButtonItem;
  30. UIBarButtonItem *_progressBarButtonItem;
  31. UIBarButtonItem *_logoutButton;
  32. VLCProgressView *_progressView;
  33. UIActivityIndicatorView *_activityIndicator;
  34. DBMetadata *_selectedFile;
  35. }
  36. @end
  37. @implementation VLCDropboxTableViewController
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. self.modalPresentationStyle = UIModalPresentationFormSheet;
  42. _dropboxController = [[VLCDropboxController alloc] init];
  43. _dropboxController.delegate = self;
  44. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  45. [DBSession setSharedSession:dbSession];
  46. [DBRequest setNetworkRequestDelegate:_dropboxController];
  47. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  48. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  49. UIBarButtonItem *backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  50. self.navigationItem.leftBarButtonItem = backButton;
  51. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  52. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  53. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  54. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  55. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  56. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  57. _progressView = [VLCProgressView new];
  58. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  59. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  60. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  61. self.flatLoginButton.hidden = YES;
  62. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  63. } else {
  64. self.loginButton.hidden = YES;
  65. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  66. }
  67. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  68. [self _showProgressInToolbar:NO];
  69. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  70. _activityIndicator.hidesWhenStopped = YES;
  71. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  72. [self.view addSubview:_activityIndicator];
  73. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  74. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  75. [self.cloudStorageLogo sizeToFit];
  76. self.cloudStorageLogo.center = self.view.center;
  77. }
  78. - (void)viewWillAppear:(BOOL)animated
  79. {
  80. self.navigationController.toolbarHidden = NO;
  81. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  82. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  83. [self updateViewAfterSessionChange];
  84. [super viewWillAppear:animated];
  85. }
  86. - (void)viewWillDisappear:(BOOL)animated
  87. {
  88. self.navigationController.toolbarHidden = YES;
  89. [super viewWillDisappear:animated];
  90. }
  91. - (void)_showProgressInToolbar:(BOOL)value
  92. {
  93. if (!value)
  94. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  95. else {
  96. _progressView.progressBar.progress = 0.;
  97. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  98. }
  99. }
  100. - (void)_requestInformationForCurrentPath
  101. {
  102. [_activityIndicator startAnimating];
  103. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  104. }
  105. #pragma mark - interface interaction
  106. - (BOOL)shouldAutorotate
  107. {
  108. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  109. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  110. return NO;
  111. return YES;
  112. }
  113. - (IBAction)goBack:(id)sender
  114. {
  115. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  116. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  117. [self _requestInformationForCurrentPath];
  118. } else
  119. [self.navigationController popViewControllerAnimated:YES];
  120. }
  121. #pragma mark - Table view data source
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  123. {
  124. return _dropboxController.currentListFiles.count;
  125. }
  126. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. static NSString *CellIdentifier = @"DropboxCell";
  129. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  130. if (cell == nil)
  131. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  132. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  133. cell.delegate = self;
  134. return cell;
  135. }
  136. #pragma mark - Table view delegate
  137. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  140. }
  141. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  144. if (!_selectedFile.isDirectory)
  145. [_dropboxController streamFile:_selectedFile];
  146. else {
  147. /* dive into subdirectory */
  148. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  149. [self _requestInformationForCurrentPath];
  150. }
  151. _selectedFile = nil;
  152. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  153. }
  154. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  155. {
  156. if (buttonIndex == 1)
  157. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  158. _selectedFile = nil;
  159. }
  160. #pragma mark - dropbox controller delegate
  161. - (void)mediaListUpdated
  162. {
  163. [_activityIndicator stopAnimating];
  164. [self.tableView reloadData];
  165. NSUInteger count = _dropboxController.currentListFiles.count;
  166. if (count == 0)
  167. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  168. else if (count != 1)
  169. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  170. else
  171. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  172. }
  173. - (void)operationWithProgressInformationStarted
  174. {
  175. [self _showProgressInToolbar:YES];
  176. }
  177. - (void)updateRemainingTime:(NSString *)time
  178. {
  179. [_progressView updateTime:time];
  180. }
  181. - (void)currentProgressInformation:(float)progress
  182. {
  183. [_progressView.progressBar setProgress:progress animated:YES];
  184. }
  185. - (void)operationWithProgressInformationStopped
  186. {
  187. [self _showProgressInToolbar:NO];
  188. }
  189. #pragma mark - communication with app delegate
  190. - (void)updateViewAfterSessionChange
  191. {
  192. self.navigationItem.rightBarButtonItem = _logoutButton;
  193. if (![[DBSession sharedSession] isLinked]) {
  194. [self _showLoginPanel];
  195. return;
  196. } else if (self.loginToCloudStorageView.superview) {
  197. [self.loginToCloudStorageView removeFromSuperview];
  198. }
  199. _currentPath = @"/";
  200. if([_dropboxController.currentListFiles count] == 0)
  201. [self _requestInformationForCurrentPath];
  202. }
  203. #pragma mark - login dialog
  204. - (void)logout
  205. {
  206. [_dropboxController logout];
  207. [self updateViewAfterSessionChange];
  208. }
  209. - (void)_showLoginPanel
  210. {
  211. self.loginToCloudStorageView.frame = self.tableView.frame;
  212. self.navigationItem.rightBarButtonItem = nil;
  213. [self.view addSubview:self.loginToCloudStorageView];
  214. }
  215. - (IBAction)loginAction:(id)sender
  216. {
  217. if (!_dropboxController.sessionIsLinked)
  218. [[DBSession sharedSession] linkFromController:self];
  219. else
  220. [_dropboxController logout];
  221. }
  222. #pragma mark - VLCLocalNetworkListCell delegation
  223. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  224. {
  225. _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  226. /* selected item is a proper file, ask the user if s/he wants to download it */
  227. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  228. [alert show];
  229. }
  230. @end