VLCDropboxTableViewController.m 8.5 KB

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