VLCOpenNetworkStreamTVViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  37. * wearing a Father Xmas hat.
  38. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  39. * and should not be confused with the idea of religious statements or propagation there off
  40. */
  41. NSCalendar *gregorian =
  42. [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  43. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[NSDate date]];
  44. if (dayOfYear >= 354)
  45. self.nothingFoundConeImageView.image = [UIImage imageNamed:@"xmas-cone"];
  46. }
  47. - (void)viewWillAppear:(BOOL)animated
  48. {
  49. /* force store update */
  50. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  51. [ubiquitousKeyValueStore synchronize];
  52. /* fetch data from cloud */
  53. _recentURLs = [NSMutableArray arrayWithArray:[ubiquitousKeyValueStore arrayForKey:kVLCRecentURLs]];
  54. [self.previouslyPlayedStreamsTableView reloadData];
  55. }
  56. - (void)ubiquitousKeyValueStoreDidChange:(NSNotification *)notification
  57. {
  58. /* TODO: don't blindly trust that the Cloud knows best */
  59. _recentURLs = [NSMutableArray arrayWithArray:[[NSUbiquitousKeyValueStore defaultStore] arrayForKey:kVLCRecentURLs]];
  60. [self.previouslyPlayedStreamsTableView reloadData];
  61. }
  62. - (void)viewWillDisappear:(BOOL)animated
  63. {
  64. [super viewWillDisappear:animated];
  65. /* force update before we leave */
  66. [[NSUbiquitousKeyValueStore defaultStore] synchronize];
  67. }
  68. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  69. {
  70. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  71. if (!cell) {
  72. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"RecentlyPlayedURLsTableViewCell"];
  73. }
  74. NSString *content = _recentURLs[indexPath.row];
  75. cell.textLabel.text = [content lastPathComponent];
  76. cell.detailTextLabel.text = content;
  77. return cell;
  78. }
  79. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81. [self.previouslyPlayedStreamsTableView deselectRowAtIndexPath:indexPath animated:NO];
  82. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  83. }
  84. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  85. {
  86. NSInteger count = _recentURLs.count;
  87. self.nothingFoundView.hidden = count > 0;
  88. return count;
  89. }
  90. - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92. self.currentlyFocusedIndexPath = indexPath;
  93. }
  94. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  95. {
  96. return 1;
  97. }
  98. - (void)URLEnteredInField:(id)sender
  99. {
  100. NSString *urlString = self.playURLField.text;
  101. if (urlString.length) {
  102. if ([_recentURLs indexOfObject:urlString] != NSNotFound)
  103. [_recentURLs removeObject:urlString];
  104. if (_recentURLs.count >= 100)
  105. [_recentURLs removeLastObject];
  106. [_recentURLs addObject:urlString];
  107. [[NSUbiquitousKeyValueStore defaultStore] setArray:_recentURLs forKey:kVLCRecentURLs];
  108. [self _openURLStringAndDismiss:urlString];
  109. }
  110. }
  111. - (void)_openURLStringAndDismiss:(NSString *)urlString
  112. {
  113. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  114. [vpc playURL:[NSURL URLWithString:urlString] subtitlesFilePath:nil];
  115. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  116. animated:YES
  117. completion:nil];
  118. }
  119. #pragma mark - editing
  120. - (NSIndexPath *)indexPathToDelete
  121. {
  122. NSIndexPath *indexPathToDelete = self.currentlyFocusedIndexPath;
  123. return indexPathToDelete;
  124. }
  125. - (NSString *)itemToDelete
  126. {
  127. NSIndexPath *indexPathToDelete = self.indexPathToDelete;
  128. if (!indexPathToDelete) {
  129. return nil;
  130. }
  131. NSString *ret = nil;
  132. @synchronized(_recentURLs) {
  133. NSInteger index = indexPathToDelete.item;
  134. if (index < _recentURLs.count) {
  135. ret = _recentURLs[index];
  136. }
  137. }
  138. return ret;
  139. }
  140. - (void)setEditing:(BOOL)editing
  141. {
  142. [super setEditing:editing];
  143. UITableViewCell *focusedCell = [self.previouslyPlayedStreamsTableView cellForRowAtIndexPath:self.currentlyFocusedIndexPath];
  144. if (editing) {
  145. [focusedCell.layer addAnimation:[CAAnimation vlc_wiggleAnimationwithSoftMode:YES]
  146. forKey:VLCWiggleAnimationKey];
  147. } else {
  148. [focusedCell.layer removeAnimationForKey:VLCWiggleAnimationKey];
  149. }
  150. }
  151. - (void)deleteFileAtIndex:(NSIndexPath *)indexPathToDelete
  152. {
  153. [super deleteFileAtIndex:indexPathToDelete];
  154. if (!indexPathToDelete) {
  155. return;
  156. }
  157. @synchronized(_recentURLs) {
  158. [_recentURLs removeObjectAtIndex:indexPathToDelete.row];
  159. }
  160. [[NSUbiquitousKeyValueStore defaultStore] setArray:_recentURLs forKey:kVLCRecentURLs];
  161. [self.previouslyPlayedStreamsTableView reloadData];
  162. }
  163. @end