VLCDropboxTableViewController.m 8.3 KB

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