VLCGoogleDriveTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static NSString *const kKeychainItemName = @"Google Drive Quickstart #3";
  19. @interface VLCGoogleDriveTableViewController ()
  20. {
  21. VLCGoogleDriveController *_googleDriveController;
  22. GTMOAuth2ViewControllerTouch *_authController;
  23. NSString *_currentPath;
  24. UIBarButtonItem *_backButton;
  25. UIBarButtonItem *_backToMenuButton;
  26. UIBarButtonItem *_numberOfFilesBarButtonItem;
  27. UIBarButtonItem *_progressBarButtonItem;
  28. UIBarButtonItem *_downloadingBarLabel;
  29. UIProgressView *_progressView;
  30. UIActivityIndicatorView *_activityIndicator;
  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:kGTLAuthScopeDriveFile
  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
  73. finishedWithAuth:(GTMOAuth2Authentication *)authResult
  74. error:(NSError *)error
  75. {
  76. if (error != nil)
  77. {
  78. [self showAlert:@"Authentication Error" message:error.localizedDescription];
  79. _googleDriveController.driveService.authorizer = nil;
  80. }
  81. else
  82. {
  83. _googleDriveController.driveService.authorizer = authResult;
  84. }
  85. [self updateViewAfterSessionChange];
  86. }
  87. - (void)showAlert:(NSString *)title message:(NSString *)message
  88. {
  89. UIAlertView *alert;
  90. alert = [[UIAlertView alloc] initWithTitle: title
  91. message: message
  92. delegate: nil
  93. cancelButtonTitle: @"OK"
  94. otherButtonTitles: nil];
  95. [alert show];
  96. }
  97. - (void)viewWillAppear:(BOOL)animated
  98. {
  99. self.navigationController.toolbarHidden = NO;
  100. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  101. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  102. [self updateViewAfterSessionChange];
  103. [super viewWillAppear:animated];
  104. CGRect aiFrame = _activityIndicator.frame;
  105. CGSize tvSize = self.tableView.frame.size;
  106. aiFrame.origin.x = (tvSize.width - aiFrame.size.width) / 2.;
  107. aiFrame.origin.y = (tvSize.height - aiFrame.size.height) / 2.;
  108. _activityIndicator.frame = aiFrame;
  109. }
  110. - (void)viewWillDisappear:(BOOL)animated
  111. {
  112. self.navigationController.toolbarHidden = YES;
  113. [super viewWillDisappear:animated];
  114. }
  115. - (void)_showProgressInToolbar:(BOOL)value
  116. {
  117. if (!value)
  118. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  119. else {
  120. _progressView.progress = 0.;
  121. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _downloadingBarLabel, _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  122. }
  123. }
  124. - (void)_requestInformationForCurrentPath
  125. {
  126. [_activityIndicator startAnimating];
  127. [_googleDriveController requestDirectoryListingAtPath:_currentPath];
  128. self.navigationItem.leftBarButtonItem = ![_currentPath isEqualToString:@"/"] ? _backButton : _backToMenuButton;
  129. }
  130. #pragma mark - interface interaction
  131. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  132. {
  133. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  134. return NO;
  135. return YES;
  136. }
  137. - (IBAction)goBack:(id)sender
  138. {
  139. if (![_currentPath isEqualToString:@"/"] && [_currentPath length] > 0) {
  140. _currentPath = [_currentPath stringByDeletingLastPathComponent];
  141. [self _requestInformationForCurrentPath];
  142. } else
  143. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  144. }
  145. #pragma mark - Table view data source
  146. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  147. {
  148. return 1;
  149. }
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  151. {
  152. return _googleDriveController.currentListFiles.count;
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. static NSString *CellIdentifier = @"GoogleDriveCell";
  157. VLCGoogleDriveTableViewCell *cell = (VLCGoogleDriveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  158. if (cell == nil)
  159. cell = [VLCGoogleDriveTableViewCell cellWithReuseIdentifier:CellIdentifier];
  160. // cell.fileMetadata = _googleDriveController.currentListFiles[indexPath.row];
  161. return cell;
  162. }
  163. #pragma mark - Table view delegate
  164. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  165. {
  166. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  167. }
  168. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. // _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  171. // if (!_selectedFile.isDirectory) {
  172. // /* selected item is a proper file, ask the user if s/he wants to download it */
  173. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"GOOGLE_DRIVE_DOWNLOAD", @"") message:[NSString stringWithFormat:NSLocalizedString(@"GOOGLE_DRIVE_DL_LONG", @""), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", @""), nil];
  174. // [alert show];
  175. // } else {
  176. // /* dive into subdirectory */
  177. // _currentPath = [_currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  178. // [self _requestInformationForCurrentPath];
  179. // _selectedFile = nil;
  180. // }
  181. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  182. }
  183. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  184. {
  185. // if (buttonIndex == 1)
  186. // [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  187. // _selectedFile = nil;
  188. }
  189. #pragma mark - dropbox controller delegate
  190. - (void)mediaListUpdated
  191. {
  192. [_activityIndicator stopAnimating];
  193. [self.tableView reloadData];
  194. NSUInteger count = _googleDriveController.currentListFiles.count;
  195. if (count == 0)
  196. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", @"");
  197. else if (count != 1)
  198. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", @""), count];
  199. else
  200. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", @"");
  201. }
  202. - (void)operationWithProgressInformationStarted
  203. {
  204. [self _showProgressInToolbar:YES];
  205. }
  206. - (void)currentProgressInformation:(float)progress
  207. {
  208. [_progressView setProgress: progress animated:YES];
  209. }
  210. - (void)operationWithProgressInformationStopped
  211. {
  212. [self _showProgressInToolbar:NO];
  213. }
  214. #pragma mark - communication with app delegate
  215. - (void)updateViewAfterSessionChange
  216. {
  217. if (![_googleDriveController isAuthorized]) {
  218. [self _showLoginPanel];
  219. return;
  220. } else if (self.loginToGoogleDriveView.superview)
  221. [self.loginToGoogleDriveView removeFromSuperview];
  222. _currentPath = @"/";
  223. [self _requestInformationForCurrentPath];
  224. }
  225. #pragma mark - login dialog
  226. - (void)_showLoginPanel
  227. {
  228. self.loginToGoogleDriveView.frame = self.tableView.frame;
  229. [self.view addSubview:self.loginToGoogleDriveView];
  230. }
  231. - (IBAction)loginToGoogleDriveAction:(id)sender
  232. {
  233. if (![_googleDriveController isAuthorized]) {
  234. _googleDriveController.isAuthorized = NO;
  235. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  236. } else {
  237. _googleDriveController.isAuthorized = YES;
  238. [_googleDriveController logout];
  239. }
  240. }
  241. @end