VLCOpenNetworkStreamViewController.m 5.6 KB

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