VLCPlaylistViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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:NSLocalizedString(@"About",@"") style:UIBarButtonItemStyleBordered target:self action:@selector(showAboutView:)];
  31. self.navigationItem.leftBarButtonItem = addButton;
  32. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  33. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  34. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  35. _gridView.alwaysBounceVertical = YES;
  36. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  37. } else {
  38. self.tabBar.selectedItem = self.localFilesBarItem;
  39. self.networkStreamsBarItem.title = NSLocalizedString(@"Network",@"");
  40. }
  41. }
  42. - (void)viewDidAppear:(BOOL)animated
  43. {
  44. [super viewDidAppear:animated];
  45. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
  46. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  47. }
  48. - (void)viewDidDisappear:(BOOL)animated
  49. {
  50. [super viewDidDisappear:animated];
  51. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  52. }
  53. #pragma mark - Table View
  54. - (void)updateViewContents
  55. {
  56. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  57. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  58. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  59. [self.tableView reloadData];
  60. else
  61. [self.gridView reloadData];
  62. }
  63. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  64. {
  65. return 1;
  66. }
  67. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  68. {
  69. return _foundMedia.count;
  70. }
  71. // Customize the appearance of table view cells.
  72. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  73. {
  74. static NSString *CellIdentifier = @"Cell";
  75. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  76. if (cell == nil)
  77. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  78. MLFile *object = _foundMedia[indexPath.row];
  79. cell.titleLabel.text = object.title;
  80. cell.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
  81. cell.thumbnailView.image = object.computedThumbnail;
  82. cell.progressIndicator.progress = object.lastPosition.floatValue;
  83. if (cell.progressIndicator.progress < 0.1f)
  84. cell.progressIndicator.hidden = YES;
  85. return cell;
  86. }
  87. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  88. {
  89. return YES;
  90. }
  91. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. if (editingStyle == UITableViewCellEditingStyleDelete) {
  94. NSUInteger row = indexPath.row;
  95. MLFile *mediaObject = _foundMedia[row];
  96. [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  97. [_foundMedia removeObjectAtIndex:row];
  98. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  99. [self.gridView deleteItemsAtIndices:[NSIndexSet indexSetWithIndex:row] withAnimation:AQGridViewItemAnimationFade];
  100. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  101. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
  102. }
  103. }
  104. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. MLFile *mediaObject = _foundMedia[indexPath.row];
  107. if (!self.movieViewController) {
  108. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  109. }
  110. self.movieViewController.mediaItem = mediaObject;
  111. [self.navigationController pushViewController:self.movieViewController animated:YES];
  112. }
  113. #pragma mark - AQGridView
  114. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  115. {
  116. return _foundMedia.count;
  117. }
  118. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  119. {
  120. static NSString *AQCellIdentifier = @"AQCell";
  121. VLCPlaylistGridViewCell *cell = (VLCPlaylistGridViewCell *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  122. if (cell == nil) {
  123. cell = [[VLCPlaylistGridViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 384.,216.) reuseIdentifier:AQCellIdentifier];
  124. cell.selectionStyle = AQGridViewCellSelectionStyleBlueGray;
  125. }
  126. MLFile *object = _foundMedia[index];
  127. cell.title = object.title;
  128. cell.subtitle = [NSString stringWithFormat:@"%@ — %.2f MB", [VLCTime timeWithNumber:[object duration]], [object fileSizeInBytes] / 2e6];
  129. cell.thumbnail = object.computedThumbnail;
  130. return cell;
  131. }
  132. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  133. {
  134. static CGSize cellSize = { 384., 216. };
  135. return cellSize;
  136. }
  137. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  138. {
  139. MLFile *mediaObject = _foundMedia[index];
  140. if (!self.movieViewController)
  141. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  142. self.movieViewController.mediaItem = mediaObject;
  143. [self.navigationController pushViewController:self.movieViewController animated:YES];
  144. }
  145. #pragma mark - UI implementation
  146. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  147. {
  148. if (self.tableView.editing) {
  149. self.editButtonItem.style = UIBarButtonItemStylePlain;
  150. self.editButtonItem.title = NSLocalizedString(@"Edit",@"");
  151. [self.tableView setEditing:NO animated:YES];
  152. } else {
  153. self.editButtonItem.style = UIBarButtonItemStyleDone;
  154. self.editButtonItem.title = NSLocalizedString(@"Done",@"");
  155. [self.tableView setEditing:YES animated:YES];
  156. }
  157. }
  158. - (void)showAboutView:(id)sender
  159. {
  160. if (!self.aboutViewController)
  161. self.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  162. [self.navigationController pushViewController:self.aboutViewController animated:YES];
  163. }
  164. #pragma mark - tab bar
  165. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
  166. {
  167. if (item == self.networkStreamsBarItem) {
  168. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.url", @"public.text", nil]]) {
  169. _pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  170. if (!_pasteURL || [[_pasteURL absoluteString] isEqualToString:@""]) {
  171. NSString * pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  172. _pasteURL = [NSURL URLWithString:pasteString];
  173. }
  174. if (_pasteURL && ![[_pasteURL scheme] isEqualToString:@""] && ![[_pasteURL absoluteString] isEqualToString:@""]) {
  175. NSString * messageString = [NSString stringWithFormat:@"Do you want to open %@?", [_pasteURL absoluteString]];
  176. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Open URL?" message:messageString delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
  177. [alert show];
  178. }
  179. }
  180. }
  181. self.tabBar.selectedItem = self.localFilesBarItem;
  182. }
  183. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  184. {
  185. if (buttonIndex == 1)
  186. [self openMovieFromURL:_pasteURL];
  187. }
  188. - (void)openMovieFromURL:(NSURL *)url
  189. {
  190. if (!self.movieViewController)
  191. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  192. self.movieViewController.url = url;
  193. [self.navigationController pushViewController:self.movieViewController animated:YES];
  194. }
  195. @end