VLCNetworkServerBrowserViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Tobias Conradi <videolan # tobias-conradi.de>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCNetworkServerBrowserViewController.h"
  15. #import "VLCNetworkListCell.h"
  16. #import "VLCActivityManager.h"
  17. #import "VLCStatusLabel.h"
  18. #import "VLCPlaybackController.h"
  19. #import "VLCDownloadViewController.h"
  20. #import "VLCNetworkServerBrowser-Protocol.h"
  21. #import "VLCServerBrowsingController.h"
  22. #import "VLC-Swift.h"
  23. @interface VLCNetworkServerBrowserViewController () <VLCNetworkServerBrowserDelegate,VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
  24. {
  25. UIRefreshControl *_refreshControl;
  26. }
  27. @property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
  28. @property (nonatomic) VLCServerBrowsingController *browsingController;
  29. @property (nonatomic) NSArray<id<VLCNetworkServerBrowserItem>> *searchArray;
  30. @end
  31. @implementation VLCNetworkServerBrowserViewController
  32. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)browser
  33. {
  34. self = [super init];
  35. if (self) {
  36. _serverBrowser = browser;
  37. browser.delegate = self;
  38. _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:browser];
  39. }
  40. return self;
  41. }
  42. - (void)viewDidLoad
  43. {
  44. [super viewDidLoad];
  45. _refreshControl = [[UIRefreshControl alloc] init];
  46. _refreshControl.tintColor = [UIColor VLCOrangeTintColor];
  47. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  48. if (@available(iOS 10, *)) {
  49. self.tableView.refreshControl = _refreshControl;
  50. } else {
  51. [self.tableView addSubview:_refreshControl];
  52. }
  53. self.tableView.backgroundColor = PresentationTheme.current.colors.background;
  54. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChange) name:kVLCThemeDidChangeNotification object:nil];
  55. self.title = self.serverBrowser.title;
  56. [self update];
  57. }
  58. - (void)viewWillAppear:(BOOL)animated
  59. {
  60. [super viewWillAppear:animated];
  61. [self updateUI];
  62. }
  63. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
  64. {
  65. [self updateUI];
  66. [[VLCActivityManager defaultManager] networkActivityStopped];
  67. [_refreshControl endRefreshing];
  68. }
  69. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error
  70. {
  71. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  72. errorMessage:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  73. viewController:self
  74. buttonsAction:@[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_OK", nil)
  75. action: ^(UIAlertAction* action){}]]];
  76. }
  77. - (void)updateUI
  78. {
  79. [self.tableView reloadData];
  80. self.title = self.serverBrowser.title;
  81. }
  82. - (void)update
  83. {
  84. [self.serverBrowser update];
  85. [[VLCActivityManager defaultManager] networkActivityStarted];
  86. }
  87. -(void)handleRefresh
  88. {
  89. [self update];
  90. }
  91. #pragma mark - server browser item specifics
  92. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  93. {
  94. if (item.isContainer) {
  95. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  96. [self.navigationController pushViewController:targetViewController animated:YES];
  97. } else {
  98. if (singlePlayback) {
  99. [self.browsingController streamFileForItem:item];
  100. } else {
  101. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  102. [self.browsingController configureSubtitlesInMediaList:mediaList];
  103. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  104. }
  105. }
  106. }
  107. - (void)playAllAction:(id)sender
  108. {
  109. NSArray *items = self.serverBrowser.items;
  110. NSMutableArray *fileList = [[NSMutableArray alloc] init];
  111. for (id<VLCNetworkServerBrowserItem> iter in items) {
  112. if (![iter isContainer]) {
  113. [fileList addObject:[iter media]];
  114. }
  115. }
  116. if (fileList.count > 0) {
  117. VLCMediaList *fileMediaList = [[VLCMediaList alloc] initWithArray:fileList];
  118. [self.browsingController configureSubtitlesInMediaList:fileMediaList];
  119. [self.browsingController streamMediaList:fileMediaList startingAtIndex:0];
  120. }
  121. }
  122. #pragma mark - table view data source, for more see super
  123. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  124. {
  125. if (self.searchController.isActive)
  126. return _searchArray.count;
  127. return self.serverBrowser.items.count;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  132. if (cell == nil)
  133. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  134. id<VLCNetworkServerBrowserItem> item;
  135. if (self.searchController.isActive) {
  136. item = _searchArray[indexPath.row];
  137. } else {
  138. item = self.serverBrowser.items[indexPath.row];
  139. }
  140. [self.browsingController configureCell:cell withItem:item];
  141. cell.delegate = self;
  142. return cell;
  143. }
  144. #pragma mark - table view delegate, for more see super
  145. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  148. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  149. [[VLCActivityManager defaultManager] networkActivityStopped];
  150. UIColor *color = (indexPath.row % 2 == 0)? PresentationTheme.current.colors.cellBackgroundB : PresentationTheme.current.colors.cellBackgroundA;
  151. cell.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  152. cell.titleLabel.textColor = cell.folderTitleLabel.textColor = cell.subtitleLabel.textColor = cell.thumbnailView.tintColor = PresentationTheme.current.colors.cellTextColor;
  153. }
  154. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. id<VLCNetworkServerBrowserItem> item;
  157. NSInteger row = indexPath.row;
  158. BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  159. if (self.searchController.isActive) {
  160. if (row < _searchArray.count) {
  161. item = _searchArray[row];
  162. singlePlayback = YES;
  163. }
  164. } else {
  165. NSArray *items = self.serverBrowser.items;
  166. if (row < items.count) {
  167. item = items[row];
  168. }
  169. }
  170. if (item) {
  171. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  172. }
  173. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  174. }
  175. #pragma mark - VLCNetworkListCell delegation
  176. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  177. {
  178. id<VLCNetworkServerBrowserItem> item;
  179. if (self.searchController.isActive)
  180. item = _searchArray[[self.tableView indexPathForCell:cell].row];
  181. else
  182. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  183. if ([self.browsingController triggerDownloadForItem:item]) {
  184. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  185. }
  186. }
  187. #pragma mark - Search Research Updater
  188. - (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
  189. {
  190. NSString *searchString = _searchController.searchBar.text;
  191. [self searchForText:searchString];
  192. [self.tableView reloadData];
  193. }
  194. - (void)searchForText:(NSString *)searchString
  195. {
  196. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  197. _searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  198. }
  199. #pragma mark -
  200. - (void)themeDidChange
  201. {
  202. self.tableView.backgroundColor = PresentationTheme.current.colors.background;
  203. [self setNeedsStatusBarAppearanceUpdate];
  204. }
  205. @end