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