VLCOpenNetworkStreamViewController.m 5.1 KB

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