VLCOneDriveTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*****************************************************************************
  2. * VLCOneDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCOneDriveTableViewController.h"
  13. #import "VLCOneDriveController.h"
  14. #import "UIBarButtonItem+Theme.h"
  15. #import "VLCCloudStorageTableViewCell.h"
  16. #import "VLCAppDelegate.h"
  17. #import "VLCOneDriveController.h"
  18. #import "VLCProgressView.h"
  19. @interface VLCOneDriveTableViewController () <UITableViewDataSource, UITableViewDelegate, VLCOneDriveControllerDelegate, VLCCloudStorageTableViewCell>
  20. {
  21. UIBarButtonItem *_backButton;
  22. UIBarButtonItem *_logoutButton;
  23. UIBarButtonItem *_numberOfFilesBarButtonItem;
  24. UIBarButtonItem *_progressBarButtonItem;
  25. VLCProgressView *_progressView;
  26. UIActivityIndicatorView *_activityIndicator;
  27. VLCOneDriveController *_oneDriveController;
  28. NSString *_currentPath;
  29. VLCOneDriveObject *_selectedFile;
  30. }
  31. @end
  32. @implementation VLCOneDriveTableViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _oneDriveController = [VLCOneDriveController sharedInstance];
  36. _oneDriveController.delegate = self;
  37. self.modalPresentationStyle = UIModalPresentationFormSheet;
  38. self.navigationItem.title = @"OneDrive";
  39. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  40. self.navigationItem.leftBarButtonItem = _backButton;
  41. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  42. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  43. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  44. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  45. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  46. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ UITextAttributeFont : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  47. _progressView = [VLCProgressView new];
  48. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  49. self.cloudStorageLogo = nil;
  50. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  51. self.flatLoginButton.hidden = YES;
  52. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  53. } else {
  54. self.loginButton.hidden = YES;
  55. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  56. }
  57. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  58. [self _showProgressInToolbar:NO];
  59. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  60. _activityIndicator.hidesWhenStopped = YES;
  61. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  62. [self.view addSubview:_activityIndicator];
  63. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  64. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  65. }
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. self.navigationController.toolbarHidden = NO;
  69. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  70. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  71. [super viewWillAppear:animated];
  72. if (_oneDriveController.activeSession)
  73. [_oneDriveController login];
  74. if (!_oneDriveController.userAuthenticated)
  75. [self _showLoginDialog];
  76. [self.cloudStorageLogo sizeToFit];
  77. self.cloudStorageLogo.center = self.view.center;
  78. }
  79. - (void)viewWillDisappear:(BOOL)animated
  80. {
  81. self.navigationController.toolbarHidden = YES;
  82. [super viewWillDisappear:animated];
  83. }
  84. #pragma mark - generic interface interaction
  85. - (IBAction)goBack:(id)sender
  86. {
  87. if (_oneDriveController.rootFolder != _oneDriveController.currentFolder) {
  88. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  89. _oneDriveController.currentFolder = nil;
  90. self.title = _oneDriveController.rootFolder.name;
  91. } else {
  92. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  93. self.title = _oneDriveController.currentFolder.name;
  94. }
  95. [_activityIndicator startAnimating];
  96. [_oneDriveController loadCurrentFolder];
  97. } else
  98. [self.navigationController popViewControllerAnimated:YES];
  99. }
  100. #pragma mark - table view data source
  101. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  102. {
  103. return _oneDriveController.currentFolder.items.count;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. static NSString *CellIdentifier = @"OneDriveCell";
  108. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  109. if (cell == nil)
  110. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  111. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  112. cell.delegate = self;
  113. return cell;
  114. }
  115. #pragma mark - table view delegate
  116. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  119. }
  120. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];
  123. if (selectedObject.isFolder) {
  124. /* dive into sub folder */
  125. [_activityIndicator startAnimating];
  126. _oneDriveController.currentFolder = selectedObject;
  127. [_oneDriveController loadCurrentFolder];
  128. self.title = selectedObject.name;
  129. } else {
  130. /* stream file */
  131. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  132. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  133. [appDelegate openMovieFromURL:url];
  134. }
  135. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  136. }
  137. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  138. {
  139. if (buttonIndex == 1)
  140. [_oneDriveController downloadObject:_selectedFile];
  141. _selectedFile = nil;
  142. }
  143. #pragma mark - login dialog
  144. - (void)logout
  145. {
  146. [_oneDriveController logout];
  147. [self updateViewAfterSessionChange];
  148. }
  149. - (void)_showLoginDialog
  150. {
  151. self.loginToCloudStorageView.frame = self.tableView.frame;
  152. self.navigationItem.rightBarButtonItem = nil;
  153. [self.view addSubview:self.loginToCloudStorageView];
  154. }
  155. - (void)loginAction:(id)sender
  156. {
  157. [_oneDriveController login];
  158. }
  159. #pragma mark - onedrive controller delegation
  160. - (void)mediaListUpdated
  161. {
  162. [_activityIndicator stopAnimating];
  163. [self.tableView reloadData];
  164. NSUInteger count = _oneDriveController.currentFolder.items.count;
  165. if (count == 0)
  166. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  167. else if (count != 1)
  168. _numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  169. else
  170. _numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  171. }
  172. - (void)sessionWasUpdated
  173. {
  174. [self updateViewAfterSessionChange];
  175. }
  176. #pragma mark - download visualization
  177. - (void)_showProgressInToolbar:(BOOL)value
  178. {
  179. if (!value)
  180. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  181. else {
  182. _progressView.progressBar.progress = 0.;
  183. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  184. }
  185. }
  186. - (void)operationWithProgressInformationStarted
  187. {
  188. [self _showProgressInToolbar:YES];
  189. }
  190. - (void)currentProgressInformation:(float)progress
  191. {
  192. [_progressView.progressBar setProgress:progress animated:YES];
  193. }
  194. - (void)updateRemainingTime:(NSString *)time
  195. {
  196. [_progressView updateTime:time];
  197. }
  198. - (void)operationWithProgressInformationStopped
  199. {
  200. [self _showProgressInToolbar:NO];
  201. }
  202. #pragma mark - app delegate
  203. - (void)updateViewAfterSessionChange
  204. {
  205. self.navigationItem.rightBarButtonItem = _logoutButton;
  206. if (![_oneDriveController userAuthenticated]) {
  207. [self _showLoginDialog];
  208. return;
  209. } else if (self.loginToCloudStorageView.superview) {
  210. [self.loginToCloudStorageView removeFromSuperview];
  211. }
  212. if (_oneDriveController.currentFolder != nil)
  213. [self mediaListUpdated];
  214. else {
  215. [_activityIndicator startAnimating];
  216. [_oneDriveController loadCurrentFolder];
  217. }
  218. }
  219. #pragma mark - cell delegationx
  220. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  221. {
  222. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  223. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  224. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  225. [alert show];
  226. }
  227. @end