VLCPlaylistViewController.m 9.6 KB

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