VLCOpenNetworkStreamViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. @interface VLCOpenNetworkStreamViewController ()
  15. {
  16. NSMutableArray *_recentURLs;
  17. }
  18. @end
  19. @implementation VLCOpenNetworkStreamViewController
  20. + (void)initialize
  21. {
  22. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  23. NSDictionary *appDefaults = @{kVLCRecentURLs : @[], kVLCPrivateWebStreaming : @(NO)};
  24. [defaults registerDefaults:appDefaults];
  25. }
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. [self.openButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  30. [self.privateModeLabel setText:NSLocalizedString(@"PRIVATE_PLAYBACK_TOGGLE", @"")];
  31. self.title = NSLocalizedString(@"OPEN_NETWORK", @"");
  32. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  33. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  34. }
  35. - (void)viewWillAppear:(BOOL)animated
  36. {
  37. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  38. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  39. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  40. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  41. pasteURL = [NSURL URLWithString:pasteString];
  42. }
  43. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  44. self.urlField.text = [pasteURL absoluteString];
  45. }
  46. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  47. _recentURLs = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCRecentURLs]];
  48. self.privateToggleSwitch.on = [defaults boolForKey:kVLCPrivateWebStreaming];
  49. [super viewWillAppear:animated];
  50. }
  51. - (void)viewWillDisappear:(BOOL)animated
  52. {
  53. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  54. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  55. [defaults setBool:self.privateToggleSwitch.on forKey:kVLCPrivateWebStreaming];
  56. [defaults synchronize];
  57. [super viewWillDisappear:animated];
  58. }
  59. - (CGSize)contentSizeForViewInPopover {
  60. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  61. }
  62. #pragma mark - UI interaction
  63. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  64. {
  65. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  66. return NO;
  67. return YES;
  68. }
  69. - (IBAction)goBack:(id)sender
  70. {
  71. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  72. }
  73. - (IBAction)openButtonAction:(id)sender
  74. {
  75. if ([self.urlField.text length] > 0) {
  76. if (!self.privateToggleSwitch.on) {
  77. if (_recentURLs.count >= 15)
  78. [_recentURLs removeLastObject];
  79. [_recentURLs addObject:self.urlField.text];
  80. [self.historyTableView reloadData];
  81. }
  82. [self _openURLStringAndDismiss:self.urlField.text];
  83. }
  84. }
  85. #pragma mark - table view data source
  86. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  87. {
  88. return 1;
  89. }
  90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  91. {
  92. return _recentURLs.count;
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. static NSString *CellIdentifier = @"StreamingHistoryCell";
  97. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  98. if (cell == nil) {
  99. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  100. cell.textLabel.textColor = [UIColor whiteColor];
  101. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  102. }
  103. NSInteger row = indexPath.row;
  104. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  105. cell.detailTextLabel.text = _recentURLs[row];
  106. return cell;
  107. }
  108. #pragma mark - table view delegate
  109. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  112. }
  113. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. return YES;
  116. }
  117. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. if (editingStyle == UITableViewCellEditingStyleDelete) {
  120. [_recentURLs removeObjectAtIndex:indexPath.row];
  121. [tableView reloadData];
  122. }
  123. }
  124. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  127. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  128. }
  129. #pragma mark - internals
  130. - (void)_openURLStringAndDismiss:(NSString *)url
  131. {
  132. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  133. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:url]];
  134. [self dismissModalViewControllerAnimated:YES];
  135. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  136. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  137. }
  138. @end