VLCOpenNetworkStreamTVViewController.m 7.0 KB

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