VLCDropboxTableViewController.m 8.6 KB

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