VLCOneDriveTableViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "VLCCloudStorageTableViewCell.h"
  15. #import "VLCPlaybackController.h"
  16. #import "VLCProgressView.h"
  17. #import "UIDevice+VLC.h"
  18. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  19. {
  20. VLCOneDriveController *_oneDriveController;
  21. VLCOneDriveObject *_selectedFile;
  22. }
  23. @end
  24. @implementation VLCOneDriveTableViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
  28. self.controller = _oneDriveController;
  29. self.controller.delegate = self;
  30. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  31. #if TARGET_OS_IOS
  32. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  33. [self.cloudStorageLogo sizeToFit];
  34. self.cloudStorageLogo.center = self.view.center;
  35. #endif
  36. }
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. [super viewWillAppear:animated];
  40. [self updateViewAfterSessionChange];
  41. self.authorizationInProgress = NO;
  42. }
  43. #pragma mark - generic interface interaction
  44. - (void)goBack
  45. {
  46. if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
  47. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  48. _oneDriveController.currentFolder = nil;
  49. self.title = _oneDriveController.rootFolder.name;
  50. } else {
  51. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  52. self.title = _oneDriveController.currentFolder.name;
  53. }
  54. [self.activityIndicator startAnimating];
  55. [_oneDriveController loadCurrentFolder];
  56. } else
  57. [self.navigationController popViewControllerAnimated:YES];
  58. }
  59. #pragma mark - table view data source
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  61. {
  62. static NSString *CellIdentifier = @"OneDriveCell";
  63. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  64. if (cell == nil)
  65. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  66. NSArray *items = _oneDriveController.currentFolder.items;
  67. if (indexPath.row < items.count) {
  68. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  69. cell.delegate = self;
  70. }
  71. return cell;
  72. }
  73. #pragma mark - table view delegate
  74. - (void)mediaListUpdated
  75. {
  76. [self.tableView reloadData];
  77. [self.activityIndicator stopAnimating];
  78. }
  79. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81. NSArray *folderItems = _oneDriveController.currentFolder.items;
  82. NSInteger row = indexPath.row;
  83. if (row >= folderItems.count)
  84. return;
  85. VLCOneDriveObject *selectedObject = folderItems[row];
  86. if (selectedObject.isFolder) {
  87. /* dive into sub folder */
  88. [self.activityIndicator startAnimating];
  89. _oneDriveController.currentFolder = selectedObject;
  90. [_oneDriveController loadCurrentFolder];
  91. self.title = selectedObject.name;
  92. } else {
  93. /* stream file */
  94. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  95. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  96. [vpc playURL:url successCallback:nil errorCallback:nil];
  97. #if TARGET_OS_TV
  98. VLCFullscreenMovieTVViewController *movieVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  99. [self presentViewController:movieVC
  100. animated:YES
  101. completion:nil];
  102. #endif
  103. }
  104. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  105. }
  106. #pragma mark - login dialog
  107. - (void)loginAction:(id)sender
  108. {
  109. if (![_oneDriveController isAuthorized]) {
  110. self.authorizationInProgress = YES;
  111. [_oneDriveController loginWithViewController:self];
  112. } else
  113. [_oneDriveController logout];
  114. }
  115. #pragma mark - onedrive controller delegation
  116. - (void)sessionWasUpdated
  117. {
  118. [self updateViewAfterSessionChange];
  119. }
  120. #pragma mark - cell delegation
  121. #if TARGET_OS_IOS
  122. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  123. {
  124. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  125. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  126. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  127. /* selected item is a proper file, ask the user if s/he wants to download it */
  128. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  129. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  130. delegate:self
  131. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  132. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  133. [alert show];
  134. } else {
  135. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  136. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  137. delegate:self
  138. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  139. otherButtonTitles:nil];
  140. [alert show];
  141. }
  142. }
  143. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  144. {
  145. if (buttonIndex == 1)
  146. [_oneDriveController downloadObject:_selectedFile];
  147. _selectedFile = nil;
  148. }
  149. #endif
  150. @end