VLCDropboxTableViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  86. [self.view addSubview:_activityIndicator];
  87. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  88. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  89. }
  90. - (void)viewWillAppear:(BOOL)animated
  91. {
  92. self.navigationController.toolbarHidden = NO;
  93. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  94. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  95. [self updateViewAfterSessionChange];
  96. [super viewWillAppear:animated];
  97. [self.cloudStorageLogo sizeToFit];
  98. self.cloudStorageLogo.center = self.view.center;
  99. }
  100. - (void)viewWillDisappear:(BOOL)animated
  101. {
  102. self.navigationController.toolbarHidden = YES;
  103. [super viewWillDisappear:animated];
  104. }
  105. - (void)_showProgressInToolbar:(BOOL)value
  106. {
  107. if (!value)
  108. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  109. else {
  110. _progressBar.progress = 0.;
  111. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  112. }
  113. }
  114. - (void)_requestInformationForCurrentPath
  115. {
  116. [_activityIndicator startAnimating];
  117. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  118. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  119. }
  120. #pragma mark - interface interaction
  121. - (BOOL)shouldAutorotate
  122. {
  123. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  124. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  125. return NO;
  126. return YES;
  127. }
  128. - (IBAction)goBack:(id)sender
  129. {
  130. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  131. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  132. [self _requestInformationForCurrentPath];
  133. } else
  134. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  135. }
  136. #pragma mark - Table view data source
  137. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  138. {
  139. return 1;
  140. }
  141. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  142. {
  143. return _dropboxController.currentListFiles.count;
  144. }
  145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. static NSString *CellIdentifier = @"DropboxCell";
  148. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  149. if (cell == nil)
  150. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  151. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  152. cell.delegate = self;
  153. return cell;
  154. }
  155. #pragma mark - Table view delegate
  156. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  159. }
  160. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  163. if (!_selectedFile.isDirectory)
  164. [_dropboxController streamFile:_selectedFile];
  165. else {
  166. /* dive into subdirectory */
  167. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  168. [self _requestInformationForCurrentPath];
  169. }
  170. _selectedFile = nil;
  171. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  172. }
  173. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  174. {
  175. if (buttonIndex == 1)
  176. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  177. _selectedFile = nil;
  178. }
  179. #pragma mark - dropbox controller delegate
  180. - (void)mediaListUpdated
  181. {
  182. [_activityIndicator stopAnimating];
  183. [self.tableView reloadData];
  184. NSUInteger count = _dropboxController.currentListFiles.count;
  185. if (count == 0)
  186. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  187. else if (count != 1)
  188. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  189. else
  190. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  191. }
  192. - (void)operationWithProgressInformationStarted
  193. {
  194. [self _showProgressInToolbar:YES];
  195. }
  196. - (void)updateRemainingTime:(NSString *)time
  197. {
  198. [_progressLabel setText:[NSString stringWithFormat:NSLocalizedString(@"REMAINING_TIME", nil), time]];
  199. CGSize size = [_progressLabel.text sizeWithFont:_progressLabel.font];
  200. [_progressLabel setFrame:CGRectMake(_progressLabel.frame.origin.x, _progressLabel.frame.origin.y, size.width, size.height)];
  201. }
  202. - (void)currentProgressInformation:(float)progress {
  203. [_progressBar setProgress:progress animated:YES];
  204. }
  205. - (void)operationWithProgressInformationStopped
  206. {
  207. [self _showProgressInToolbar:NO];
  208. }
  209. #pragma mark - communication with app delegate
  210. - (void)updateViewAfterSessionChange
  211. {
  212. if (![[DBSession sharedSession] isLinked]) {
  213. [self _showLoginPanel];
  214. return;
  215. } else if (self.loginToCloudStorageView.superview)
  216. [self.loginToCloudStorageView removeFromSuperview];
  217. _currentPath = @"/";
  218. [self _requestInformationForCurrentPath];
  219. }
  220. #pragma mark - login dialog
  221. - (void)_showLoginPanel
  222. {
  223. self.loginToCloudStorageView.frame = self.tableView.frame;
  224. [self.view addSubview:self.loginToCloudStorageView];
  225. }
  226. - (IBAction)loginAction:(id)sender
  227. {
  228. if (!_dropboxController.sessionIsLinked)
  229. [[DBSession sharedSession] linkFromController:self];
  230. else
  231. [_dropboxController logout];
  232. }
  233. #pragma mark - table view cell delegation
  234. #pragma mark - VLCLocalNetworkListCell delegation
  235. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  236. {
  237. _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  238. /* selected item is a proper file, ask the user if s/he wants to download it */
  239. 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];
  240. [alert show];
  241. }
  242. @end