VLCCloudStorageTableViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. - (UIStatusBarStyle)preferredStatusBarStyle
  89. {
  90. return PresentationTheme.current.colors.statusBarStyle;
  91. }
  92. - (void)viewWillDisappear:(BOOL)animated
  93. {
  94. tempNav.toolbarHidden = YES;
  95. tempNav = nil;
  96. [super viewWillDisappear:animated];
  97. }
  98. -(void)handleRefresh
  99. {
  100. //set the title while refreshing
  101. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  102. //set the date and time of refreshing
  103. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  104. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  105. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  106. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  107. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  108. [self requestInformationForCurrentPath];
  109. }
  110. - (void)requestInformationForCurrentPath
  111. {
  112. [_activityIndicator startAnimating];
  113. [self.controller requestDirectoryListingAtPath:self.currentPath];
  114. }
  115. - (void)mediaListUpdated
  116. {
  117. [_activityIndicator stopAnimating];
  118. [_refreshControl endRefreshing];
  119. [self.tableView reloadData];
  120. NSUInteger count = self.controller.currentListFiles.count;
  121. if (count == 0)
  122. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  123. else if (count != 1)
  124. self.numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  125. else
  126. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  127. }
  128. - (NSArray*)_generateToolbarItemsWithSortButton : (BOOL)withsb
  129. {
  130. NSMutableArray* result = [NSMutableArray array];
  131. if (withsb)
  132. {
  133. [result addObjectsFromArray:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _sortBarButtonItem]];
  134. }
  135. [result addObjectsFromArray:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]]];
  136. return result;
  137. }
  138. - (void)_showProgressInToolbar:(BOOL)value
  139. {
  140. if (!value) {
  141. [self setToolbarItems:[self _generateToolbarItemsWithSortButton:[self.controller supportSorting]] animated:YES];
  142. }
  143. else {
  144. _progressView.progressBar.progress = 0.;
  145. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  146. }
  147. }
  148. - (void)updateRemainingTime:(NSString *)time
  149. {
  150. [_progressView updateTime:time];
  151. }
  152. - (void)currentProgressInformation:(CGFloat)progress
  153. {
  154. [_progressView.progressBar setProgress:progress animated:YES];
  155. }
  156. - (void)operationWithProgressInformationStarted
  157. {
  158. [self _showProgressInToolbar:YES];
  159. }
  160. - (void)operationWithProgressInformationStopped
  161. {
  162. [self _showProgressInToolbar:NO];
  163. }
  164. #pragma mark - UITableViewDataSources
  165. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  166. {
  167. return self.controller.currentListFiles.count;
  168. }
  169. #pragma mark - UITableViewDelegate
  170. - (void)tableView:(UITableView *)tableView willDisplayCell:(__kindof UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. VLCCloudStorageTableViewCell *cloudcell = [cell isKindOfClass:VLCCloudStorageTableViewCell.class] ? (id)cell : nil;
  173. cloudcell.backgroundColor = PresentationTheme.current.colors.cellBackgroundA;
  174. cloudcell.titleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  175. cloudcell.folderTitleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  176. cloudcell.subtitleLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  177. }
  178. - (void)goBack
  179. {
  180. if (((![self.currentPath isEqualToString:@""] && ![self.currentPath isEqualToString:@"/"]) && [self.currentPath length] > 0) && [self.controller isAuthorized]){
  181. self.currentPath = [self.currentPath stringByDeletingLastPathComponent];
  182. [self requestInformationForCurrentPath];
  183. } else
  184. [self.navigationController popViewControllerAnimated:YES];
  185. }
  186. - (void)showLoginPanel
  187. {
  188. self.loginToCloudStorageView.frame = self.tableView.frame;
  189. self.navigationItem.rightBarButtonItem = nil;
  190. [self.tableView addSubview:self.loginToCloudStorageView];
  191. }
  192. - (void)updateViewAfterSessionChange
  193. {
  194. BOOL hasProgressbar = NO;
  195. for (id item in self.toolbarItems) {
  196. if (item == _progressBarButtonItem) {
  197. hasProgressbar = YES;
  198. }
  199. }
  200. if (!hasProgressbar) {
  201. //Only show sorting button and number of files button when there is no progress bar in the toolbar
  202. //Only show sorting button when controller support sorting and is authorized
  203. [self setToolbarItems:[self _generateToolbarItemsWithSortButton:self.controller.isAuthorized && [self.controller supportSorting]] animated:YES];
  204. }
  205. if (self.controller.canPlayAll) {
  206. self.navigationItem.rightBarButtonItems = @[_logoutButton, [UIBarButtonItem themedPlayAllButtonWithTarget:self andSelector:@selector(playAllAction:)]];
  207. } else {
  208. self.navigationItem.rightBarButtonItem = _logoutButton;
  209. }
  210. if (_authorizationInProgress || [self.controller isAuthorized]) {
  211. if (self.loginToCloudStorageView.superview) {
  212. [self.loginToCloudStorageView removeFromSuperview];
  213. }
  214. }
  215. if (![self.controller isAuthorized]) {
  216. [_activityIndicator stopAnimating];
  217. [self showLoginPanel];
  218. return;
  219. }
  220. //reload if we didn't come back from streaming
  221. if (self.currentPath == nil) {
  222. self.currentPath = @"";
  223. }
  224. if ([self.controller.currentListFiles count] == 0)
  225. [self requestInformationForCurrentPath];
  226. }
  227. - (void)logout
  228. {
  229. _currentPath = nil;
  230. [self.controller logout];
  231. [self updateViewAfterSessionChange];
  232. }
  233. - (void)sortButtonClicked:(UIBarButtonItem*)sender
  234. {
  235. [self presentViewController:self->sheet animated:YES completion:^{
  236. [self->sheet.collectionView selectItemAtIndexPath:self->manager.selectedIndex animated:NO scrollPosition:UICollectionViewScrollPositionCenteredVertically];
  237. }];
  238. }
  239. - (IBAction)loginAction:(id)sender
  240. {
  241. }
  242. - (IBAction)playAllAction:(id)sender
  243. {
  244. }
  245. @end