VLCDropboxTableViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. [self updateViewAfterSessionChange];
  68. [super viewWillAppear:animated];
  69. }
  70. - (void)viewWillDisappear:(BOOL)animated
  71. {
  72. self.navigationController.toolbarHidden = YES;
  73. [super viewWillDisappear:animated];
  74. }
  75. - (void)_showProgressInToolbar:(BOOL)value
  76. {
  77. if (!value)
  78. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  79. else {
  80. _progressView.progress = 0.;
  81. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  82. }
  83. }
  84. - (void)_requestInformationForCurrentPath
  85. {
  86. [_activityIndicator startAnimating];
  87. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  88. }
  89. - (IBAction)dismiss:(id)sender
  90. {
  91. [self.navigationController dismissModalViewControllerAnimated:YES];
  92. }
  93. - (IBAction)folderUp:(id)sender
  94. {
  95. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  96. [self _requestInformationForCurrentPath];
  97. }
  98. #pragma mark - Table view data source
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  100. {
  101. return 1;
  102. }
  103. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  104. {
  105. return _dropboxController.currentListFiles.count;
  106. }
  107. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  108. {
  109. static NSString *CellIdentifier = @"DropboxCell";
  110. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  111. if (cell == nil)
  112. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  113. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  114. return cell;
  115. }
  116. #pragma mark - Table view delegate
  117. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. DBMetadata *selectedFile = _dropboxController.currentListFiles[indexPath.row];
  120. if (!selectedFile.isDirectory) {
  121. /* selected item is a proper file, download it */
  122. [_dropboxController downloadFileToDocumentFolder:selectedFile];
  123. } else {
  124. /* dive into subdirectory */
  125. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", selectedFile.filename];
  126. [self _requestInformationForCurrentPath];
  127. }
  128. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  129. }
  130. #pragma mark - dropbox controller delegate
  131. - (void)mediaListUpdated
  132. {
  133. [_activityIndicator stopAnimating];
  134. [self.tableView reloadData];
  135. NSUInteger count = _dropboxController.currentListFiles.count;
  136. if (count == 0)
  137. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  138. else if (count != 1)
  139. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  140. else
  141. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  142. }
  143. - (void)operationWithProgressInformationStarted
  144. {
  145. [self _showProgressInToolbar:YES];
  146. }
  147. - (void)currentProgressInformation:(float)progress
  148. {
  149. [_progressView setProgress: progress animated:YES];
  150. }
  151. - (void)operationWithProgressInformationStopped
  152. {
  153. [self _showProgressInToolbar:NO];
  154. }
  155. #pragma mark - communication with app delegate
  156. - (void)updateViewAfterSessionChange
  157. {
  158. if (![[DBSession sharedSession] isLinked]) {
  159. [self _showLoginPanel];
  160. return;
  161. } else if (self.loginToDropboxView.superview)
  162. [self.loginToDropboxView removeFromSuperview];
  163. _currentPath = @"/";
  164. [self _requestInformationForCurrentPath];
  165. }
  166. #pragma mark - login dialog
  167. - (void)_showLoginPanel
  168. {
  169. self.loginToDropboxView.frame = self.tableView.frame;
  170. [self.tableView addSubview:self.loginToDropboxView];
  171. }
  172. - (IBAction)loginToDropboxAction:(id)sender
  173. {
  174. if (!_dropboxController.sessionIsLinked)
  175. [[DBSession sharedSession] linkFromController:self];
  176. else
  177. [_dropboxController logout];
  178. }
  179. @end