VLCCloudStorageTableViewController.m 12 KB

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