VLCDropboxTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCDropboxController>
  25. {
  26. VLCDropboxController *_dropboxController;
  27. NSString *_currentPath;
  28. UIBarButtonItem *_backButton;
  29. UIBarButtonItem *_backToMenuButton;
  30. UIBarButtonItem *_numberOfFilesBarButtonItem;
  31. UIBarButtonItem *_progressBarButtonItem;
  32. UIBarButtonItem *_downloadingBarLabel;
  33. UIProgressView *_progressBar;
  34. UILabel *_progressLabel;
  35. UIActivityIndicatorView *_activityIndicator;
  36. DBMetadata *_selectedFile;
  37. }
  38. @end
  39. @implementation VLCDropboxTableViewController
  40. - (void)viewDidLoad
  41. {
  42. [super viewDidLoad];
  43. self.modalPresentationStyle = UIModalPresentationFormSheet;
  44. _dropboxController = [[VLCDropboxController alloc] init];
  45. _dropboxController.delegate = self;
  46. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  47. [DBSession setSharedSession:dbSession];
  48. [DBRequest setNetworkRequestDelegate:_dropboxController];
  49. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  50. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  51. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  52. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  53. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  54. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  55. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  56. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  57. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  58. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  59. _progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  60. _progressLabel = [[UILabel alloc] init];
  61. _progressLabel.textColor = [UIColor whiteColor];
  62. _progressLabel.font = [UIFont systemFontOfSize:11.];
  63. UIView *progressView = [[UIView alloc] init];
  64. [progressView addSubview:_progressBar];
  65. [progressView addSubview:_progressLabel];
  66. [progressView addConstraint:[NSLayoutConstraint constraintWithItem:_progressBar attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
  67. [progressView addConstraint:[NSLayoutConstraint constraintWithItem:progressView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_progressLabel attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
  68. [progressView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_progressBar]-[_progressLabel]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(_progressBar, _progressLabel)]];
  69. progressView.translatesAutoresizingMaskIntoConstraints = NO;
  70. _progressLabel.translatesAutoresizingMaskIntoConstraints = NO;
  71. _progressBar.translatesAutoresizingMaskIntoConstraints = NO;
  72. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:progressView];
  73. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  74. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  75. self.flatLoginButton.hidden = YES;
  76. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  77. } else {
  78. self.loginButton.hidden = YES;
  79. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  80. }
  81. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  82. [self _showProgressInToolbar:NO];
  83. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  84. _activityIndicator.hidesWhenStopped = YES;
  85. [self.view addSubview:_activityIndicator];
  86. }
  87. - (void)viewWillAppear:(BOOL)animated
  88. {
  89. self.navigationController.toolbarHidden = NO;
  90. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  91. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  92. [self updateViewAfterSessionChange];
  93. [super viewWillAppear:animated];
  94. CGRect aiFrame = _activityIndicator.frame;
  95. CGSize tvSize = self.tableView.frame.size;
  96. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  97. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  98. _activityIndicator.frame = aiFrame;
  99. [self.cloudStorageLogo sizeToFit];
  100. self.cloudStorageLogo.center = self.view.center;
  101. }
  102. - (void)viewWillDisappear:(BOOL)animated
  103. {
  104. self.navigationController.toolbarHidden = YES;
  105. [super viewWillDisappear:animated];
  106. }
  107. - (void)_showProgressInToolbar:(BOOL)value
  108. {
  109. if (!value)
  110. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  111. else {
  112. _progressBar.progress = 0.;
  113. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  114. }
  115. }
  116. - (void)_requestInformationForCurrentPath
  117. {
  118. [_activityIndicator startAnimating];
  119. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  120. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  121. }
  122. #pragma mark - interface interaction
  123. - (BOOL)shouldAutorotate
  124. {
  125. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  126. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  127. return NO;
  128. return YES;
  129. }
  130. - (IBAction)goBack:(id)sender
  131. {
  132. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  133. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  134. [self _requestInformationForCurrentPath];
  135. } else
  136. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  137. }
  138. #pragma mark - Table view data source
  139. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  140. {
  141. return 1;
  142. }
  143. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  144. {
  145. return _dropboxController.currentListFiles.count;
  146. }
  147. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  148. {
  149. static NSString *CellIdentifier = @"DropboxCell";
  150. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  151. if (cell == nil)
  152. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  153. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  154. cell.delegate = self;
  155. return cell;
  156. }
  157. #pragma mark - Table view delegate
  158. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  165. if (!_selectedFile.isDirectory)
  166. [_dropboxController streamFile:_selectedFile];
  167. else {
  168. /* dive into subdirectory */
  169. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  170. [self _requestInformationForCurrentPath];
  171. }
  172. _selectedFile = nil;
  173. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  174. }
  175. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  176. {
  177. if (buttonIndex == 1)
  178. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  179. _selectedFile = nil;
  180. }
  181. #pragma mark - dropbox controller delegate
  182. - (void)mediaListUpdated
  183. {
  184. [_activityIndicator stopAnimating];
  185. [self.tableView reloadData];
  186. NSUInteger count = _dropboxController.currentListFiles.count;
  187. if (count == 0)
  188. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  189. else if (count != 1)
  190. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  191. else
  192. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  193. }
  194. - (void)operationWithProgressInformationStarted
  195. {
  196. [self _showProgressInToolbar:YES];
  197. }
  198. - (void)updateRemainingTime:(NSString *)time
  199. {
  200. [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
  201. CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
  202. [_progressLabel setFrame:CGRectMake(_progressLabel.frame.origin.x, _progressLabel.frame.origin.y, size.width, size.height)];
  203. }
  204. - (void)currentProgressInformation:(float)progress {
  205. [_progressBar setProgress:progress animated:YES];
  206. }
  207. - (void)operationWithProgressInformationStopped
  208. {
  209. [self _showProgressInToolbar:NO];
  210. }
  211. #pragma mark - communication with app delegate
  212. - (void)updateViewAfterSessionChange
  213. {
  214. if (![[DBSession sharedSession] isLinked]) {
  215. [self _showLoginPanel];
  216. return;
  217. } else if (self.loginToCloudStorageView.superview)
  218. [self.loginToCloudStorageView removeFromSuperview];
  219. _currentPath = @"/";
  220. [self _requestInformationForCurrentPath];
  221. }
  222. #pragma mark - login dialog
  223. - (void)_showLoginPanel
  224. {
  225. self.loginToCloudStorageView.frame = self.tableView.frame;
  226. [self.view addSubview:self.loginToCloudStorageView];
  227. }
  228. - (IBAction)loginAction:(id)sender
  229. {
  230. if (!_dropboxController.sessionIsLinked)
  231. [[DBSession sharedSession] linkFromController:self];
  232. else
  233. [_dropboxController logout];
  234. }
  235. #pragma mark - table view cell delegation
  236. #pragma mark - VLCLocalNetworkListCell delegation
  237. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  238. {
  239. _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  240. /* selected item is a proper file, ask the user if s/he wants to download it */
  241. 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];
  242. [alert show];
  243. }
  244. @end