VLCOpenNetworkStreamViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 : @[]};
  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. self.navigationController.navigationBarHidden = NO;
  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. _recentURLs = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kVLCRecentURLs]];
  43. [super viewWillAppear:animated];
  44. }
  45. - (void)viewWillDisappear:(BOOL)animated
  46. {
  47. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  48. self.navigationController.navigationBarHidden = YES;
  49. [super viewWillDisappear:animated];
  50. }
  51. - (CGSize)contentSizeForViewInPopover {
  52. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  53. }
  54. #pragma mark - UI interaction
  55. - (IBAction)openButtonAction:(id)sender
  56. {
  57. if ([self.urlField.text length] > 0) {
  58. if (!self.privateToggleSwitch.on) {
  59. if (_recentURLs.count >= 15)
  60. [_recentURLs removeLastObject];
  61. [_recentURLs addObject:self.urlField.text];
  62. [self.historyTableView reloadData];
  63. }
  64. [self _openURLStringAndDismiss:self.urlField.text];
  65. }
  66. }
  67. - (IBAction)dismiss:(id)sender
  68. {
  69. [self.navigationController dismissModalViewControllerAnimated:YES];
  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. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  119. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  120. [defaults synchronize];
  121. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  122. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:url]];
  123. [self dismissModalViewControllerAnimated:YES];
  124. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  125. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  126. }
  127. @end