VLCOpenNetworkStreamViewController.m 4.7 KB

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