VLCDropboxTableViewController.m 9.3 KB

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