VLCGoogleDriveTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // VLCGoogleDriveTableViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Carola Nitz on 21.09.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 "VLCGoogleDriveTableViewController.h"
  11. #import "VLCGoogleDriveTableViewCell.h"
  12. #import "VLCGoogleDriveController.h"
  13. #import "VLCAppDelegate.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "UIBarButtonItem+Theme.h"
  16. #import "VLCGoogleDriveConstants.h"
  17. #import "GTMOAuth2ViewControllerTouch.h"
  18. @interface VLCGoogleDriveTableViewController ()
  19. {
  20. GTLDriveFile *_selectedFile;
  21. GTMOAuth2ViewControllerTouch *_authController;
  22. NSString *_currentPath;
  23. UIBarButtonItem *_backButton;
  24. UIBarButtonItem *_backToMenuButton;
  25. UIBarButtonItem *_numberOfFilesBarButtonItem;
  26. UIBarButtonItem *_progressBarButtonItem;
  27. UIBarButtonItem *_downloadingBarLabel;
  28. UIProgressView *_progressView;
  29. UIActivityIndicatorView *_activityIndicator;
  30. VLCGoogleDriveController *_googleDriveController;
  31. }
  32. @end
  33. @implementation VLCGoogleDriveTableViewController
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. self.modalPresentationStyle = UIModalPresentationFormSheet;
  38. _googleDriveController = [[VLCGoogleDriveController alloc] init];
  39. _googleDriveController.delegate = self;
  40. [_googleDriveController startSession];
  41. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  42. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  43. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  44. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  45. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  46. self.tableView.rowHeight = [VLCGoogleDriveTableViewCell heightOfCell];
  47. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  48. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  49. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  50. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  51. _progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
  52. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  53. _downloadingBarLabel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"DOWNLOADING",@"") style:UIBarButtonItemStylePlain target:nil action:nil];
  54. [_downloadingBarLabel setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  55. [_loginToGoogleDriveButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", @"") forState:UIControlStateNormal];
  56. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  57. [self _showProgressInToolbar:NO];
  58. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  59. _activityIndicator.hidesWhenStopped = YES;
  60. [self.view addSubview:_activityIndicator];
  61. }
  62. - (GTMOAuth2ViewControllerTouch *)createAuthController
  63. {
  64. _authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeDrive
  65. clientID:kVLCGoogleDriveClientID
  66. clientSecret:kVLCGoogleDriveClientSecret
  67. keychainItemName:kKeychainItemName
  68. delegate:self
  69. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  70. return _authController;
  71. }
  72. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  73. {
  74. if (error != nil) {
  75. [self showAlert:@"Authentication Error" message:error.localizedDescription];
  76. _googleDriveController.driveService.authorizer = nil;
  77. } else {
  78. _googleDriveController.driveService.authorizer = authResult;
  79. }
  80. [self updateViewAfterSessionChange];
  81. }
  82. - (void)showAlert:(NSString *)title message:(NSString *)message
  83. {
  84. UIAlertView *alert;
  85. alert = [[UIAlertView alloc] initWithTitle: title
  86. message: message
  87. delegate: nil
  88. cancelButtonTitle: @"OK"
  89. otherButtonTitles: nil];
  90. [alert show];
  91. }
  92. - (void)viewWillAppear:(BOOL)animated
  93. {
  94. self.navigationController.toolbarHidden = NO;
  95. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  96. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  97. [self updateViewAfterSessionChange];
  98. [super viewWillAppear:animated];
  99. CGRect aiFrame = _activityIndicator.frame;
  100. CGSize tvSize = self.tableView.frame.size;
  101. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  102. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  103. _activityIndicator.frame = aiFrame;
  104. }
  105. - (void)viewWillDisappear:(BOOL)animated
  106. {
  107. self.navigationController.toolbarHidden = YES;
  108. [super viewWillDisappear:animated];
  109. }
  110. - (void)_showProgressInToolbar:(BOOL)value
  111. {
  112. if (!value)
  113. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  114. else {
  115. _progressView.progress = 0.;
  116. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  117. }
  118. }
  119. - (void)_requestInformationForCurrentPath
  120. {
  121. [_activityIndicator startAnimating];
  122. [_googleDriveController requestDirectoryListingAtPath:_currentPath];
  123. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  124. }
  125. #pragma mark - interface interaction
  126. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  127. {
  128. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  129. return NO;
  130. return YES;
  131. }
  132. - (IBAction)goBack:(id)sender
  133. {
  134. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  135. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  136. [self _requestInformationForCurrentPath];
  137. } else
  138. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  139. }
  140. #pragma mark - Table view data source
  141. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  142. {
  143. return 1;
  144. }
  145. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  146. {
  147. return _googleDriveController.currentListFiles.count;
  148. }
  149. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. static NSString *CellIdentifier = @"GoogleDriveCell";
  152. VLCGoogleDriveTableViewCell *cell = (VLCGoogleDriveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  153. if (cell == nil)
  154. cell = [VLCGoogleDriveTableViewCell cellWithReuseIdentifier:CellIdentifier];
  155. cell.driveFile = _googleDriveController.currentListFiles[indexPath.row];
  156. return cell;
  157. }
  158. #pragma mark - Table view delegate
  159. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  160. {
  161. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  162. }
  163. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  164. {
  165. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  166. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  167. /* selected item is a proper file, ask the user if s/he wants to download it */
  168. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"GOOGLE_DRIVE_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"GOOGLE_DRIVE_DL_LONG", @""), _selectedFile.title, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
  169. [alert show];
  170. } else {
  171. /* dive into subdirectory */
  172. _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.title];
  173. [self _requestInformationForCurrentPath];
  174. _selectedFile = nil;
  175. }
  176. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  177. }
  178. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  179. {
  180. if (buttonIndex == 1)
  181. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  182. _selectedFile = nil;
  183. }
  184. #pragma mark - dropbox controller delegate
  185. - (void)mediaListUpdated
  186. {
  187. [_activityIndicator stopAnimating];
  188. [self.tableView reloadData];
  189. NSUInteger count = _googleDriveController.currentListFiles.count;
  190. if (count == 0)
  191. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  192. else if (count != 1)
  193. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  194. else
  195. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  196. }
  197. - (void)operationWithProgressInformationStarted
  198. {
  199. [self _showProgressInToolbar:YES];
  200. }
  201. - (void)currentProgressInformation:(float)progress
  202. {
  203. [_progressView setProgress: progress animated:YES];
  204. }
  205. - (void)operationWithProgressInformationStopped
  206. {
  207. [self _showProgressInToolbar:NO];
  208. }
  209. #pragma mark - communication with app delegate
  210. - (void)updateViewAfterSessionChange
  211. {
  212. if (![_googleDriveController isAuthorized]) {
  213. [self _showLoginPanel];
  214. return;
  215. } else if (self.loginToGoogleDriveView.superview)
  216. [self.loginToGoogleDriveView removeFromSuperview];
  217. _currentPath = @"/";
  218. [self _requestInformationForCurrentPath];
  219. }
  220. #pragma mark - login dialog
  221. - (void)_showLoginPanel
  222. {
  223. self.loginToGoogleDriveView.frame = self.tableView.frame;
  224. [self.view addSubview:self.loginToGoogleDriveView];
  225. }
  226. - (IBAction)loginToGoogleDriveAction:(id)sender
  227. {
  228. if (![_googleDriveController isAuthorized]) {
  229. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  230. } else {
  231. [_googleDriveController logout];
  232. }
  233. }
  234. @end