VLCNetworkServerBrowserViewController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. @interface VLCNetworkServerBrowserViewController () <VLCNetworkServerBrowserDelegate,VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
  23. {
  24. UIRefreshControl *_refreshControl;
  25. }
  26. @property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
  27. @property (nonatomic) VLCServerBrowsingController *browsingController;
  28. @property (nonatomic) NSArray<id<VLCNetworkServerBrowserItem>> *searchArray;
  29. @end
  30. @implementation VLCNetworkServerBrowserViewController
  31. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)browser
  32. {
  33. self = [super init];
  34. if (self) {
  35. _serverBrowser = browser;
  36. browser.delegate = self;
  37. _browsingController = [[VLCServerBrowsingController alloc] initWithViewController:self serverBrowser:browser];
  38. }
  39. return self;
  40. }
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  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. self.title = self.serverBrowser.title;
  50. [self update];
  51. }
  52. - (void)viewWillAppear:(BOOL)animated
  53. {
  54. [super viewWillAppear:animated];
  55. [self updateUI];
  56. }
  57. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
  58. [self updateUI];
  59. [[VLCActivityManager defaultManager] networkActivityStopped];
  60. [_refreshControl endRefreshing];
  61. }
  62. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  63. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  64. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  65. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  66. }
  67. - (void)updateUI
  68. {
  69. [self.tableView reloadData];
  70. self.title = self.serverBrowser.title;
  71. }
  72. - (void)update
  73. {
  74. [self.serverBrowser update];
  75. [[VLCActivityManager defaultManager] networkActivityStarted];
  76. }
  77. -(void)handleRefresh
  78. {
  79. //set the title while refreshing
  80. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  81. //set the date and time of refreshing
  82. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  83. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  84. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  85. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  86. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  87. //end the refreshing
  88. [self update];
  89. }
  90. #pragma mark - server browser item specifics
  91. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  92. {
  93. if (item.isContainer) {
  94. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  95. [self.navigationController pushViewController:targetViewController animated:YES];
  96. } else {
  97. if (singlePlayback) {
  98. [self.browsingController streamFileForItem:item];
  99. } else {
  100. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  101. [self.browsingController configureSubtitlesInMediaList:mediaList];
  102. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  103. }
  104. }
  105. }
  106. - (void)playAllAction:(id)sender
  107. {
  108. NSArray *items = self.serverBrowser.items;
  109. NSMutableArray *fileList = [[NSMutableArray alloc] init];
  110. for (id<VLCNetworkServerBrowserItem> iter in items) {
  111. if (![iter isContainer]) {
  112. [fileList addObject:[iter media]];
  113. }
  114. }
  115. if (fileList.count > 0) {
  116. VLCMediaList *fileMediaList = [[VLCMediaList alloc] initWithArray:fileList];
  117. [self.browsingController configureSubtitlesInMediaList:fileMediaList];
  118. [self.browsingController streamMediaList:fileMediaList startingAtIndex:0];
  119. }
  120. }
  121. #pragma mark - table view data source, for more see super
  122. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  123. {
  124. if (self.searchController.isActive)
  125. return _searchArray.count;
  126. return self.serverBrowser.items.count;
  127. }
  128. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  129. {
  130. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  131. if (cell == nil)
  132. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  133. id<VLCNetworkServerBrowserItem> item;
  134. if (self.searchController.isActive) {
  135. item = _searchArray[indexPath.row];
  136. } else {
  137. item = self.serverBrowser.items[indexPath.row];
  138. }
  139. [self.browsingController configureCell:cell withItem:item];
  140. cell.delegate = self;
  141. return cell;
  142. }
  143. #pragma mark - table view delegate, for more see super
  144. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  145. {
  146. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  147. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  148. [[VLCActivityManager defaultManager] networkActivityStopped];
  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. item = _searchArray[row];
  157. singlePlayback = YES;
  158. } else {
  159. item = self.serverBrowser.items[row];
  160. }
  161. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  162. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  163. }
  164. #pragma mark - VLCNetworkListCell delegation
  165. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  166. {
  167. id<VLCNetworkServerBrowserItem> item;
  168. if (self.searchController.isActive)
  169. item = _searchArray[[self.tableView indexPathForCell:cell].row];
  170. else
  171. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  172. if ([self.browsingController triggerDownloadForItem:item]) {
  173. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  174. }
  175. }
  176. #pragma mark - Search Research Updater
  177. - (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
  178. {
  179. NSString *searchString = _searchController.searchBar.text;
  180. [self searchForText:searchString];
  181. [self.tableView reloadData];
  182. }
  183. - (void)searchForText:(NSString *)searchString
  184. {
  185. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  186. _searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  187. }
  188. @end