VLCDropboxTableViewController.m 7.5 KB

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