VLCOneDriveTableViewController.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];
  81. if (selectedObject.isFolder) {
  82. /* dive into sub folder */
  83. [self.activityIndicator startAnimating];
  84. _oneDriveController.currentFolder = selectedObject;
  85. [_oneDriveController loadCurrentFolder];
  86. self.title = selectedObject.name;
  87. } else {
  88. /* stream file */
  89. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  90. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  91. [vpc playURL:url successCallback:nil errorCallback:nil];
  92. }
  93. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  94. }
  95. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  96. {
  97. if (buttonIndex == 1)
  98. [_oneDriveController downloadObject:_selectedFile];
  99. _selectedFile = nil;
  100. }
  101. #pragma mark - login dialog
  102. - (void)loginAction:(id)sender
  103. {
  104. if (![_oneDriveController isAuthorized]) {
  105. self.authorizationInProgress = YES;
  106. [_oneDriveController loginWithViewController:self];
  107. } else
  108. [_oneDriveController logout];
  109. }
  110. #pragma mark - onedrive controller delegation
  111. - (void)sessionWasUpdated
  112. {
  113. [self updateViewAfterSessionChange];
  114. }
  115. #pragma mark - cell delegation
  116. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  117. {
  118. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  119. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  120. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  121. /* selected item is a proper file, ask the user if s/he wants to download it */
  122. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  123. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  124. delegate:self
  125. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  126. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  127. [alert show];
  128. } else {
  129. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  130. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  131. delegate:self
  132. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  133. otherButtonTitles:nil];
  134. [alert show];
  135. }
  136. }
  137. @end