VLCNetworkServerBrowserViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. NSInteger count = items.count;
  110. NSMutableArray *fileList = [[NSMutableArray alloc] init];
  111. for (NSInteger x = count - 1; x > -1; x--) {
  112. id<VLCNetworkServerBrowserItem> iter = items[x];
  113. if (![iter isContainer]) {
  114. [fileList addObject:[iter media]];
  115. }
  116. }
  117. if (fileList.count > 0) {
  118. VLCMediaList *fileMediaList = [[VLCMediaList alloc] initWithArray:fileList];
  119. [self.browsingController configureSubtitlesInMediaList:fileMediaList];
  120. [self.browsingController streamMediaList:fileMediaList startingAtIndex:0];
  121. }
  122. }
  123. #pragma mark - table view data source, for more see super
  124. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  125. {
  126. if (self.searchController.isActive)
  127. return _searchArray.count;
  128. return self.serverBrowser.items.count;
  129. }
  130. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  131. {
  132. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  133. if (cell == nil)
  134. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  135. id<VLCNetworkServerBrowserItem> item;
  136. if (self.searchController.isActive) {
  137. item = _searchArray[indexPath.row];
  138. } else {
  139. item = self.serverBrowser.items[indexPath.row];
  140. }
  141. [self.browsingController configureCell:cell withItem:item];
  142. cell.delegate = self;
  143. return cell;
  144. }
  145. #pragma mark - table view delegate, for more see super
  146. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  149. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  150. [[VLCActivityManager defaultManager] networkActivityStopped];
  151. }
  152. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  153. {
  154. id<VLCNetworkServerBrowserItem> item;
  155. NSInteger row = indexPath.row;
  156. BOOL singlePlayback = ![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem];
  157. if (self.searchController.isActive) {
  158. item = _searchArray[row];
  159. singlePlayback = YES;
  160. } else {
  161. item = self.serverBrowser.items[row];
  162. }
  163. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  164. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  165. }
  166. #pragma mark - VLCNetworkListCell delegation
  167. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  168. {
  169. id<VLCNetworkServerBrowserItem> item;
  170. if (self.searchController.isActive)
  171. item = _searchArray[[self.tableView indexPathForCell:cell].row];
  172. else
  173. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  174. if ([self.browsingController triggerDownloadForItem:item]) {
  175. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  176. }
  177. }
  178. #pragma mark - Search Research Updater
  179. - (void)updateSearchResultsForSearchController:(UISearchController *)_searchController
  180. {
  181. NSString *searchString = _searchController.searchBar.text;
  182. [self searchForText:searchString];
  183. [self.tableView reloadData];
  184. }
  185. - (void)searchForText:(NSString *)searchString
  186. {
  187. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  188. _searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  189. }
  190. @end