VLCOpenNetworkStreamViewController.m 4.9 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 : @[]};
  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. #pragma mark - table view data source
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  69. {
  70. return 1;
  71. }
  72. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  73. {
  74. return _recentURLs.count;
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. static NSString *CellIdentifier = @"StreamingHistoryCell";
  79. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  80. if (cell == nil) {
  81. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  82. cell.textLabel.textColor = [UIColor whiteColor];
  83. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  84. }
  85. NSInteger row = indexPath.row;
  86. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  87. cell.detailTextLabel.text = _recentURLs[row];
  88. return cell;
  89. }
  90. #pragma mark - table view delegate
  91. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  94. }
  95. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. return YES;
  98. }
  99. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101. if (editingStyle == UITableViewCellEditingStyleDelete) {
  102. [_recentURLs removeObjectAtIndex:indexPath.row];
  103. [tableView reloadData];
  104. }
  105. }
  106. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  107. {
  108. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  109. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  110. }
  111. #pragma mark - internals
  112. - (void)_openURLStringAndDismiss:(NSString *)url
  113. {
  114. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  115. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  116. [defaults synchronize];
  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