VLCOneDriveTableViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*****************************************************************************
  2. * VLCOneDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre Sagaspe <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCOneDriveTableViewController.h"
  14. #import "VLCOneDriveController.h"
  15. #import "VLCCloudStorageTableViewCell.h"
  16. #import "VLCPlaybackController.h"
  17. #import "VLCProgressView.h"
  18. #import "UIDevice+VLC.h"
  19. #import "NSString+SupportedMedia.h"
  20. #import "VLCConstants.h"
  21. #import "VLC-Swift.h"
  22. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  23. {
  24. VLCOneDriveController *_oneDriveController;
  25. VLCOneDriveObject *_selectedFile;
  26. }
  27. @end
  28. @implementation VLCOneDriveTableViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
  32. self.controller = _oneDriveController;
  33. self.controller.delegate = self;
  34. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  35. #if TARGET_OS_IOS
  36. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  37. [self.cloudStorageLogo sizeToFit];
  38. self.cloudStorageLogo.center = self.view.center;
  39. #endif
  40. }
  41. - (void)viewWillAppear:(BOOL)animated
  42. {
  43. [super viewWillAppear:animated];
  44. [self updateViewAfterSessionChange];
  45. self.authorizationInProgress = NO;
  46. }
  47. #pragma mark - generic interface interaction
  48. - (void)goBack
  49. {
  50. if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
  51. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  52. _oneDriveController.currentFolder = nil;
  53. self.title = _oneDriveController.rootFolder.name;
  54. } else {
  55. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  56. self.title = _oneDriveController.currentFolder.name;
  57. }
  58. [self.activityIndicator startAnimating];
  59. [_oneDriveController loadCurrentFolder];
  60. } else
  61. [self.navigationController popViewControllerAnimated:YES];
  62. }
  63. #pragma mark - table view data source
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. static NSString *CellIdentifier = @"OneDriveCell";
  67. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  68. if (cell == nil)
  69. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  70. NSArray *items = _oneDriveController.currentFolder.items;
  71. if (indexPath.row < items.count) {
  72. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  73. cell.delegate = self;
  74. }
  75. return cell;
  76. }
  77. #pragma mark - table view delegate
  78. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  79. {
  80. NSArray *folderItems = _oneDriveController.currentFolder.items;
  81. NSInteger row = indexPath.row;
  82. if (row >= folderItems.count)
  83. return;
  84. VLCOneDriveObject *selectedObject = folderItems[row];
  85. if (selectedObject.isFolder) {
  86. /* dive into sub folder */
  87. [self.activityIndicator startAnimating];
  88. _oneDriveController.currentFolder = selectedObject;
  89. [_oneDriveController loadCurrentFolder];
  90. self.title = selectedObject.name;
  91. } else {
  92. if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
  93. /* stream file */
  94. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  95. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:@[[VLCMedia mediaWithURL:url]]];
  96. [self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:selectedObject.subtitleURL];
  97. } else {
  98. NSInteger posIndex = 0;
  99. NSUInteger counter = 0;
  100. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  101. for (VLCOneDriveObject *item in folderItems) {
  102. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  103. continue;
  104. NSURL *url = [NSURL URLWithString:item.downloadPath];
  105. if (url) {
  106. [mediaList addMedia:[VLCMedia mediaWithURL:url]];
  107. if (item.subtitleURL)
  108. [[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
  109. counter ++;
  110. if (item == selectedObject)
  111. posIndex = mediaList.count - 1;
  112. }
  113. }
  114. if (mediaList.count > 0)
  115. [self streamMediaList:mediaList startingAtIndex:posIndex subtitlesFilePath:nil];
  116. }
  117. }
  118. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  119. }
  120. - (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex subtitlesFilePath:(NSString *)subtitlesFilePath
  121. {
  122. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  123. vpc.fullscreenSessionRequested = NO;
  124. [vpc playMediaList:mediaList firstIndex:startIndex subtitlesFilePath:subtitlesFilePath];
  125. }
  126. - (void)playAllAction:(id)sender
  127. {
  128. NSUInteger counter = 0;
  129. NSArray *folderItems = _oneDriveController.currentFolder.items;
  130. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  131. for (VLCOneDriveObject *item in folderItems) {
  132. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  133. continue;
  134. NSURL *url = [NSURL URLWithString:item.downloadPath];
  135. if (url) {
  136. [mediaList addMedia:[VLCMedia mediaWithURL:url]];
  137. if (item.subtitleURL)
  138. [[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
  139. counter ++;
  140. }
  141. }
  142. if (mediaList.count > 0)
  143. [self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:nil];
  144. }
  145. #pragma mark - login dialog
  146. - (void)loginAction:(id)sender
  147. {
  148. if (![_oneDriveController isAuthorized]) {
  149. self.authorizationInProgress = YES;
  150. [_oneDriveController loginWithViewController:self];
  151. } else
  152. [_oneDriveController logout];
  153. }
  154. #pragma mark - onedrive controller delegation
  155. - (void)sessionWasUpdated
  156. {
  157. [self updateViewAfterSessionChange];
  158. }
  159. #pragma mark - cell delegation
  160. #if TARGET_OS_IOS
  161. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  162. {
  163. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  164. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  165. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  166. /* selected item is a proper file, ask the user if s/he wants to download it */
  167. NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  168. action: ^(UIAlertAction* action){
  169. self->_selectedFile = nil;
  170. }],
  171. [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  172. action: ^(UIAlertAction* action){
  173. [self->_oneDriveController downloadObject:self->_selectedFile];
  174. self->_selectedFile = nil;
  175. }]];
  176. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  177. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  178. viewController:self
  179. buttonsAction:buttonsAction];
  180. } else {
  181. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  182. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  183. viewController:self
  184. buttonsAction:@[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_OK", nil)
  185. action: ^(UIAlertAction* action){
  186. self->_selectedFile = nil;
  187. }]]];
  188. }
  189. }
  190. #endif
  191. @end