VLCOpenNetworkStreamTVViewController.m 7.2 KB

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