VLCOneDriveTableViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #import "VLCProgressView.h"
  19. #import "UIDevice+VLC.h"
  20. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  21. {
  22. VLCOneDriveController *_oneDriveController;
  23. VLCOneDriveObject *_selectedFile;
  24. }
  25. @end
  26. @implementation VLCOneDriveTableViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
  30. self.controller = _oneDriveController;
  31. self.controller.delegate = self;
  32. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  33. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  34. [self.cloudStorageLogo sizeToFit];
  35. self.cloudStorageLogo.center = self.view.center;
  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. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  67. cell.delegate = self;
  68. return cell;
  69. }
  70. #pragma mark - table view delegate
  71. - (void)mediaListUpdated
  72. {
  73. [self.tableView reloadData];
  74. [self.activityIndicator stopAnimating];
  75. }
  76. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. VLCOneDriveObject *selectedObject = _oneDriveController.currentFolder.items[indexPath.row];
  79. if (selectedObject.isFolder) {
  80. /* dive into sub folder */
  81. [self.activityIndicator startAnimating];
  82. _oneDriveController.currentFolder = selectedObject;
  83. [_oneDriveController loadCurrentFolder];
  84. self.title = selectedObject.name;
  85. } else {
  86. /* stream file */
  87. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  88. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  89. [appDelegate openMovieFromURL:url];
  90. }
  91. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  92. }
  93. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  94. {
  95. if (buttonIndex == 1)
  96. [_oneDriveController downloadObject:_selectedFile];
  97. _selectedFile = nil;
  98. }
  99. #pragma mark - login dialog
  100. - (void)loginAction:(id)sender
  101. {
  102. if (![_oneDriveController isAuthorized]) {
  103. self.authorizationInProgress = YES;
  104. [_oneDriveController login];
  105. } else
  106. [_oneDriveController logout];
  107. }
  108. #pragma mark - onedrive controller delegation
  109. - (void)sessionWasUpdated
  110. {
  111. [self updateViewAfterSessionChange];
  112. }
  113. #pragma mark - cell delegation
  114. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  115. {
  116. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  117. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  118. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  119. /* selected item is a proper file, ask the user if s/he wants to download it */
  120. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  121. [alert show];
  122. } else {
  123. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  124. [alert show];
  125. }
  126. }
  127. @end