VLCDropboxTableViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // VLCDropboxTableViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 24.05.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCDropboxTableViewController.h"
  11. #import "VLCDropboxTableViewCell.h"
  12. #import "VLCDropboxController.h"
  13. #import "VLCAppDelegate.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCDropboxConstants.h"
  16. #import "UIBarButtonItem+Theme.h"
  17. #import <DropboxSDK/DropboxSDK.h>
  18. @interface VLCDropboxTableViewController ()
  19. {
  20. VLCDropboxController *_dropboxController;
  21. NSString *_currentPath;
  22. UIBarButtonItem *_backButton;
  23. UIBarButtonItem *_backToMenuButton;
  24. UIBarButtonItem *_numberOfFilesBarButtonItem;
  25. UIBarButtonItem *_progressBarButtonItem;
  26. UIBarButtonItem *_downloadingBarLabel;
  27. UIProgressView *_progressView;
  28. UIActivityIndicatorView *_activityIndicator;
  29. DBMetadata *_selectedFile;
  30. }
  31. @end
  32. @implementation VLCDropboxTableViewController
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. self.modalPresentationStyle = UIModalPresentationFormSheet;
  37. _dropboxController = [[VLCDropboxController alloc] init];
  38. _dropboxController.delegate = self;
  39. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  40. [DBSession setSharedSession:dbSession];
  41. [DBRequest setNetworkRequestDelegate:_dropboxController];
  42. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  43. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  44. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  45. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  46. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  47. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  48. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  49. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  50. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  51. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  52. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  53. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  54. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  55. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  56. [_loginToDropboxButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  57. [self _showProgressInToolbar:NO];
  58. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  59. _activityIndicator.hidesWhenStopped = YES;
  60. [self.view addSubview:_activityIndicator];
  61. }
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. self.navigationController.toolbarHidden = NO;
  65. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  66. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  67. [self updateViewAfterSessionChange];
  68. [super viewWillAppear:animated];
  69. CGRect aiFrame = _activityIndicator.frame;
  70. CGSize tvSize = self.tableView.frame.size;
  71. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  72. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  73. _activityIndicator.frame = aiFrame;
  74. }
  75. - (void)viewWillDisappear:(BOOL)animated
  76. {
  77. self.navigationController.toolbarHidden = YES;
  78. [super viewWillDisappear:animated];
  79. }
  80. - (void)_showProgressInToolbar:(BOOL)value
  81. {
  82. if (!value)
  83. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  84. else {
  85. _progressView.progress = 0.;
  86. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  87. }
  88. }
  89. - (void)_requestInformationForCurrentPath
  90. {
  91. [_activityIndicator startAnimating];
  92. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  93. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  94. }
  95. #pragma mark - interface interaction
  96. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  97. {
  98. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  99. return NO;
  100. return YES;
  101. }
  102. - (IBAction)goBack:(id)sender
  103. {
  104. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  105. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  106. [self _requestInformationForCurrentPath];
  107. } else
  108. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  109. }
  110. #pragma mark - Table view data source
  111. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  112. {
  113. return 1;
  114. }
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117. return _dropboxController.currentListFiles.count;
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. static NSString *CellIdentifier = @"DropboxCell";
  122. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  123. if (cell == nil)
  124. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  125. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  126. return cell;
  127. }
  128. #pragma mark - Table view delegate
  129. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  132. }
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  136. if (!_selectedFile.isDirectory) {
  137. /* selected item is a proper file, ask the user if s/he wants to download it */
  138. 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];
  139. [alert show];
  140. } else {
  141. /* dive into subdirectory */
  142. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  143. [self _requestInformationForCurrentPath];
  144. _selectedFile = nil;
  145. }
  146. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  147. }
  148. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  149. {
  150. if (buttonIndex == 1)
  151. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  152. _selectedFile = nil;
  153. }
  154. #pragma mark - dropbox controller delegate
  155. - (void)mediaListUpdated
  156. {
  157. [_activityIndicator stopAnimating];
  158. [self.tableView reloadData];
  159. NSUInteger count = _dropboxController.currentListFiles.count;
  160. if (count == 0)
  161. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  162. else if (count != 1)
  163. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  164. else
  165. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  166. }
  167. - (void)operationWithProgressInformationStarted
  168. {
  169. [self _showProgressInToolbar:YES];
  170. }
  171. - (void)currentProgressInformation:(float)progress
  172. {
  173. [_progressView setProgress: progress animated:YES];
  174. }
  175. - (void)operationWithProgressInformationStopped
  176. {
  177. [self _showProgressInToolbar:NO];
  178. }
  179. #pragma mark - communication with app delegate
  180. - (void)updateViewAfterSessionChange
  181. {
  182. if (![[DBSession sharedSession] isLinked]) {
  183. [self _showLoginPanel];
  184. return;
  185. } else if (self.loginToDropboxView.superview)
  186. [self.loginToDropboxView removeFromSuperview];
  187. _currentPath = @"/";
  188. [self _requestInformationForCurrentPath];
  189. }
  190. #pragma mark - login dialog
  191. - (void)_showLoginPanel
  192. {
  193. self.loginToDropboxView.frame = self.tableView.frame;
  194. [self.view addSubview:self.loginToDropboxView];
  195. }
  196. - (IBAction)loginToDropboxAction:(id)sender
  197. {
  198. if (!_dropboxController.sessionIsLinked)
  199. [[DBSession sharedSession] linkFromController:self];
  200. else
  201. [_dropboxController logout];
  202. }
  203. @end