VLCDropboxTableViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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:UIBarButtonItemStyleDone target:self action:@selector(dismiss:)];
  42. self.navigationItem.rightBarButtonItem = addButton;
  43. _backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(folderUp:)];
  44. [_backButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  45. [_backButton setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  46. self.navigationItem.leftBarButtonItem = _backButton;
  47. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  48. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  49. self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  50. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  51. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  52. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  53. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  54. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  55. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  56. [self _showProgressInToolbar:NO];
  57. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  58. _activityIndicator.hidesWhenStopped = YES;
  59. CGRect aiFrame = _activityIndicator.frame;
  60. CGSize tvSize = self.tableView.frame.size;
  61. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  62. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  63. _activityIndicator.frame = aiFrame;
  64. [self.view addSubview:_activityIndicator];
  65. }
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. self.navigationController.toolbarHidden = NO;
  69. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  70. [self updateViewAfterSessionChange];
  71. [super viewWillAppear:animated];
  72. }
  73. - (void)viewWillDisappear:(BOOL)animated
  74. {
  75. self.navigationController.toolbarHidden = YES;
  76. [super viewWillDisappear:animated];
  77. }
  78. - (void)_showProgressInToolbar:(BOOL)value
  79. {
  80. if (!value)
  81. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  82. else {
  83. _progressView.progress = 0.;
  84. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  85. }
  86. }
  87. - (void)_requestInformationForCurrentPath
  88. {
  89. [_activityIndicator startAnimating];
  90. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  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. NSString *backButtonTitle = _currentPath.lastPathComponent;
  146. if ([backButtonTitle isEqualToString:@"/"]) {
  147. backButtonTitle = @"";
  148. _backButton.enabled = NO;
  149. } else
  150. _backButton.enabled = YES;
  151. _backButton.title = backButtonTitle;
  152. }
  153. - (void)operationWithProgressInformationStarted
  154. {
  155. [self _showProgressInToolbar:YES];
  156. }
  157. - (void)currentProgressInformation:(float)progress
  158. {
  159. [_progressView setProgress: progress animated:YES];
  160. }
  161. - (void)operationWithProgressInformationStopped
  162. {
  163. [self _showProgressInToolbar:NO];
  164. }
  165. #pragma mark - communication with app delegate
  166. - (void)updateViewAfterSessionChange
  167. {
  168. if (![[DBSession sharedSession] isLinked]) {
  169. [self _showLoginPanel];
  170. return;
  171. } else if (self.loginToDropboxView.superview)
  172. [self.loginToDropboxView removeFromSuperview];
  173. _currentPath = @"/";
  174. [self _requestInformationForCurrentPath];
  175. }
  176. #pragma mark - login dialog
  177. - (void)_showLoginPanel
  178. {
  179. self.loginToDropboxView.frame = self.tableView.frame;
  180. [self.tableView addSubview:self.loginToDropboxView];
  181. }
  182. - (IBAction)loginToDropboxAction:(id)sender
  183. {
  184. if (!_dropboxController.sessionIsLinked)
  185. [[DBSession sharedSession] linkFromController:self];
  186. else
  187. [_dropboxController logout];
  188. }
  189. @end