VLCCloudStorageTableViewController.m 12 KB

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