VLCOpenNetworkStreamViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
  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. self.urlField.delegate = self;
  37. self.urlField.keyboardType = UIKeyboardTypeURL;
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. self.navigationController.navigationBar.translucent = NO;
  42. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  43. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  44. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  45. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  46. pasteURL = [NSURL URLWithString:pasteString];
  47. }
  48. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  49. self.urlField.text = [pasteURL absoluteString];
  50. }
  51. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  52. _recentURLs = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCRecentURLs]];
  53. self.privateToggleSwitch.on = [defaults boolForKey:kVLCPrivateWebStreaming];
  54. [super viewWillAppear:animated];
  55. }
  56. - (void)viewWillDisappear:(BOOL)animated
  57. {
  58. if (SYSTEM_RUNS_IN_THE_FUTURE)
  59. self.navigationController.navigationBar.translucent = YES;
  60. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  61. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  62. [defaults setBool:self.privateToggleSwitch.on forKey:kVLCPrivateWebStreaming];
  63. [defaults synchronize];
  64. [super viewWillDisappear:animated];
  65. }
  66. - (CGSize)contentSizeForViewInPopover {
  67. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  68. }
  69. #pragma mark - UI interaction
  70. - (BOOL)shouldAutorotate
  71. {
  72. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  73. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  74. return NO;
  75. return YES;
  76. }
  77. - (IBAction)goBack:(id)sender
  78. {
  79. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  80. }
  81. - (IBAction)openButtonAction:(id)sender
  82. {
  83. if ([self.urlField.text length] > 0) {
  84. if (!self.privateToggleSwitch.on) {
  85. if ([_recentURLs indexOfObject:self.urlField.text] != NSNotFound)
  86. [_recentURLs removeObject:self.urlField.text];
  87. if (_recentURLs.count >= 15)
  88. [_recentURLs removeLastObject];
  89. [_recentURLs addObject:self.urlField.text];
  90. [self.historyTableView reloadData];
  91. }
  92. [self _openURLStringAndDismiss:self.urlField.text];
  93. }
  94. }
  95. #pragma mark - table view data source
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  97. {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. return _recentURLs.count;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. static NSString *CellIdentifier = @"StreamingHistoryCell";
  107. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  108. if (cell == nil) {
  109. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  110. cell.textLabel.textColor = [UIColor whiteColor];
  111. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  112. }
  113. NSInteger row = indexPath.row;
  114. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  115. cell.detailTextLabel.text = _recentURLs[row];
  116. return cell;
  117. }
  118. #pragma mark - table view delegate
  119. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  122. }
  123. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. return YES;
  126. }
  127. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. if (editingStyle == UITableViewCellEditingStyleDelete) {
  130. [_recentURLs removeObjectAtIndex:indexPath.row];
  131. [tableView reloadData];
  132. }
  133. }
  134. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  135. {
  136. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  137. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  138. }
  139. #pragma mark - internals
  140. - (void)_openURLStringAndDismiss:(NSString *)url
  141. {
  142. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  143. [appDelegate.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  144. [appDelegate.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:[NSURL URLWithString:url] afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  145. }
  146. #pragma mark - text view delegate
  147. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  148. [self.urlField resignFirstResponder];
  149. return NO;
  150. }
  151. @end