VLCDropboxTableViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCDropboxTableViewController.h"
  11. #import "VLCDropboxTableViewCell.h"
  12. #import "VLCDropboxController.h"
  13. #import "VLCAppDelegate.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCDropboxConstants.h"
  16. #import <DropboxSDK/DropboxSDK.h>
  17. @interface VLCDropboxTableViewController ()
  18. {
  19. VLCDropboxController *_dropboxController;
  20. NSString *_currentPath;
  21. UIBarButtonItem *_numberOfFilesBarButtonItem;
  22. UIBarButtonItem *_progressBarButtonItem;
  23. UIBarButtonItem *_downloadingBarLabel;
  24. UIProgressView *_progressView;
  25. UIActivityIndicatorView *_activityIndicator;
  26. DBMetadata *_selectedFile;
  27. }
  28. @end
  29. @implementation VLCDropboxTableViewController
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. _dropboxController = [[VLCDropboxController alloc] init];
  34. _dropboxController.delegate = self;
  35. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  36. [DBSession setSharedSession:dbSession];
  37. [DBRequest setNetworkRequestDelegate:_dropboxController];
  38. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  39. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  40. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  41. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  42. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  43. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  44. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  45. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  46. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  47. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  48. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  49. [_loginToDropboxButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  50. [self _showProgressInToolbar:NO];
  51. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  52. _activityIndicator.hidesWhenStopped = YES;
  53. [self.view addSubview:_activityIndicator];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated
  56. {
  57. self.navigationController.toolbarHidden = NO;
  58. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  59. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  60. [self updateViewAfterSessionChange];
  61. [super viewWillAppear:animated];
  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. }
  68. - (void)viewWillDisappear:(BOOL)animated
  69. {
  70. self.navigationController.toolbarHidden = YES;
  71. [super viewWillDisappear:animated];
  72. }
  73. - (void)_showProgressInToolbar:(BOOL)value
  74. {
  75. if (!value)
  76. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  77. else {
  78. _progressView.progress = 0.;
  79. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  80. }
  81. }
  82. - (void)_requestInformationForCurrentPath
  83. {
  84. [_activityIndicator startAnimating];
  85. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  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 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  114. }
  115. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  118. if (!_selectedFile.isDirectory) {
  119. /* selected item is a proper file, ask the user if s/he wants to download it */
  120. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", @""), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
  121. [alert show];
  122. } else {
  123. /* dive into subdirectory */
  124. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  125. [self _requestInformationForCurrentPath];
  126. _selectedFile = nil;
  127. }
  128. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  129. }
  130. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  131. {
  132. if (buttonIndex == 1)
  133. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  134. _selectedFile = nil;
  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. }
  149. - (void)operationWithProgressInformationStarted
  150. {
  151. [self _showProgressInToolbar:YES];
  152. }
  153. - (void)currentProgressInformation:(float)progress
  154. {
  155. [_progressView setProgress: progress animated:YES];
  156. }
  157. - (void)operationWithProgressInformationStopped
  158. {
  159. [self _showProgressInToolbar:NO];
  160. }
  161. #pragma mark - communication with app delegate
  162. - (void)updateViewAfterSessionChange
  163. {
  164. if (![[DBSession sharedSession] isLinked]) {
  165. [self _showLoginPanel];
  166. return;
  167. } else if (self.loginToDropboxView.superview)
  168. [self.loginToDropboxView removeFromSuperview];
  169. _currentPath = @"/";
  170. [self _requestInformationForCurrentPath];
  171. }
  172. #pragma mark - login dialog
  173. - (void)_showLoginPanel
  174. {
  175. self.loginToDropboxView.frame = self.tableView.frame;
  176. [self.tableView addSubview:self.loginToDropboxView];
  177. }
  178. - (IBAction)loginToDropboxAction:(id)sender
  179. {
  180. if (!_dropboxController.sessionIsLinked)
  181. [[DBSession sharedSession] linkFromController:self];
  182. else
  183. [_dropboxController logout];
  184. }
  185. @end