VLCDropboxTableViewController.m 9.7 KB

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