VLCOpenNetworkStreamTVViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. @interface VLCOpenNetworkStreamTVViewController ()
  16. {
  17. NSMutableArray *_recentURLs;
  18. UILabel *_noURLsToShowLabel;
  19. }
  20. @end
  21. @implementation VLCOpenNetworkStreamTVViewController
  22. - (NSString *)title
  23. {
  24. return NSLocalizedString(@"NETWORK_TITLE", nil);
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. _noURLsToShowLabel = [[UILabel alloc] init];
  29. _noURLsToShowLabel.text = NSLocalizedString(@"NO_RECENT_STREAMS", nil);
  30. _noURLsToShowLabel.textAlignment = NSTextAlignmentCenter;
  31. _noURLsToShowLabel.textColor = [UIColor VLCLightTextColor];
  32. _noURLsToShowLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
  33. [_noURLsToShowLabel sizeToFit];
  34. [_noURLsToShowLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
  35. [self.view addSubview:_noURLsToShowLabel];
  36. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:_noURLsToShowLabel
  37. attribute:NSLayoutAttributeCenterY
  38. relatedBy:NSLayoutRelationEqual
  39. toItem:self.view
  40. attribute:NSLayoutAttributeCenterY
  41. multiplier:1.0
  42. constant:0.0];
  43. [self.view addConstraint:yConstraint];
  44. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:_noURLsToShowLabel
  45. attribute:NSLayoutAttributeCenterX
  46. relatedBy:NSLayoutRelationEqual
  47. toItem:self.view
  48. attribute:NSLayoutAttributeCenterX
  49. multiplier:1.0
  50. constant:0.0];
  51. [self.view addConstraint:xConstraint];
  52. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  53. [notificationCenter addObserver:self
  54. selector:@selector(ubiquitousKeyValueStoreDidChange:)
  55. name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
  56. object:[NSUbiquitousKeyValueStore defaultStore]];
  57. /* force store update */
  58. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  59. [ubiquitousKeyValueStore synchronize];
  60. /* fetch data from cloud */
  61. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  62. #ifndef NDEBUG
  63. if (_recentURLs.count == 0) {
  64. [_recentURLs addObject:@"https://www.youtube.com/watch?v=13e2GxpqGPY"];
  65. [_recentURLs addObject:@"https://vimeo.com/74370512"];
  66. }
  67. #endif
  68. [self.previouslyPlayedStreamsTableView reloadData];
  69. _noURLsToShowLabel.hidden = _recentURLs.count != 0;
  70. self.playURLField.placeholder = NSLocalizedString(@"ENTER_URL", nil);
  71. }
  72. - (void)ubiquitousKeyValueStoreDidChange:(NSNotification *)notification
  73. {
  74. /* TODO: don't blindly trust that the Cloud knows best */
  75. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  76. [self.previouslyPlayedStreamsTableView reloadData];
  77. _noURLsToShowLabel.hidden = _recentURLs.count != 0;
  78. }
  79. - (void)viewWillDisappear:(BOOL)animated
  80. {
  81. [super viewWillDisappear:animated];
  82. /* force update before we leave */
  83. [[NSUbiquitousKeyValueStore defaultStore] synchronize];
  84. }
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  88. if (!cell) {
  89. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  90. }
  91. NSString *content = _recentURLs[indexPath.row];
  92. cell.textLabel.text = [content lastPathComponent];
  93. cell.detailTextLabel.text = content;
  94. return cell;
  95. }
  96. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. [self.previouslyPlayedStreamsTableView deselectRowAtIndexPath:indexPath animated:NO];
  99. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  100. }
  101. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  102. {
  103. return _recentURLs.count;
  104. }
  105. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  106. {
  107. return 1;
  108. }
  109. - (void)URLEnteredInField:(id)sender
  110. {
  111. [self _openURLStringAndDismiss:self.playURLField.text];
  112. }
  113. - (void)_openURLStringAndDismiss:(NSString *)url
  114. {
  115. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  116. [vpc playURL:[NSURL URLWithString:url] subtitlesFilePath:nil];
  117. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  118. animated:YES
  119. completion:nil];
  120. }
  121. @end