VLCOneDriveTableViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. @interface VLCOneDriveTableViewController () <UITableViewDataSource, UITableViewDelegate, VLCOneDriveControllerDelegate, VLCCloudStorageTableViewCell>
  19. {
  20. UIBarButtonItem *_backButton;
  21. UIBarButtonItem *_logoutButton;
  22. UIActivityIndicatorView *_activityIndicator;
  23. VLCOneDriveController *_oneDriveController;
  24. NSString *_currentPath;
  25. }
  26. @end
  27. @implementation VLCOneDriveTableViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. _oneDriveController = [VLCOneDriveController sharedInstance];
  31. _oneDriveController.delegate = self;
  32. self.modalPresentationStyle = UIModalPresentationFormSheet;
  33. self.navigationItem.title = @"OneDrive";
  34. _backButton = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  35. self.navigationItem.leftBarButtonItem = _backButton;
  36. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
  37. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  38. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  39. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  40. self.cloudStorageLogo = nil;
  41. if (!SYSTEM_RUNS_IOS7_OR_LATER) {
  42. self.flatLoginButton.hidden = YES;
  43. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  44. } else {
  45. self.loginButton.hidden = YES;
  46. [self.flatLoginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  47. }
  48. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  49. _activityIndicator.hidesWhenStopped = YES;
  50. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  51. [self.view addSubview:_activityIndicator];
  52. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  53. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated
  56. {
  57. [super viewWillAppear:animated];
  58. if (_oneDriveController.activeSession)
  59. [_oneDriveController login];
  60. if (!_oneDriveController.userAuthenticated)
  61. [self _showLoginDialog];
  62. [self.cloudStorageLogo sizeToFit];
  63. self.cloudStorageLogo.center = self.view.center;
  64. }
  65. #pragma mark - generic interface interaction
  66. - (IBAction)goBack:(id)sender
  67. {
  68. if (_oneDriveController.rootFolder != _oneDriveController.currentFolder) {
  69. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  70. _oneDriveController.currentFolder = nil;
  71. self.title = _oneDriveController.rootFolder.name;
  72. } else {
  73. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  74. self.title = _oneDriveController.currentFolder.name;
  75. }
  76. [_activityIndicator startAnimating];
  77. [_oneDriveController loadCurrentFolder];
  78. } else
  79. [self.navigationController popViewControllerAnimated:YES];
  80. }
  81. #pragma mark - table view data source
  82. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  83. {
  84. return _oneDriveController.currentFolder.items.count;
  85. }
  86. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. static NSString *CellIdentifier = @"OneDriveCell";
  89. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  90. if (cell == nil)
  91. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  92. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  93. cell.delegate = self;
  94. return cell;
  95. }
  96. #pragma mark - table view delegate
  97. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  100. }
  101. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];
  104. if (selectedObject.isFolder) {
  105. /* dive into sub folder */
  106. [_activityIndicator startAnimating];
  107. _oneDriveController.currentFolder = selectedObject;
  108. [_oneDriveController loadCurrentFolder];
  109. self.title = selectedObject.name;
  110. } else {
  111. /* stream file */
  112. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  113. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  114. [appDelegate openMovieFromURL:url];
  115. }
  116. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  117. }
  118. #pragma mark - login dialog
  119. - (void)logout
  120. {
  121. [_oneDriveController logout];
  122. [self updateViewAfterSessionChange];
  123. }
  124. - (void)_showLoginDialog
  125. {
  126. self.loginToCloudStorageView.frame = self.tableView.frame;
  127. self.navigationItem.rightBarButtonItem = nil;
  128. [self.view addSubview:self.loginToCloudStorageView];
  129. }
  130. - (void)loginAction:(id)sender
  131. {
  132. [_oneDriveController login];
  133. }
  134. #pragma mark - onedrive controller delegation
  135. - (void)mediaListUpdated
  136. {
  137. [_activityIndicator stopAnimating];
  138. [self.tableView reloadData];
  139. }
  140. - (void)sessionWasUpdated
  141. {
  142. [self updateViewAfterSessionChange];
  143. }
  144. #pragma mark - app delegate
  145. - (void)updateViewAfterSessionChange
  146. {
  147. self.navigationItem.rightBarButtonItem = _logoutButton;
  148. if (![_oneDriveController userAuthenticated]) {
  149. [self _showLoginDialog];
  150. return;
  151. } else if (self.loginToCloudStorageView.superview) {
  152. [self.loginToCloudStorageView removeFromSuperview];
  153. }
  154. if (_oneDriveController.currentFolder != nil)
  155. [self mediaListUpdated];
  156. else {
  157. [_activityIndicator startAnimating];
  158. [_oneDriveController loadCurrentFolder];
  159. }
  160. }
  161. #pragma mark - cell delegationx
  162. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  163. {
  164. }
  165. @end