VLCDropboxTableViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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)folderUp:(id)sender
  92. {
  93. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  94. [self _requestInformationForCurrentPath];
  95. }
  96. #pragma mark - Table view data source
  97. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  98. {
  99. return 1;
  100. }
  101. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  102. {
  103. return _dropboxController.currentListFiles.count;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. static NSString *CellIdentifier = @"DropboxCell";
  108. VLCDropboxTableViewCell *cell = (VLCDropboxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  109. if (cell == nil)
  110. cell = [VLCDropboxTableViewCell cellWithReuseIdentifier:CellIdentifier];
  111. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  112. return cell;
  113. }
  114. #pragma mark - Table view delegate
  115. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  118. }
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. DBMetadata *selectedFile = _dropboxController.currentListFiles[indexPath.row];
  122. if (!selectedFile.isDirectory) {
  123. /* selected item is a proper file, ask the user if s/he wants to download it */
  124. 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];
  125. [alert show];
  126. } else {
  127. /* dive into subdirectory */
  128. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", selectedFile.filename];
  129. [self _requestInformationForCurrentPath];
  130. }
  131. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  132. }
  133. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  134. {
  135. if (buttonIndex == 1) {
  136. DBMetadata *selectedFile = _dropboxController.currentListFiles[self.tableView.indexPathForSelectedRow.row];
  137. [_dropboxController downloadFileToDocumentFolder:selectedFile];
  138. }
  139. }
  140. #pragma mark - dropbox controller delegate
  141. - (void)mediaListUpdated
  142. {
  143. [_activityIndicator stopAnimating];
  144. [self.tableView reloadData];
  145. NSUInteger count = _dropboxController.currentListFiles.count;
  146. if (count == 0)
  147. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  148. else if (count != 1)
  149. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  150. else
  151. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  152. }
  153. - (void)operationWithProgressInformationStarted
  154. {
  155. [self _showProgressInToolbar:YES];
  156. }
  157. - (void)currentProgressInformation:(float)progress
  158. {
  159. [_progressView setProgress: progress animated:YES];
  160. }
  161. - (void)operationWithProgressInformationStopped
  162. {
  163. [self _showProgressInToolbar:NO];
  164. }
  165. #pragma mark - communication with app delegate
  166. - (void)updateViewAfterSessionChange
  167. {
  168. if (![[DBSession sharedSession] isLinked]) {
  169. [self _showLoginPanel];
  170. return;
  171. } else if (self.loginToDropboxView.superview)
  172. [self.loginToDropboxView removeFromSuperview];
  173. _currentPath = @"/";
  174. [self _requestInformationForCurrentPath];
  175. }
  176. #pragma mark - login dialog
  177. - (void)_showLoginPanel
  178. {
  179. self.loginToDropboxView.frame = self.tableView.frame;
  180. [self.tableView addSubview:self.loginToDropboxView];
  181. }
  182. - (IBAction)loginToDropboxAction:(id)sender
  183. {
  184. if (!_dropboxController.sessionIsLinked)
  185. [[DBSession sharedSession] linkFromController:self];
  186. else
  187. [_dropboxController logout];
  188. }
  189. @end