VLCOpenNetworkStreamTVViewController.m 7.3 KB

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