VLCCloudStorageTableViewController.m 8.8 KB

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