VLCDropboxTableViewController.m 11 KB

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