VLCNetworkServerBrowserViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 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. _browsingController.allowsFileDownload = YES;
  39. }
  40. return self;
  41. }
  42. - (void)viewDidLoad
  43. {
  44. [super viewDidLoad];
  45. _refreshControl = [[UIRefreshControl alloc] init];
  46. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  47. _refreshControl.tintColor = [UIColor whiteColor];
  48. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  49. [self.tableView addSubview:_refreshControl];
  50. self.title = self.serverBrowser.title;
  51. [self update];
  52. }
  53. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
  54. [self.tableView reloadData];
  55. [[VLCActivityManager defaultManager] networkActivityStopped];
  56. [_refreshControl endRefreshing];
  57. }
  58. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  59. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  60. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  61. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  62. }
  63. - (void)update
  64. {
  65. [self.serverBrowser update];
  66. [[VLCActivityManager defaultManager] networkActivityStarted];
  67. }
  68. -(void)handleRefresh
  69. {
  70. //set the title while refreshing
  71. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  72. //set the date and time of refreshing
  73. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  74. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  75. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  76. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  77. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  78. //end the refreshing
  79. [self update];
  80. }
  81. #pragma mark - server browser item specifics
  82. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  83. {
  84. if (item.isContainer) {
  85. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  86. [self.navigationController pushViewController:targetViewController animated:YES];
  87. } else {
  88. if (singlePlayback) {
  89. [self.browsingController streamFileForItem:item];
  90. } else {
  91. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  92. [self.browsingController configureSubtitlesInMediaList:mediaList];
  93. [self.browsingController streamMediaList:mediaList startingAtIndex:index];
  94. }
  95. }
  96. }
  97. #pragma mark - table view data source, for more see super
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  99. {
  100. if (tableView == self.searchDisplayController.searchResultsTableView)
  101. return _searchArray.count;
  102. return self.serverBrowser.items.count;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  107. if (cell == nil)
  108. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  109. id<VLCNetworkServerBrowserItem> item;
  110. if (tableView == self.searchDisplayController.searchResultsTableView) {
  111. item = _searchArray[indexPath.row];
  112. } else {
  113. item = self.serverBrowser.items[indexPath.row];
  114. }
  115. [self.browsingController configureCell:cell withItem:item];
  116. cell.delegate = self;
  117. return cell;
  118. }
  119. #pragma mark - table view delegate, for more see super
  120. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  123. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  124. [[VLCActivityManager defaultManager] networkActivityStopped];
  125. }
  126. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. id<VLCNetworkServerBrowserItem> item;
  129. NSInteger row = indexPath.row;
  130. BOOL singlePlayback = NO;
  131. if (tableView == self.searchDisplayController.searchResultsTableView) {
  132. item = _searchArray[row];
  133. singlePlayback = YES;
  134. } else {
  135. item = self.serverBrowser.items[row];
  136. }
  137. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  138. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  139. }
  140. #pragma mark - VLCNetworkListCell delegation
  141. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  142. {
  143. id<VLCNetworkServerBrowserItem> item;
  144. if ([self.searchDisplayController isActive])
  145. item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  146. else
  147. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  148. if ([self.browsingController triggerDownloadForItem:item]) {
  149. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  150. }
  151. }
  152. #pragma mark - search
  153. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  154. {
  155. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  156. self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  157. return YES;
  158. }
  159. @end