VLCOpenNetworkStreamViewController.m 6.6 KB

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