VLCDropboxTableViewController.m 9.8 KB

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