VLCOpenNetworkStreamViewController.m 5.0 KB

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