VLCNetworkServerBrowserViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "VLCPlaybackService.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)networkServerBrowserShouldPopView:(id<VLCNetworkServerBrowser>)networkBrowser
  70. {
  71. [self.navigationController popViewControllerAnimated:YES];
  72. }
  73. - (void)networkServerBrowserEndParsing:(id<VLCNetworkServerBrowser>)networkBrowser
  74. {
  75. [self stopActivityIndicator];
  76. }
  77. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error
  78. {
  79. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  80. errorMessage:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  81. viewController:self];
  82. }
  83. - (UIStatusBarStyle)preferredStatusBarStyle
  84. {
  85. return PresentationTheme.current.colors.statusBarStyle;
  86. }
  87. - (void)updateUI
  88. {
  89. [self.tableView reloadData];
  90. self.title = self.serverBrowser.title;
  91. }
  92. - (void)update
  93. {
  94. [self.serverBrowser update];
  95. [[VLCActivityManager defaultManager] networkActivityStarted];
  96. }
  97. -(void)handleRefresh
  98. {
  99. [self update];
  100. }
  101. #pragma mark - server browser item specifics
  102. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  103. {
  104. if (item.isContainer) {
  105. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  106. [self.navigationController pushViewController:targetViewController animated:YES];
  107. } else {
  108. if (singlePlayback) {
  109. [self.browsingController streamFileForItem:item];
  110. } else {
  111. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  112. [self.browsingController configureSubtitlesInMediaList:mediaList];
  113. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  114. }
  115. }
  116. }
  117. - (void)playAllAction:(id)sender
  118. {
  119. NSArray *items = self.serverBrowser.items;
  120. NSMutableArray *fileList = [[NSMutableArray alloc] init];
  121. for (id<VLCNetworkServerBrowserItem> iter in items) {
  122. if (![iter isContainer]) {
  123. [fileList addObject:[iter media]];
  124. }
  125. }
  126. if (fileList.count > 0) {
  127. VLCMediaList *fileMediaList = [[VLCMediaList alloc] initWithArray:fileList];
  128. [self.browsingController configureSubtitlesInMediaList:fileMediaList];
  129. [self.browsingController streamMediaList:fileMediaList startingAtIndex:0];
  130. }
  131. }
  132. #pragma mark - table view data source, for more see super
  133. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  134. {
  135. if (self.searchController.isActive)
  136. return _searchArray.count;
  137. return self.serverBrowser.items.count;
  138. }
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  142. if (cell == nil)
  143. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  144. id<VLCNetworkServerBrowserItem> item;
  145. if (self.searchController.isActive) {
  146. item = _searchArray[indexPath.row];
  147. } else {
  148. item = self.serverBrowser.items[indexPath.row];
  149. }
  150. [self.browsingController configureCell:cell withItem:item];
  151. cell.delegate = self;
  152. return cell;
  153. }
  154. #pragma mark - table view delegate, for more see super
  155. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  158. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  159. [[VLCActivityManager defaultManager] networkActivityStopped];
  160. cell.titleLabel.textColor = cell.folderTitleLabel.textColor = cell.subtitleLabel.textColor = cell.thumbnailView.tintColor = PresentationTheme.current.colors.cellTextColor;
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. id<VLCNetworkServerBrowserItem> item;
  165. NSInteger row = indexPath.row;
  166. BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  167. if (self.searchController.isActive) {
  168. if (row < _searchArray.count) {
  169. item = _searchArray[row];
  170. singlePlayback = YES;
  171. }
  172. } else {
  173. NSArray *items = self.serverBrowser.items;
  174. if (row < items.count) {
  175. item = items[row];
  176. }
  177. }
  178. if (item) {
  179. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  180. }
  181. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  182. }
  183. #pragma mark - VLCNetworkListCell delegation
  184. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  185. {
  186. id<VLCNetworkServerBrowserItem> item;
  187. if (self.searchController.isActive)
  188. item = _searchArray[[self.tableView indexPathForCell:cell].row];
  189. else
  190. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  191. if ([self.browsingController triggerDownloadForItem:item]) {
  192. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  193. }
  194. }
  195. #pragma mark - Search Research Updater
  196. - (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
  197. {
  198. NSString *searchString = _searchController.searchBar.text;
  199. [self searchForText:searchString];
  200. [self.tableView reloadData];
  201. }
  202. - (void)searchForText:(NSString *)searchString
  203. {
  204. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  205. _searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  206. }
  207. #pragma mark -
  208. - (void)themeDidChange
  209. {
  210. self.tableView.backgroundColor = PresentationTheme.current.colors.background;
  211. [self setNeedsStatusBarAppearanceUpdate];
  212. }
  213. @end