VLCOpenNetworkStreamTVViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCOpenNetworkStreamTVViewController.h"
  12. #import "VLCPlaybackController.h"
  13. #import "VLCPlayerDisplayController.h"
  14. #import "VLCFullscreenMovieTVViewController.h"
  15. #import "Reachability.h"
  16. #import "VLCHTTPUploaderController.h"
  17. @interface VLCOpenNetworkStreamTVViewController ()
  18. {
  19. NSMutableArray *_recentURLs;
  20. UILabel *_noURLsToShowLabel;
  21. Reachability *_reachability;
  22. }
  23. @end
  24. @implementation VLCOpenNetworkStreamTVViewController
  25. - (NSString *)title
  26. {
  27. return NSLocalizedString(@"NETWORK_TITLE", nil);
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. _reachability = [Reachability reachabilityForLocalWiFi];
  32. self.httpServerLabel.textColor = [UIColor VLCDarkBackgroundColor];
  33. _noURLsToShowLabel = [[UILabel alloc] init];
  34. _noURLsToShowLabel.text = NSLocalizedString(@"NO_RECENT_STREAMS", nil);
  35. _noURLsToShowLabel.textAlignment = NSTextAlignmentCenter;
  36. _noURLsToShowLabel.textColor = [UIColor VLCLightTextColor];
  37. _noURLsToShowLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
  38. [_noURLsToShowLabel sizeToFit];
  39. [_noURLsToShowLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
  40. [self.view addSubview:_noURLsToShowLabel];
  41. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:_noURLsToShowLabel
  42. attribute:NSLayoutAttributeCenterY
  43. relatedBy:NSLayoutRelationEqual
  44. toItem:self.view
  45. attribute:NSLayoutAttributeCenterY
  46. multiplier:1.0
  47. constant:0.0];
  48. [self.view addConstraint:yConstraint];
  49. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:_noURLsToShowLabel
  50. attribute:NSLayoutAttributeCenterX
  51. relatedBy:NSLayoutRelationEqual
  52. toItem:self.view
  53. attribute:NSLayoutAttributeCenterX
  54. multiplier:1.0
  55. constant:0.0];
  56. [self.view addConstraint:xConstraint];
  57. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  58. [notificationCenter addObserver:self
  59. selector:@selector(ubiquitousKeyValueStoreDidChange:)
  60. name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
  61. object:[NSUbiquitousKeyValueStore defaultStore]];
  62. [notificationCenter addObserver:self
  63. selector:@selector(reachabilityChanged)
  64. name:kReachabilityChangedNotification
  65. object:nil];
  66. /* force store update */
  67. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  68. [ubiquitousKeyValueStore synchronize];
  69. /* fetch data from cloud */
  70. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  71. #ifndef NDEBUG
  72. if (_recentURLs.count == 0) {
  73. [_recentURLs addObject:@"http://streams.videolan.org/streams/mp4/Mr_MrsSmith-h264_aac.mp4"];
  74. [_recentURLs addObject:@"http://streams.videolan.org/streams/mp4/Mr&MrsSmith.txt"];
  75. }
  76. #endif
  77. [self.previouslyPlayedStreamsTableView reloadData];
  78. _noURLsToShowLabel.hidden = _recentURLs.count != 0;
  79. self.playURLField.placeholder = NSLocalizedString(@"ENTER_URL", nil);
  80. }
  81. - (void)ubiquitousKeyValueStoreDidChange:(NSNotification *)notification
  82. {
  83. /* TODO: don't blindly trust that the Cloud knows best */
  84. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  85. [self.previouslyPlayedStreamsTableView reloadData];
  86. _noURLsToShowLabel.hidden = _recentURLs.count != 0;
  87. }
  88. - (void)reachabilityChanged
  89. {
  90. [self updateHTTPServerAddress];
  91. }
  92. - (void)updateHTTPServerAddress
  93. {
  94. BOOL connectedViaWifi = _reachability.currentReachabilityStatus == ReachableViaWiFi;
  95. self.toggleHTTPServerButton.enabled = connectedViaWifi;
  96. NSString *uploadText = connectedViaWifi ? [[VLCHTTPUploaderController sharedInstance] httpStatus] : NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", nil);
  97. self.httpServerLabel.text = uploadText;
  98. if (connectedViaWifi && [VLCHTTPUploaderController sharedInstance].isServerRunning)
  99. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_ON", nil) forState:UIControlStateNormal];
  100. else
  101. [self.toggleHTTPServerButton setTitle:NSLocalizedString(@"HTTP_SERVER_OFF", nil) forState:UIControlStateNormal];
  102. }
  103. - (void)toggleHTTPServer:(id)sender
  104. {
  105. BOOL futureHTTPServerState = ![VLCHTTPUploaderController sharedInstance].isServerRunning ;
  106. [[NSUserDefaults standardUserDefaults] setBool:futureHTTPServerState forKey:kVLCSettingSaveHTTPUploadServerStatus];
  107. [[VLCHTTPUploaderController sharedInstance] changeHTTPServerState:futureHTTPServerState];
  108. [self updateHTTPServerAddress];
  109. [[NSUserDefaults standardUserDefaults] synchronize];
  110. }
  111. - (void)viewWillAppear:(BOOL)animated
  112. {
  113. [super viewWillAppear:animated];
  114. [_reachability startNotifier];
  115. [self updateHTTPServerAddress];
  116. }
  117. - (void)viewWillDisappear:(BOOL)animated
  118. {
  119. [super viewWillDisappear:animated];
  120. [_reachability stopNotifier];
  121. /* force update before we leave */
  122. [[NSUbiquitousKeyValueStore defaultStore] synchronize];
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  127. if (!cell) {
  128. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  129. }
  130. NSString *content = _recentURLs[indexPath.row];
  131. cell.textLabel.text = [content lastPathComponent];
  132. cell.detailTextLabel.text = content;
  133. return cell;
  134. }
  135. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. [self.previouslyPlayedStreamsTableView deselectRowAtIndexPath:indexPath animated:NO];
  138. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  139. }
  140. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  141. {
  142. return _recentURLs.count;
  143. }
  144. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  145. {
  146. return 1;
  147. }
  148. - (void)URLEnteredInField:(id)sender
  149. {
  150. [self _openURLStringAndDismiss:self.playURLField.text];
  151. }
  152. - (void)_openURLStringAndDismiss:(NSString *)url
  153. {
  154. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  155. [vpc playURL:[NSURL URLWithString:url] subtitlesFilePath:nil];
  156. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  157. animated:YES
  158. completion:nil];
  159. }
  160. @end