VLCOneDriveTableViewController.m 6.2 KB

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