VLCDropboxTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. UIBarButtonItem *_backButton;
  26. UIActivityIndicatorView *_activityIndicator;
  27. }
  28. @end
  29. @implementation VLCDropboxTableViewController
  30. - (id)initWithStyle:(UITableViewStyle)style
  31. {
  32. self = [super initWithStyle:style];
  33. return self;
  34. }
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. _dropboxController = [[VLCDropboxController alloc] init];
  39. _dropboxController.delegate = self;
  40. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  41. [DBSession setSharedSession:dbSession];
  42. [DBRequest setNetworkRequestDelegate:_dropboxController];
  43. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_DONE", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss:)];
  44. [addButton setBackgroundImage:[UIImage imageNamed:@"doneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  45. [addButton setBackgroundImage:[UIImage imageNamed:@"doneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  46. [addButton setTitleTextAttributes:@{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} forState:UIControlStateNormal];
  47. self.navigationItem.rightBarButtonItem = addButton;
  48. _backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(folderUp:)];
  49. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  50. [_backButton setBackgroundImage:[UIImage imageNamed:@"backButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  51. self.navigationItem.leftBarButtonItem = _backButton;
  52. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  53. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  54. self.tableView.rowHeight = [VLCDropboxTableViewCell heightOfCell];
  55. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  56. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  57. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  58. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  59. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  60. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  61. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  62. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  63. [_loginToDropboxButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  64. [self _showProgressInToolbar:NO];
  65. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  66. _activityIndicator.hidesWhenStopped = YES;
  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.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  74. [self updateViewAfterSessionChange];
  75. [super viewWillAppear:animated];
  76. CGRect aiFrame = _activityIndicator.frame;
  77. CGSize tvSize = self.tableView.frame.size;
  78. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  79. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  80. _activityIndicator.frame = aiFrame;
  81. }
  82. - (void)viewWillDisappear:(BOOL)animated
  83. {
  84. self.navigationController.toolbarHidden = YES;
  85. [super viewWillDisappear:animated];
  86. }
  87. - (void)_showProgressInToolbar:(BOOL)value
  88. {
  89. if (!value)
  90. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  91. else {
  92. _progressView.progress = 0.;
  93. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  94. }
  95. }
  96. - (void)_requestInformationForCurrentPath
  97. {
  98. [_activityIndicator startAnimating];
  99. [_dropboxController requestDirectoryListingAtPath:_currentPath];
  100. }
  101. - (IBAction)dismiss:(id)sender
  102. {
  103. [self.navigationController dismissModalViewControllerAnimated:YES];
  104. }
  105. - (IBAction)folderUp:(id)sender
  106. {
  107. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  108. [self _requestInformationForCurrentPath];
  109. }
  110. #pragma mark - Table view data source
  111. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  112. {
  113. return 1;
  114. }
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117. return _dropboxController.currentListFiles.count;
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. static NSString *CellIdentifier = @"DropboxCell";
  122. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  123. if (cell == nil)
  124. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  125. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  126. return cell;
  127. }
  128. #pragma mark - Table view delegate
  129. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  132. }
  133. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. DBMetadata *selectedFile = _dropboxController.currentListFiles[indexPath.row];
  136. if (!selectedFile.isDirectory) {
  137. /* selected item is a proper file, ask the user if s/he wants to download it */
  138. 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];
  139. [alert show];
  140. } else {
  141. /* dive into subdirectory */
  142. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", selectedFile.filename];
  143. [self _requestInformationForCurrentPath];
  144. }
  145. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  146. }
  147. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  148. {
  149. if (buttonIndex == 1) {
  150. DBMetadata *selectedFile = _dropboxController.currentListFiles[self.tableView.indexPathForSelectedRow.row];
  151. [_dropboxController downloadFileToDocumentFolder:selectedFile];
  152. }
  153. }
  154. #pragma mark - dropbox controller delegate
  155. - (void)mediaListUpdated
  156. {
  157. [_activityIndicator stopAnimating];
  158. [self.tableView reloadData];
  159. NSUInteger count = _dropboxController.currentListFiles.count;
  160. if (count == 0)
  161. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  162. else if (count != 1)
  163. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  164. else
  165. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  166. NSString *backButtonTitle = _currentPath.lastPathComponent;
  167. if ([backButtonTitle isEqualToString:@"/"]) {
  168. backButtonTitle = @"";
  169. _backButton.enabled = NO;
  170. } else
  171. _backButton.enabled = YES;
  172. _backButton.title = backButtonTitle;
  173. }
  174. - (void)operationWithProgressInformationStarted
  175. {
  176. [self _showProgressInToolbar:YES];
  177. }
  178. - (void)currentProgressInformation:(float)progress
  179. {
  180. [_progressView setProgress: progress animated:YES];
  181. }
  182. - (void)operationWithProgressInformationStopped
  183. {
  184. [self _showProgressInToolbar:NO];
  185. }
  186. #pragma mark - communication with app delegate
  187. - (void)updateViewAfterSessionChange
  188. {
  189. if (![[DBSession sharedSession] isLinked]) {
  190. [self _showLoginPanel];
  191. return;
  192. } else if (self.loginToDropboxView.superview)
  193. [self.loginToDropboxView removeFromSuperview];
  194. _currentPath = @"/";
  195. [self _requestInformationForCurrentPath];
  196. }
  197. #pragma mark - login dialog
  198. - (void)_showLoginPanel
  199. {
  200. self.loginToDropboxView.frame = self.tableView.frame;
  201. [self.tableView addSubview:self.loginToDropboxView];
  202. }
  203. - (IBAction)loginToDropboxAction:(id)sender
  204. {
  205. if (!_dropboxController.sessionIsLinked)
  206. [[DBSession sharedSession] linkFromController:self];
  207. else
  208. [_dropboxController logout];
  209. }
  210. @end