VLCDropboxTableViewController.m 7.8 KB

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