VLCCloudStorageTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*****************************************************************************
  2. * VLCCloudStorageTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2019 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Fabio Ritrovato <sephiroth87 # videolan.org>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCCloudStorageTableViewController.h"
  15. #import "VLCCloudStorageTableViewCell.h"
  16. #import "VLCProgressView.h"
  17. #import "VLC-Swift.h"
  18. @interface VLCCloudStorageTableViewController()
  19. {
  20. VLCProgressView *_progressView;
  21. VLCActionSheet *sheet;
  22. VLCCloudSortingSpecifierManager *manager;
  23. UIRefreshControl *_refreshControl;
  24. UIBarButtonItem *_progressBarButtonItem;
  25. UIBarButtonItem *_logoutButton;
  26. UINavigationController *tempNav;
  27. }
  28. @end
  29. @implementation VLCCloudStorageTableViewController
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. _authorizationInProgress = NO;
  34. self.modalPresentationStyle = UIModalPresentationFormSheet;
  35. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_BACK", nil) style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
  36. self.navigationItem.leftBarButtonItem = backButton;
  37. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStylePlain target:self action:@selector(logout)];
  38. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateForTheme) name:kVLCThemeDidChangeNotification object:nil];
  40. _refreshControl = [[UIRefreshControl alloc] init];
  41. _refreshControl.tintColor = [UIColor whiteColor];
  42. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  43. [self.tableView addSubview:_refreshControl];
  44. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  45. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  46. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  47. _sortBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: [NSString stringWithFormat:NSLocalizedString(@"SORT", nil), 0]
  48. style:UIBarButtonItemStylePlain target:self action:@selector(sortButtonClicked:)];
  49. _sortBarButtonItem.tintColor = PresentationTheme.current.colors.orangeUI;
  50. _numberOfFilesBarButtonItem.tintColor = PresentationTheme.current.colors.orangeUI;
  51. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  52. _activityIndicator.hidesWhenStopped = YES;
  53. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  54. [self.view addSubview:_activityIndicator];
  55. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  56. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  57. _progressView = [VLCProgressView new];
  58. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  59. _progressView.tintColor = PresentationTheme.current.colors.orangeUI;
  60. _progressView.progressLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  61. sheet = [[VLCActionSheet alloc] init];
  62. manager = [[VLCCloudSortingSpecifierManager alloc] initWithController: self];
  63. sheet.dataSource = manager;
  64. sheet.delegate = manager;
  65. sheet.modalPresentationStyle = UIModalPresentationCustom;
  66. [sheet.collectionView registerClass:[VLCActionSheetCell class] forCellWithReuseIdentifier:VLCActionSheetCell.identifier];
  67. [self _showProgressInToolbar:NO];
  68. [self updateForTheme];
  69. }
  70. - (void)updateForTheme
  71. {
  72. self.tableView.separatorColor = PresentationTheme.current.colors.background;
  73. self.tableView.backgroundColor = PresentationTheme.current.colors.background;
  74. self.view.backgroundColor = PresentationTheme.current.colors.background;
  75. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  76. _activityIndicator.activityIndicatorViewStyle = PresentationTheme.current == PresentationTheme.brightTheme ? UIActivityIndicatorViewStyleGray : UIActivityIndicatorViewStyleWhiteLarge;
  77. self.loginToCloudStorageView.backgroundColor = PresentationTheme.current.colors.background;
  78. self.navigationController.toolbar.barStyle = PresentationTheme.current.colors.toolBarStyle;
  79. _progressView.progressLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  80. }
  81. - (void)viewWillAppear:(BOOL)animated
  82. {
  83. //Workaround since in viewWillDisappear self.navigationController can be nil which will lead to a lingering toolbar
  84. tempNav = self.navigationController;
  85. tempNav.toolbarHidden = NO;
  86. [super viewWillAppear:animated];
  87. }
  88. - (void)viewWillDisappear:(BOOL)animated
  89. {
  90. tempNav.toolbarHidden = YES;
  91. tempNav = nil;
  92. [super viewWillDisappear:animated];
  93. }
  94. -(void)handleRefresh
  95. {
  96. //set the title while refreshing
  97. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  98. //set the date and time of refreshing
  99. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  100. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  101. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  102. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  103. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  104. [self requestInformationForCurrentPath];
  105. }
  106. - (void)requestInformationForCurrentPath
  107. {
  108. [_activityIndicator startAnimating];
  109. [self.controller requestDirectoryListingAtPath:self.currentPath];
  110. }
  111. - (void)mediaListUpdated
  112. {
  113. [_activityIndicator stopAnimating];
  114. [_refreshControl endRefreshing];
  115. [self.tableView reloadData];
  116. NSUInteger count = self.controller.currentListFiles.count;
  117. if (count == 0)
  118. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  119. else if (count != 1)
  120. self.numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  121. else
  122. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  123. }
  124. - (NSArray*)_generateToolbarItemsWithSortButton : (BOOL)withsb
  125. {
  126. NSMutableArray* result = [NSMutableArray array];
  127. if (withsb)
  128. {
  129. [result addObjectsFromArray:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _sortBarButtonItem]];
  130. }
  131. [result addObjectsFromArray:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]]];
  132. return result;
  133. }
  134. - (void)_showProgressInToolbar:(BOOL)value
  135. {
  136. if (!value) {
  137. [self setToolbarItems:[self _generateToolbarItemsWithSortButton:[self.controller supportSorting]] animated:YES];
  138. }
  139. else {
  140. _progressView.progressBar.progress = 0.;
  141. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  142. }
  143. }
  144. - (void)updateRemainingTime:(NSString *)time
  145. {
  146. [_progressView updateTime:time];
  147. }
  148. - (void)currentProgressInformation:(CGFloat)progress
  149. {
  150. [_progressView.progressBar setProgress:progress animated:YES];
  151. }
  152. - (void)operationWithProgressInformationStarted
  153. {
  154. [self _showProgressInToolbar:YES];
  155. }
  156. - (void)operationWithProgressInformationStopped
  157. {
  158. [self _showProgressInToolbar:NO];
  159. }
  160. #pragma mark - UITableViewDataSources
  161. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  162. {
  163. return self.controller.currentListFiles.count;
  164. }
  165. #pragma mark - UITableViewDelegate
  166. - (void)tableView:(UITableView *)tableView willDisplayCell:(__kindof UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. VLCCloudStorageTableViewCell *cloudcell = [cell isKindOfClass:VLCCloudStorageTableViewCell.class] ? (id)cell : nil;
  169. cloudcell.backgroundColor = PresentationTheme.current.colors.cellBackgroundA;
  170. cloudcell.titleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  171. cloudcell.folderTitleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  172. cloudcell.subtitleLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  173. }
  174. - (void)goBack
  175. {
  176. if (((![self.currentPath isEqualToString:@""] && ![self.currentPath isEqualToString:@"/"]) && [self.currentPath length] > 0) && [self.controller isAuthorized]){
  177. self.currentPath = [self.currentPath stringByDeletingLastPathComponent];
  178. [self requestInformationForCurrentPath];
  179. } else
  180. [self.navigationController popViewControllerAnimated:YES];
  181. }
  182. - (void)showLoginPanel
  183. {
  184. self.loginToCloudStorageView.frame = self.tableView.frame;
  185. self.navigationItem.rightBarButtonItem = nil;
  186. [self.tableView addSubview:self.loginToCloudStorageView];
  187. }
  188. - (void)updateViewAfterSessionChange
  189. {
  190. BOOL hasProgressbar = NO;
  191. for (id item in self.toolbarItems) {
  192. if (item == _progressBarButtonItem) {
  193. hasProgressbar = YES;
  194. }
  195. }
  196. if (!hasProgressbar) {
  197. //Only show sorting button and number of files button when there is no progress bar in the toolbar
  198. //Only show sorting button when controller support sorting and is authorized
  199. [self setToolbarItems:[self _generateToolbarItemsWithSortButton:self.controller.isAuthorized && [self.controller supportSorting]] animated:YES];
  200. }
  201. if (self.controller.canPlayAll) {
  202. self.navigationItem.rightBarButtonItems = @[_logoutButton, [UIBarButtonItem themedPlayAllButtonWithTarget:self andSelector:@selector(playAllAction:)]];
  203. } else {
  204. self.navigationItem.rightBarButtonItem = _logoutButton;
  205. }
  206. if (_authorizationInProgress || [self.controller isAuthorized]) {
  207. if (self.loginToCloudStorageView.superview) {
  208. [self.loginToCloudStorageView removeFromSuperview];
  209. }
  210. }
  211. if (![self.controller isAuthorized]) {
  212. [_activityIndicator stopAnimating];
  213. [self showLoginPanel];
  214. return;
  215. }
  216. //reload if we didn't come back from streaming
  217. if (self.currentPath == nil) {
  218. self.currentPath = @"";
  219. }
  220. if ([self.controller.currentListFiles count] == 0)
  221. [self requestInformationForCurrentPath];
  222. }
  223. - (void)logout
  224. {
  225. [self.controller logout];
  226. [self updateViewAfterSessionChange];
  227. }
  228. - (void)sortButtonClicked:(UIBarButtonItem*)sender
  229. {
  230. [self presentViewController:self->sheet animated:YES completion:^{
  231. [self->sheet.collectionView selectItemAtIndexPath:self->manager.selectedIndex animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  232. }];
  233. }
  234. - (IBAction)loginAction:(id)sender
  235. {
  236. }
  237. - (IBAction)playAllAction:(id)sender
  238. {
  239. }
  240. @end