VLCOpenNetworkStreamViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // VLCOpenNetworkStreamViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 16.06.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCOpenNetworkStreamViewController.h"
  11. #import "VLCAppDelegate.h"
  12. #import "VLCPlaylistViewController.h"
  13. #import "UIBarButtonItem+Theme.h"
  14. #import "UINavigationController+Theme.h"
  15. #import "VLCMenuTableViewController.h"
  16. @interface VLCOpenNetworkStreamViewController ()
  17. {
  18. NSMutableArray *_recentURLs;
  19. }
  20. @end
  21. @implementation VLCOpenNetworkStreamViewController
  22. + (void)initialize
  23. {
  24. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  25. NSDictionary *appDefaults = @{kVLCRecentURLs : @[], kVLCPrivateWebStreaming : @(NO)};
  26. [defaults registerDefaults:appDefaults];
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. [self.openButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  32. [self.privateModeLabel setText:NSLocalizedString(@"PRIVATE_PLAYBACK_TOGGLE", @"")];
  33. self.title = NSLocalizedString(@"OPEN_NETWORK", @"");
  34. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  35. [self.whatToOpenHelpLabel setText:NSLocalizedString(@"OPEN_NETWORK_HELP", @"")];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. self.navigationController.navigationBar.translucent = NO;
  40. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  41. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  42. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  43. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  44. pasteURL = [NSURL URLWithString:pasteString];
  45. }
  46. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  47. self.urlField.text = [pasteURL absoluteString];
  48. }
  49. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  50. _recentURLs = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCRecentURLs]];
  51. self.privateToggleSwitch.on = [defaults boolForKey:kVLCPrivateWebStreaming];
  52. [super viewWillAppear:animated];
  53. }
  54. - (void)viewWillDisappear:(BOOL)animated
  55. {
  56. if (SYSTEM_RUNS_IN_THE_FUTURE)
  57. self.navigationController.navigationBar.translucent = YES;
  58. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  59. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  60. [defaults setBool:self.privateToggleSwitch.on forKey:kVLCPrivateWebStreaming];
  61. [defaults synchronize];
  62. [super viewWillDisappear:animated];
  63. }
  64. - (CGSize)contentSizeForViewInPopover {
  65. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  66. }
  67. #pragma mark - UI interaction
  68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  69. {
  70. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  71. return NO;
  72. return YES;
  73. }
  74. - (IBAction)goBack:(id)sender
  75. {
  76. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  77. }
  78. - (IBAction)openButtonAction:(id)sender
  79. {
  80. if ([self.urlField.text length] > 0) {
  81. if (!self.privateToggleSwitch.on) {
  82. if ([_recentURLs indexOfObject:self.urlField.text] != NSNotFound)
  83. [_recentURLs removeObject:self.urlField.text];
  84. if (_recentURLs.count >= 15)
  85. [_recentURLs removeLastObject];
  86. [_recentURLs addObject:self.urlField.text];
  87. [self.historyTableView reloadData];
  88. }
  89. [self _openURLStringAndDismiss:self.urlField.text];
  90. }
  91. }
  92. #pragma mark - table view data source
  93. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  94. {
  95. return 1;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  98. {
  99. return _recentURLs.count;
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. static NSString *CellIdentifier = @"StreamingHistoryCell";
  104. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  105. if (cell == nil) {
  106. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  107. cell.textLabel.textColor = [UIColor whiteColor];
  108. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  109. }
  110. NSInteger row = indexPath.row;
  111. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  112. cell.detailTextLabel.text = _recentURLs[row];
  113. return cell;
  114. }
  115. #pragma mark - table view delegate
  116. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  119. }
  120. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. return YES;
  123. }
  124. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. if (editingStyle == UITableViewCellEditingStyleDelete) {
  127. [_recentURLs removeObjectAtIndex:indexPath.row];
  128. [tableView reloadData];
  129. }
  130. }
  131. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  134. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  135. }
  136. #pragma mark - internals
  137. - (void)_openURLStringAndDismiss:(NSString *)url
  138. {
  139. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  140. [appDelegate.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  141. [appDelegate.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:[NSURL URLWithString:url] afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  142. }
  143. @end