VLCDropboxTableViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*****************************************************************************
  2. * VLCDropboxTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. * Fabio Ritrovato <sephiroth87 # videolan.org>
  12. * Tamas Timar <ttimar.vlc # gmail.com>
  13. *
  14. * Refer to the COPYING file of the official project for license.
  15. *****************************************************************************/
  16. #import "VLCDropboxTableViewController.h"
  17. #import "VLCDropboxController.h"
  18. #import "VLCCloudStorageTableViewCell.h"
  19. #import "UIDevice+VLC.h"
  20. #import "VLCAppDelegate.h"
  21. #import "VLC-Swift.h"
  22. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
  23. {
  24. VLCDropboxController *_dropboxController;
  25. DBFILESMetadata *_selectedFile;
  26. NSArray *_mediaList;
  27. }
  28. @end
  29. @implementation VLCDropboxTableViewController
  30. - (instancetype)initWithPath:(NSString *)path
  31. {
  32. self = [super init];
  33. if (self) {
  34. self.currentPath = path;
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. _dropboxController = [VLCDropboxController sharedInstance];
  42. self.controller = _dropboxController;
  43. self.controller.delegate = self;
  44. #if TARGET_OS_IOS
  45. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  46. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  47. [self.cloudStorageLogo sizeToFit];
  48. self.cloudStorageLogo.center = self.view.center;
  49. #else
  50. self.title = @"Dropbox";
  51. #endif
  52. }
  53. - (void)viewWillAppear:(BOOL)animated
  54. {
  55. [super viewWillAppear:animated];
  56. if (@available(iOS 11.0, *)) {
  57. self.navigationController.navigationBar.prefersLargeTitles = NO;
  58. }
  59. self.controller = [VLCDropboxController sharedInstance];
  60. self.controller.delegate = self;
  61. if (self.currentPath != nil)
  62. self.title = self.currentPath.lastPathComponent;
  63. [self updateViewAfterSessionChange];
  64. [self.tableView reloadData];
  65. }
  66. #pragma mark - interface interaction
  67. - (BOOL)shouldAutorotate
  68. {
  69. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  70. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  71. return NO;
  72. return YES;
  73. }
  74. #pragma mark - Table view data source
  75. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. static NSString *CellIdentifier = @"DropboxCell";
  78. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  79. if (cell == nil)
  80. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  81. NSUInteger index = indexPath.row;
  82. if (_mediaList) {
  83. if (index < _mediaList.count) {
  84. cell.dropboxFile = _mediaList[index];
  85. cell.delegate = self;
  86. }
  87. }
  88. return cell;
  89. }
  90. - (void)mediaListUpdated
  91. {
  92. _mediaList = [self.controller.currentListFiles copy];
  93. [super mediaListUpdated];
  94. }
  95. #pragma mark - Table view delegate
  96. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. _selectedFile = _mediaList[indexPath.row];
  99. if (![_selectedFile isKindOfClass:[DBFILESFolderMetadata class]])
  100. [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
  101. else {
  102. /* dive into subdirectory */
  103. NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.name];
  104. self.currentPath = futurePath;
  105. [self requestInformationForCurrentPath];
  106. }
  107. _selectedFile = nil;
  108. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  109. }
  110. #pragma mark - login dialog
  111. - (IBAction)loginAction:(id)sender
  112. {
  113. if (!_dropboxController.isAuthorized) {
  114. self.authorizationInProgress = YES;
  115. [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
  116. controller:self
  117. openURL:^(NSURL *url) {
  118. [[UIApplication sharedApplication] openURL:url];
  119. }];
  120. } else
  121. [_dropboxController logout];
  122. }
  123. - (void)sessionWasUpdated:(NSNotification *)aNotification
  124. {
  125. self.authorizationInProgress = YES;
  126. [self updateViewAfterSessionChange];
  127. [_dropboxController shareCredentials];
  128. }
  129. #pragma mark - VLCCloudStorageTableViewCell delegation
  130. #if TARGET_OS_IOS
  131. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  132. {
  133. _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
  134. if (((DBFILESFileMetadata *)_selectedFile).size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  135. /* selected item is a proper file, ask the user if s/he wants to download it */
  136. NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  137. style: UIAlertActionStyleCancel
  138. action: ^(UIAlertAction *action) {
  139. self->_selectedFile = nil;
  140. }],
  141. [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  142. action: ^(UIAlertAction *action) {
  143. [self->_dropboxController downloadFileToDocumentFolder:self->_selectedFile];
  144. self->_selectedFile = nil;
  145. }]
  146. ];
  147. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  148. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  149. viewController:self
  150. buttonsAction:buttonsAction];
  151. } else {
  152. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  153. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  154. viewController:self];
  155. }
  156. }
  157. #endif
  158. @end