VLCDropboxTableViewController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  47. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  48. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  49. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  50. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  51. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  52. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  53. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  54. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  55. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  56. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  57. [_loginToDropboxButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  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. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  95. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : nil;
  96. else
  97. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  98. }
  99. #pragma mark - interface interaction
  100. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  101. {
  102. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  103. return NO;
  104. return YES;
  105. }
  106. - (IBAction)goBack:(id)sender
  107. {
  108. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  109. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  110. [self _requestInformationForCurrentPath];
  111. } else
  112. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  113. }
  114. #pragma mark - Table view data source
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  116. {
  117. return 1;
  118. }
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  120. {
  121. return _dropboxController.currentListFiles.count;
  122. }
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. static NSString *CellIdentifier = @"DropboxCell";
  126. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  127. if (cell == nil)
  128. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  129. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  130. return cell;
  131. }
  132. #pragma mark - Table view delegate
  133. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  136. }
  137. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  140. if (!_selectedFile.isDirectory) {
  141. /* selected item is a proper file, ask the user if s/he wants to download it */
  142. 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];
  143. [alert show];
  144. } else {
  145. /* dive into subdirectory */
  146. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  147. [self _requestInformationForCurrentPath];
  148. _selectedFile = nil;
  149. }
  150. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  151. }
  152. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  153. {
  154. if (buttonIndex == 1)
  155. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  156. _selectedFile = nil;
  157. }
  158. #pragma mark - dropbox controller delegate
  159. - (void)mediaListUpdated
  160. {
  161. [_activityIndicator stopAnimating];
  162. [self.tableView reloadData];
  163. NSUInteger count = _dropboxController.currentListFiles.count;
  164. if (count == 0)
  165. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  166. else if (count != 1)
  167. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  168. else
  169. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  170. }
  171. - (void)operationWithProgressInformationStarted
  172. {
  173. [self _showProgressInToolbar:YES];
  174. }
  175. - (void)currentProgressInformation:(float)progress
  176. {
  177. [_progressView setProgress: progress animated:YES];
  178. }
  179. - (void)operationWithProgressInformationStopped
  180. {
  181. [self _showProgressInToolbar:NO];
  182. }
  183. #pragma mark - communication with app delegate
  184. - (void)updateViewAfterSessionChange
  185. {
  186. if (![[DBSession sharedSession] isLinked]) {
  187. [self _showLoginPanel];
  188. return;
  189. } else if (self.loginToDropboxView.superview)
  190. [self.loginToDropboxView removeFromSuperview];
  191. _currentPath = @"/";
  192. [self _requestInformationForCurrentPath];
  193. }
  194. #pragma mark - login dialog
  195. - (void)_showLoginPanel
  196. {
  197. self.loginToDropboxView.frame = self.tableView.frame;
  198. [self.view addSubview:self.loginToDropboxView];
  199. }
  200. - (IBAction)loginToDropboxAction:(id)sender
  201. {
  202. if (!_dropboxController.sessionIsLinked)
  203. [[DBSession sharedSession] linkFromController:self];
  204. else
  205. [_dropboxController logout];
  206. }
  207. @end