VLCDropboxTableViewController.m 9.2 KB

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