VLCDropboxTableViewController.m 8.7 KB

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