VLCDropboxTableViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <DropboxSDK/DropboxSDK.h>
  17. @interface VLCDropboxTableViewController ()
  18. {
  19. VLCDropboxController *_dropboxController;
  20. NSString *_currentPath;
  21. UIBarButtonItem *_backButton;
  22. UIBarButtonItem *_numberOfFilesBarButtonItem;
  23. UIBarButtonItem *_progressBarButtonItem;
  24. UIBarButtonItem *_downloadingBarLabel;
  25. UIProgressView *_progressView;
  26. UIActivityIndicatorView *_activityIndicator;
  27. DBMetadata *_selectedFile;
  28. }
  29. @end
  30. @implementation VLCDropboxTableViewController
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. _dropboxController = [[VLCDropboxController alloc] init];
  35. _dropboxController.delegate = self;
  36. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  37. [DBSession setSharedSession:dbSession];
  38. [DBRequest setNetworkRequestDelegate:_dropboxController];
  39. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  40. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  41. _backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
  42. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  43. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  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. - (IBAction)goBack:(id)sender
  95. {
  96. if (![_currentPath isEqualToString:@"/"]) {
  97. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  98. [self _requestInformationForCurrentPath];
  99. } else
  100. [self.navigationController popViewControllerAnimated:YES];
  101. }
  102. #pragma mark - Table view data source
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  104. {
  105. return 1;
  106. }
  107. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  108. {
  109. return _dropboxController.currentListFiles.count;
  110. }
  111. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. static NSString *CellIdentifier = @"DropboxCell";
  114. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  115. if (cell == nil)
  116. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  117. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  118. return cell;
  119. }
  120. #pragma mark - Table view delegate
  121. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  124. }
  125. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  128. if (!_selectedFile.isDirectory) {
  129. /* selected item is a proper file, ask the user if s/he wants to download it */
  130. 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];
  131. [alert show];
  132. } else {
  133. /* dive into subdirectory */
  134. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  135. [self _requestInformationForCurrentPath];
  136. _selectedFile = nil;
  137. }
  138. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  139. }
  140. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  141. {
  142. if (buttonIndex == 1)
  143. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  144. _selectedFile = nil;
  145. }
  146. #pragma mark - dropbox controller delegate
  147. - (void)mediaListUpdated
  148. {
  149. [_activityIndicator stopAnimating];
  150. [self.tableView reloadData];
  151. NSUInteger count = _dropboxController.currentListFiles.count;
  152. if (count == 0)
  153. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  154. else if (count != 1)
  155. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  156. else
  157. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  158. }
  159. - (void)operationWithProgressInformationStarted
  160. {
  161. [self _showProgressInToolbar:YES];
  162. }
  163. - (void)currentProgressInformation:(float)progress
  164. {
  165. [_progressView setProgress: progress animated:YES];
  166. }
  167. - (void)operationWithProgressInformationStopped
  168. {
  169. [self _showProgressInToolbar:NO];
  170. }
  171. #pragma mark - communication with app delegate
  172. - (void)updateViewAfterSessionChange
  173. {
  174. if (![[DBSession sharedSession] isLinked]) {
  175. [self _showLoginPanel];
  176. return;
  177. } else if (self.loginToDropboxView.superview)
  178. [self.loginToDropboxView removeFromSuperview];
  179. _currentPath = @"/";
  180. [self _requestInformationForCurrentPath];
  181. }
  182. #pragma mark - login dialog
  183. - (void)_showLoginPanel
  184. {
  185. self.loginToDropboxView.frame = self.tableView.frame;
  186. [self.tableView addSubview:self.loginToDropboxView];
  187. }
  188. - (IBAction)loginToDropboxAction:(id)sender
  189. {
  190. if (!_dropboxController.sessionIsLinked)
  191. [[DBSession sharedSession] linkFromController:self];
  192. else
  193. [_dropboxController logout];
  194. }
  195. @end