VLCOpenNetworkStreamViewController.m 6.2 KB

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