VLCOpenNetworkStreamTVViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "CAAnimation+VLCWiggle.h"
  16. @interface VLCOpenNetworkStreamTVViewController ()
  17. {
  18. NSMutableArray *_recentURLs;
  19. }
  20. @property (nonatomic) NSIndexPath *currentlyFocusedIndexPath;
  21. @end
  22. @implementation VLCOpenNetworkStreamTVViewController
  23. - (NSString *)title
  24. {
  25. return NSLocalizedString(@"NETWORK_TITLE", nil);
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.nothingFoundLabel.text = NSLocalizedString(@"NO_RECENT_STREAMS", nil);
  30. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  31. [notificationCenter addObserver:self
  32. selector:@selector(ubiquitousKeyValueStoreDidChange:)
  33. name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
  34. object:[NSUbiquitousKeyValueStore defaultStore]];
  35. self.playURLField.placeholder = NSLocalizedString(@"ENTER_URL", nil);
  36. }
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. /* force store update */
  40. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  41. [ubiquitousKeyValueStore synchronize];
  42. /* fetch data from cloud */
  43. _recentURLs = [NSMutableArray arrayWithArray:[ubiquitousKeyValueStore arrayForKey:kVLCRecentURLs]];
  44. #ifndef NDEBUG
  45. if (_recentURLs.count == 0) {
  46. [_recentURLs addObject:@"https://www.youtube.com/watch?v=13e2GxpqGPY"];
  47. [_recentURLs addObject:@"https://vimeo.com/74370512"];
  48. }
  49. #endif
  50. [self.previouslyPlayedStreamsTableView reloadData];
  51. }
  52. - (void)ubiquitousKeyValueStoreDidChange:(NSNotification *)notification
  53. {
  54. /* TODO: don't blindly trust that the Cloud knows best */
  55. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  56. [self.previouslyPlayedStreamsTableView reloadData];
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated
  59. {
  60. [super viewWillDisappear:animated];
  61. /* force update before we leave */
  62. [[NSUbiquitousKeyValueStore defaultStore] synchronize];
  63. }
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  67. if (!cell) {
  68. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  69. }
  70. NSString *content = _recentURLs[indexPath.row];
  71. cell.textLabel.text = [content lastPathComponent];
  72. cell.detailTextLabel.text = content;
  73. return cell;
  74. }
  75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. [self.previouslyPlayedStreamsTableView deselectRowAtIndexPath:indexPath animated:NO];
  78. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  79. }
  80. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  81. {
  82. NSInteger count = _recentURLs.count;
  83. self.nothingFoundView.hidden = count > 0;
  84. return count;
  85. }
  86. - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. self.currentlyFocusedIndexPath = indexPath;
  89. }
  90. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  91. {
  92. return 1;
  93. }
  94. - (void)URLEnteredInField:(id)sender
  95. {
  96. NSString *urlString = self.playURLField.text;
  97. if (urlString.length) {
  98. if ([_recentURLs indexOfObject:urlString] != NSNotFound)
  99. [_recentURLs removeObject:urlString];
  100. if (_recentURLs.count >= 100)
  101. [_recentURLs removeLastObject];
  102. [_recentURLs addObject:urlString];
  103. [[NSUbiquitousKeyValueStore defaultStore] setArray:_recentURLs forKey:kVLCRecentURLs];
  104. [self _openURLStringAndDismiss:urlString];
  105. }
  106. }
  107. - (void)_openURLStringAndDismiss:(NSString *)urlString
  108. {
  109. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  110. [vpc playURL:[NSURL URLWithString:urlString] subtitlesFilePath:nil];
  111. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  112. animated:YES
  113. completion:nil];
  114. }
  115. #pragma mark - editing
  116. - (NSIndexPath *)indexPathToDelete
  117. {
  118. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  119. return indexPathToDelete;
  120. }
  121. - (NSString *)itemToDelete
  122. {
  123. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  124. if (!indexPathToDelete) {
  125. return nil;
  126. }
  127. NSString *ret;
  128. @synchronized(_recentURLs) {
  129. ret = _recentURLs[indexPathToDelete.item];
  130. }
  131. return ret;
  132. }
  133. - (void)setEditing:(BOOL)editing
  134. {
  135. [super setEditing:editing];
  136. UITableViewCell *focusedCell = [self.previouslyPlayedStreamsTableView cellForRowAtIndexPath:self.currentlyFocusedIndexPath];
  137. if (editing) {
  138. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimation]
  139. forKey:VLCWiggleAnimationKey];
  140. } else {
  141. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  142. }
  143. }
  144. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  145. {
  146. if (!indexPathToDelete) {
  147. return;
  148. }
  149. @synchronized(_recentURLs) {
  150. [_recentURLs removeObjectAtIndex:indexPathToDelete.row];
  151. }
  152. [[NSUbiquitousKeyValueStore defaultStore] setArray:_recentURLs forKey:kVLCRecentURLs];
  153. [self.previouslyPlayedStreamsTableView reloadData];
  154. }
  155. @end