VLCPlaylistViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // VLCMasterViewController.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCPlaylistViewController.h"
  9. #import "VLCMovieViewController.h"
  10. #import "VLCPlaylistTableViewCell.h"
  11. #import "VLCPlaylistGridViewCell.h"
  12. #import "VLCAboutViewController.h"
  13. @interface VLCPlaylistViewController () {
  14. NSMutableArray *_foundMedia;
  15. }
  16. @end
  17. @implementation VLCPlaylistViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self)
  22. self.title = @"Aspen";
  23. return self;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. self.tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  28. self.tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  29. [super viewDidLoad];
  30. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(showAboutView:)];
  31. self.navigationItem.leftBarButtonItem = addButton;
  32. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  33. [self updateViewContents];
  34. [[MLMediaLibrary sharedMediaLibrary] libraryDidAppear];
  35. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  36. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  37. _gridView.alwaysBounceVertical = YES;
  38. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  39. } else
  40. self.tabBar.selectedItem = self.localFilesBarItem;
  41. }
  42. #pragma mark - Table View
  43. - (void)updateViewContents
  44. {
  45. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  46. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  47. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  48. [self.tableView reloadData];
  49. else
  50. [self.gridView reloadData];
  51. }
  52. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  53. {
  54. return 1;
  55. }
  56. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  57. {
  58. return _foundMedia.count;
  59. }
  60. // Customize the appearance of table view cells.
  61. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  62. {
  63. static NSString *CellIdentifier = @"Cell";
  64. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  65. if (cell == nil)
  66. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  67. MLFile *object = _foundMedia[indexPath.row];
  68. cell.titleLabel.text = object.title;
  69. cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
  70. cell.thumbnailView.image = object.computedThumbnail;
  71. return cell;
  72. }
  73. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  74. {
  75. return YES;
  76. }
  77. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  78. {
  79. if (editingStyle == UITableViewCellEditingStyleDelete) {
  80. [_foundMedia removeObjectAtIndex:indexPath.row];
  81. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  82. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  83. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
  84. }
  85. }
  86. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. MLFile *mediaObject = _foundMedia[indexPath.row];
  89. if (!self.movieViewController) {
  90. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  91. }
  92. self.movieViewController.mediaItem = mediaObject;
  93. [self.navigationController pushViewController:self.movieViewController animated:YES];
  94. }
  95. #pragma mark - AQGridView
  96. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  97. {
  98. return _foundMedia.count;
  99. }
  100. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  101. {
  102. static NSString *AQCellIdentifier = @"AQCell";
  103. VLCPlaylistGridViewCell *cell = (VLCPlaylistGridViewCell *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  104. if (cell == nil) {
  105. cell = [[VLCPlaylistGridViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 384.,216.) reuseIdentifier:AQCellIdentifier];
  106. cell.selectionStyle = AQGridViewCellSelectionStyleBlueGray;
  107. }
  108. MLFile *object = _foundMedia[index];
  109. cell.title = object.title;
  110. cell.subtitle = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
  111. cell.thumbnail = object.computedThumbnail;
  112. return cell;
  113. }
  114. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  115. {
  116. static CGSize cellSize = { 384., 216. };
  117. return cellSize;
  118. }
  119. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  120. {
  121. MLFile *mediaObject = _foundMedia[index];
  122. if (!self.movieViewController)
  123. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  124. self.movieViewController.mediaItem = mediaObject;
  125. [self.navigationController pushViewController:self.movieViewController animated:YES];
  126. }
  127. #pragma mark - UI implementation
  128. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  129. {
  130. if (self.tableView.editing) {
  131. self.editButtonItem.style = UIBarButtonItemStylePlain;
  132. self.editButtonItem.title = NSLocalizedString(@"Edit", @"edit bar button item");
  133. [self.tableView setEditing:NO animated:YES];
  134. } else {
  135. self.editButtonItem.style = UIBarButtonItemStyleDone;
  136. self.editButtonItem.title = NSLocalizedString(@"Done", @"edit bar button item");
  137. [self.tableView setEditing:YES animated:YES];
  138. }
  139. }
  140. - (void)showAboutView:(id)sender
  141. {
  142. if (!self.aboutViewController)
  143. self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  144. [self.navigationController pushViewController:self.aboutViewController animated:YES];
  145. }
  146. #pragma mark - tab bar
  147. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
  148. {
  149. if (item == self.networkStreamsBarItem) {
  150. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.url", @"public.text", nil]]) {
  151. _pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  152. if (!_pasteURL || [[_pasteURL absoluteString] isEqualToString:@""]) {
  153. NSString * pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  154. _pasteURL = [NSURL URLWithString:pasteString];
  155. }
  156. if (_pasteURL && ![[_pasteURL scheme] isEqualToString:@""] && ![[_pasteURL absoluteString] isEqualToString:@""]) {
  157. NSString * messageString = [NSString stringWithFormat:@"Do you want to open %@?", [_pasteURL absoluteString]];
  158. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Open URL?" message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
  159. [alert show];
  160. }
  161. }
  162. }
  163. self.tabBar.selectedItem = self.localFilesBarItem;
  164. }
  165. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  166. {
  167. if (buttonIndex == 1)
  168. [self openMovieFromURL:_pasteURL];
  169. }
  170. - (void)openMovieFromURL:(NSURL *)url
  171. {
  172. if (!self.movieViewController)
  173. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  174. self.movieViewController.url = url;
  175. [self.navigationController pushViewController:self.movieViewController animated:YES];
  176. }
  177. @end