VLCDropboxTableViewController.m 11 KB

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