VLCDropboxTableViewController.m 10 KB

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