VLCCloudStorageTableViewController.m 8.8 KB

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