VLCCloudStorageTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. UIRefreshControl *_refreshControl;
  22. UIBarButtonItem *_progressBarButtonItem;
  23. UIBarButtonItem *_logoutButton;
  24. }
  25. @end
  26. @implementation VLCCloudStorageTableViewController
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. _authorizationInProgress = NO;
  31. self.modalPresentationStyle = UIModalPresentationFormSheet;
  32. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_BACK", nil) style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
  33. self.navigationItem.leftBarButtonItem = backButton;
  34. _logoutButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_LOGOUT", "") style:UIBarButtonItemStylePlain target:self action:@selector(logout)];
  35. [self.loginButton setTitle:NSLocalizedString(@"DROPBOX_LOGIN", nil) forState:UIControlStateNormal];
  36. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"sudHeaderBg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateForTheme) name:kVLCThemeDidChangeNotification object:nil];
  38. _refreshControl = [[UIRefreshControl alloc] init];
  39. _refreshControl.tintColor = [UIColor whiteColor];
  40. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  41. [self.tableView addSubview:_refreshControl];
  42. self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
  43. self.tableView.rowHeight = [VLCCloudStorageTableViewCell heightOfCell];
  44. _numberOfFilesBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), 0] style:UIBarButtonItemStylePlain target:nil action:nil];
  45. [_numberOfFilesBarButtonItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:11.] } forState:UIControlStateNormal];
  46. _numberOfFilesBarButtonItem.tintColor = PresentationTheme.current.colors.orangeUI;
  47. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  48. _activityIndicator.hidesWhenStopped = YES;
  49. _activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;
  50. [self.view addSubview:_activityIndicator];
  51. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
  52. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_activityIndicator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
  53. _progressView = [VLCProgressView new];
  54. _progressBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_progressView];
  55. _progressView.tintColor = PresentationTheme.current.colors.orangeUI;
  56. [self _showProgressInToolbar:NO];
  57. [self updateForTheme];
  58. }
  59. - (void)updateForTheme
  60. {
  61. self.tableView.separatorColor = PresentationTheme.current.colors.background;
  62. self.tableView.backgroundColor = PresentationTheme.current.colors.background;
  63. self.view.backgroundColor = PresentationTheme.current.colors.background;
  64. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  65. _activityIndicator.activityIndicatorViewStyle = PresentationTheme.current == PresentationTheme.brightTheme ? UIActivityIndicatorViewStyleGray : UIActivityIndicatorViewStyleWhiteLarge;
  66. self.loginToCloudStorageView.backgroundColor = PresentationTheme.current.colors.background;
  67. }
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. self.navigationController.toolbarHidden = NO;
  71. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  72. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  73. [super viewWillAppear:animated];
  74. }
  75. - (void)viewWillDisappear:(BOOL)animated
  76. {
  77. self.navigationController.toolbarHidden = YES;
  78. [super viewWillDisappear:animated];
  79. }
  80. -(void)handleRefresh
  81. {
  82. //set the title while refreshing
  83. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  84. //set the date and time of refreshing
  85. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  86. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  87. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  88. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  89. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  90. [self requestInformationForCurrentPath];
  91. }
  92. - (void)requestInformationForCurrentPath
  93. {
  94. [_activityIndicator startAnimating];
  95. [self.controller requestDirectoryListingAtPath:self.currentPath];
  96. }
  97. - (void)mediaListUpdated
  98. {
  99. [_activityIndicator stopAnimating];
  100. [_refreshControl endRefreshing];
  101. [self.tableView reloadData];
  102. NSUInteger count = self.controller.currentListFiles.count;
  103. if (count == 0)
  104. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"NO_FILES", nil);
  105. else if (count != 1)
  106. self.numberOfFilesBarButtonItem.title = [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
  107. else
  108. self.numberOfFilesBarButtonItem.title = NSLocalizedString(@"ONE_FILE", nil);
  109. }
  110. - (void)_showProgressInToolbar:(BOOL)value
  111. {
  112. if (!value)
  113. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _numberOfFilesBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  114. else {
  115. _progressView.progressBar.progress = 0.;
  116. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], _progressBarButtonItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]] animated:YES];
  117. }
  118. }
  119. - (void)updateRemainingTime:(NSString *)time
  120. {
  121. [_progressView updateTime:time];
  122. }
  123. - (void)currentProgressInformation:(CGFloat)progress
  124. {
  125. [_progressView.progressBar setProgress:progress animated:YES];
  126. }
  127. - (void)operationWithProgressInformationStarted
  128. {
  129. [self _showProgressInToolbar:YES];
  130. }
  131. - (void)operationWithProgressInformationStopped
  132. {
  133. [self _showProgressInToolbar:NO];
  134. }
  135. #pragma mark - UITableViewDataSources
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. return self.controller.currentListFiles.count;
  139. }
  140. #pragma mark - UITableViewDelegate
  141. - (void)tableView:(UITableView *)tableView willDisplayCell:(__kindof UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. VLCCloudStorageTableViewCell *cloudcell = [cell isKindOfClass:VLCCloudStorageTableViewCell.class] ? (id)cell : nil;
  144. cloudcell.backgroundColor = (indexPath.row % 2 == 0)? PresentationTheme.current.colors.cellBackgroundB : PresentationTheme.current.colors.cellBackgroundA;
  145. cloudcell.titleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  146. cloudcell.folderTitleLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  147. cloudcell.subtitleLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  148. }
  149. - (void)goBack
  150. {
  151. if (((![self.currentPath isEqualToString:@""] && ![self.currentPath isEqualToString:@"/"]) && [self.currentPath length] > 0) && [self.controller isAuthorized]){
  152. self.currentPath = [self.currentPath stringByDeletingLastPathComponent];
  153. [self requestInformationForCurrentPath];
  154. } else
  155. [self.navigationController popViewControllerAnimated:YES];
  156. }
  157. - (void)showLoginPanel
  158. {
  159. self.loginToCloudStorageView.frame = self.tableView.frame;
  160. self.navigationItem.rightBarButtonItem = nil;
  161. [self.tableView addSubview:self.loginToCloudStorageView];
  162. }
  163. - (void)updateViewAfterSessionChange
  164. {
  165. if (self.controller.canPlayAll) {
  166. self.navigationItem.rightBarButtonItems = @[_logoutButton, [UIBarButtonItem themedPlayAllButtonWithTarget:self andSelector:@selector(playAllAction:)]];
  167. } else {
  168. self.navigationItem.rightBarButtonItem = _logoutButton;
  169. }
  170. if(_authorizationInProgress || [self.controller isAuthorized]) {
  171. if (self.loginToCloudStorageView.superview) {
  172. [self.loginToCloudStorageView removeFromSuperview];
  173. }
  174. }
  175. if (![self.controller isAuthorized]) {
  176. [_activityIndicator stopAnimating];
  177. [self showLoginPanel];
  178. return;
  179. }
  180. //reload if we didn't come back from streaming
  181. if (self.currentPath == nil) {
  182. self.currentPath = @"";
  183. }
  184. if([self.controller.currentListFiles count] == 0)
  185. [self requestInformationForCurrentPath];
  186. }
  187. - (void)logout
  188. {
  189. [self.controller logout];
  190. [self updateViewAfterSessionChange];
  191. }
  192. - (IBAction)loginAction:(id)sender
  193. {
  194. }
  195. - (IBAction)playAllAction:(id)sender
  196. {
  197. }
  198. @end