VLCOpenNetworkStreamViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. - (IBAction)goBack:(id)sender
  63. {
  64. [self.navigationController popViewControllerAnimated:YES];
  65. }
  66. - (IBAction)openButtonAction:(id)sender
  67. {
  68. if ([self.urlField.text length] > 0) {
  69. if (!self.privateToggleSwitch.on) {
  70. if (_recentURLs.count >= 15)
  71. [_recentURLs removeLastObject];
  72. [_recentURLs addObject:self.urlField.text];
  73. [self.historyTableView reloadData];
  74. }
  75. [self _openURLStringAndDismiss:self.urlField.text];
  76. }
  77. }
  78. #pragma mark - table view data source
  79. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  80. {
  81. return 1;
  82. }
  83. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  84. {
  85. return _recentURLs.count;
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  88. {
  89. static NSString *CellIdentifier = @"StreamingHistoryCell";
  90. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  91. if (cell == nil) {
  92. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  93. cell.textLabel.textColor = [UIColor whiteColor];
  94. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  95. }
  96. NSInteger row = indexPath.row;
  97. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  98. cell.detailTextLabel.text = _recentURLs[row];
  99. return cell;
  100. }
  101. #pragma mark - table view delegate
  102. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  103. {
  104. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  105. }
  106. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  107. {
  108. return YES;
  109. }
  110. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. if (editingStyle == UITableViewCellEditingStyleDelete) {
  113. [_recentURLs removeObjectAtIndex:indexPath.row];
  114. [tableView reloadData];
  115. }
  116. }
  117. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  120. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  121. }
  122. #pragma mark - internals
  123. - (void)_openURLStringAndDismiss:(NSString *)url
  124. {
  125. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  126. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:url]];
  127. [self dismissModalViewControllerAnimated:YES];
  128. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  129. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  130. }
  131. @end