VLCDropboxTableViewController.m 12 KB

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