VLCNetworkServerBrowserViewController.m 8.5 KB

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