VLCDropboxTableViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #import "VLCDropboxTableViewController.h"
  9. #import "VLCDropboxTableViewCell.h"
  10. #import "VLCDropboxController.h"
  11. #import "VLCAppDelegate.h"
  12. #import "VLCPlaylistViewController.h"
  13. #import "VLCDropboxConstants.h"
  14. #import <DropboxSDK/DropboxSDK.h>
  15. @interface VLCDropboxTableViewController ()
  16. {
  17. VLCDropboxController *_dropboxController;
  18. NSString *_currentPath;
  19. UIBarButtonItem *_numberOfFilesBarButtonItem;
  20. UIBarButtonItem *_progressBarButtonItem;
  21. UIBarButtonItem *_downloadingBarLabel;
  22. UIProgressView *_progressView;
  23. UIBarButtonItem *_backButton;
  24. UIActivityIndicatorView *_activityIndicator;
  25. }
  26. @end
  27. @implementation VLCDropboxTableViewController
  28. - (id)initWithStyle:(UITableViewStyle)style
  29. {
  30. self = [super initWithStyle:style];
  31. return self;
  32. }
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  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. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_DONE", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss:)];
  42. [addButton setBackgroundImage:[UIImage imageNamed:@"doneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  43. [addButton setBackgroundImage:[UIImage imageNamed:@"doneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  44. self.navigationItem.rightBarButtonItem = addButton;
  45. _backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(folderUp:)];
  46. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  47. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  48. self.navigationItem.leftBarButtonItem = _backButton;
  49. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  50. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  51. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  52. self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  53. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  54. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  55. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  56. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  57. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  58. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  59. [self _showProgressInToolbar:NO];
  60. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  61. _activityIndicator.hidesWhenStopped = YES;
  62. CGRect aiFrame = _activityIndicator.frame;
  63. CGSize tvSize = self.tableView.frame.size;
  64. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  65. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  66. _activityIndicator.frame = aiFrame;
  67. [self.view addSubview:_activityIndicator];
  68. }
  69. - (void)viewWillAppear:(BOOL)animated
  70. {
  71. self.navigationController.toolbarHidden = NO;
  72. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  73. [self updateViewAfterSessionChange];
  74. [super viewWillAppear:animated];
  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. }
  95. - (IBAction)dismiss:(id)sender
  96. {
  97. [self.navigationController dismissModalViewControllerAnimated:YES];
  98. }
  99. - (IBAction)folderUp:(id)sender
  100. {
  101. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  102. [self _requestInformationForCurrentPath];
  103. }
  104. #pragma mark - Table view data source
  105. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  106. {
  107. return 1;
  108. }
  109. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  110. {
  111. return _dropboxController.currentListFiles.count;
  112. }
  113. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. static NSString *CellIdentifier = @"DropboxCell";
  116. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  117. if (cell == nil)
  118. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  119. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  120. return cell;
  121. }
  122. #pragma mark - Table view delegate
  123. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. DBMetadata *selectedFile = _dropboxController.currentListFiles[indexPath.row];
  126. if (!selectedFile.isDirectory) {
  127. /* selected item is a proper file, ask the user if s/he wants to download it */
  128. 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];
  129. [alert show];
  130. } else {
  131. /* dive into subdirectory */
  132. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", selectedFile.filename];
  133. [self _requestInformationForCurrentPath];
  134. }
  135. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  136. }
  137. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  138. {
  139. if (buttonIndex == 1) {
  140. DBMetadata *selectedFile = _dropboxController.currentListFiles[self.tableView.indexPathForSelectedRow.row];
  141. [_dropboxController downloadFileToDocumentFolder:selectedFile];
  142. }
  143. }
  144. #pragma mark - dropbox controller delegate
  145. - (void)mediaListUpdated
  146. {
  147. [_activityIndicator stopAnimating];
  148. [self.tableView reloadData];
  149. NSUInteger count = _dropboxController.currentListFiles.count;
  150. if (count == 0)
  151. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  152. else if (count != 1)
  153. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  154. else
  155. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  156. NSString *backButtonTitle = _currentPath.lastPathComponent;
  157. if ([backButtonTitle isEqualToString:@"/"]) {
  158. backButtonTitle = @"";
  159. _backButton.enabled = NO;
  160. } else
  161. _backButton.enabled = YES;
  162. _backButton.title = backButtonTitle;
  163. }
  164. - (void)operationWithProgressInformationStarted
  165. {
  166. [self _showProgressInToolbar:YES];
  167. }
  168. - (void)currentProgressInformation:(float)progress
  169. {
  170. [_progressView setProgress: progress animated:YES];
  171. }
  172. - (void)operationWithProgressInformationStopped
  173. {
  174. [self _showProgressInToolbar:NO];
  175. }
  176. #pragma mark - communication with app delegate
  177. - (void)updateViewAfterSessionChange
  178. {
  179. if (![[DBSession sharedSession] isLinked]) {
  180. [self _showLoginPanel];
  181. return;
  182. } else if (self.loginToDropboxView.superview)
  183. [self.loginToDropboxView removeFromSuperview];
  184. _currentPath = @"/";
  185. [self _requestInformationForCurrentPath];
  186. }
  187. #pragma mark - login dialog
  188. - (void)_showLoginPanel
  189. {
  190. self.loginToDropboxView.frame = self.tableView.frame;
  191. [self.tableView addSubview:self.loginToDropboxView];
  192. }
  193. - (IBAction)loginToDropboxAction:(id)sender
  194. {
  195. if (!_dropboxController.sessionIsLinked)
  196. [[DBSession sharedSession] linkFromController:self];
  197. else
  198. [_dropboxController logout];
  199. }
  200. @end