VLCOpenNetworkStreamViewController.m 6.5 KB

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