VLCPlaylistViewController.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "VLCMenuViewController.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 = @"VLC";
  23. return self;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"vlc"] style:UIBarButtonItemStyleBordered target:self action:@selector(leftButtonAction:)];
  29. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  30. * wearing a Father Xmas hat.
  31. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  32. * and should not be confused with the idea of religious statements or propagation there off
  33. */
  34. NSCalendar *gregorian =
  35. [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  36. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
  37. if (dayOfYear >= 354)
  38. addButton.image = [UIImage imageNamed:@"vlc-xmas"];
  39. self.navigationItem.leftBarButtonItem = addButton;
  40. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  41. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  42. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  43. _gridView.alwaysBounceVertical = YES;
  44. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  45. } else {
  46. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  47. _tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  48. }
  49. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  50. self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
  51. self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  52. self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  53. [self.emptyLibraryLongDescriptionLabel sizeToFit];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated
  56. {
  57. [super viewWillAppear:animated];
  58. [self _displayEmptyLibraryViewIfNeeded];
  59. }
  60. - (void)viewDidAppear:(BOOL)animated
  61. {
  62. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
  63. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  64. [super viewDidAppear:animated];
  65. }
  66. - (void)viewDidDisappear:(BOOL)animated
  67. {
  68. [super viewDidDisappear:animated];
  69. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  70. }
  71. - (void)removeMediaObject:(MLFile *)mediaObject
  72. {
  73. [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  74. [self updateViewContents];
  75. }
  76. - (void)_displayEmptyLibraryViewIfNeeded
  77. {
  78. if (_foundMedia.count > 0) {
  79. if (self.emptyLibraryView.superview)
  80. [self.emptyLibraryView removeFromSuperview];
  81. } else {
  82. self.emptyLibraryView.frame = self.view.frame;
  83. [self.view addSubview:self.emptyLibraryView];
  84. }
  85. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  86. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  87. UITableViewCellSeparatorStyleNone;
  88. }
  89. }
  90. - (void)updateViewContents
  91. {
  92. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  93. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  94. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  95. [self.tableView reloadData];
  96. else {
  97. [self.gridView reloadData];
  98. [self.gridView setNeedsDisplay];
  99. }
  100. [self _displayEmptyLibraryViewIfNeeded];
  101. }
  102. #pragma mark - Table View
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  104. {
  105. return 1;
  106. }
  107. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  108. {
  109. return _foundMedia.count;
  110. }
  111. // Customize the appearance of table view cells.
  112. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  113. {
  114. static NSString *CellIdentifier = @"PlaylistCell";
  115. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  116. if (cell == nil)
  117. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  118. cell.mediaObject = _foundMedia[indexPath.row];
  119. return cell;
  120. }
  121. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. return YES;
  124. }
  125. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. if (editingStyle == UITableViewCellEditingStyleDelete)
  128. [self removeMediaObject: _foundMedia[indexPath.row]];
  129. }
  130. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  131. {
  132. MLFile *mediaObject = _foundMedia[indexPath.row];
  133. if (!self.movieViewController)
  134. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  135. self.movieViewController.mediaItem = mediaObject;
  136. [self.navigationController pushViewController:self.movieViewController animated:YES];
  137. }
  138. #pragma mark - AQGridView
  139. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  140. {
  141. return _foundMedia.count;
  142. }
  143. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  144. {
  145. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  146. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  147. if (cell == nil) {
  148. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  149. cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  150. cell.gridView = gridView;
  151. }
  152. cell.mediaObject = _foundMedia[index];
  153. return cell;
  154. }
  155. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  156. {
  157. return [VLCPlaylistGridView preferredSize];
  158. }
  159. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  160. {
  161. [self.gridView deselectItemAtIndex:index animated:YES];
  162. MLFile *mediaObject = _foundMedia[index];
  163. if (!self.movieViewController)
  164. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  165. self.movieViewController.mediaItem = mediaObject;
  166. [self.navigationController pushViewController:self.movieViewController animated:YES];
  167. }
  168. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  169. {
  170. if (editingStyle == UITableViewCellEditingStyleDelete)
  171. [self removeMediaObject: _foundMedia[index]];
  172. }
  173. #pragma mark - UI implementation
  174. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  175. {
  176. [super setEditing:editing animated:animated];
  177. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  178. [self.gridView setEditing:editing];
  179. else
  180. [self.tableView setEditing:editing animated:YES];
  181. }
  182. - (IBAction)leftButtonAction:(id)sender
  183. {
  184. if (self.addMediaViewController == nil)
  185. self.addMediaViewController = [[VLCMenuViewController alloc] initWithNibName:@"VLCAddMediaViewController" bundle:nil];
  186. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  187. self.addMediaViewController.contentSizeForViewInPopover = self.addMediaViewController.view.frame.size;
  188. if (self.addMediaPopoverController == nil) {
  189. self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.addMediaViewController];
  190. self.addMediaPopoverController.delegate = self;
  191. }
  192. if (self.addMediaPopoverController.popoverVisible)
  193. [self.addMediaPopoverController dismissPopoverAnimated:YES];
  194. else
  195. [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
  196. permittedArrowDirections:UIPopoverArrowDirectionUp
  197. animated:YES];
  198. } else
  199. [self.navigationController presentViewController:self.addMediaViewController animated:YES completion:NULL];
  200. }
  201. /* deprecated in iOS 6 */
  202. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  203. {
  204. return (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (_foundMedia.count > 0);
  205. }
  206. /* introduced in iOS 6 */
  207. - (NSUInteger)supportedInterfaceOrientations {
  208. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAll: UIInterfaceOrientationMaskPortrait;
  209. }
  210. /* introduced in iOS 6 */
  211. - (BOOL)shouldAutorotate {
  212. return (_foundMedia.count > 0);
  213. }
  214. #pragma mark - coin coin
  215. - (void)openMovieFromURL:(NSURL *)url
  216. {
  217. if (!self.movieViewController)
  218. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  219. self.movieViewController.url = url;
  220. [self.navigationController pushViewController:self.movieViewController animated:YES];
  221. }
  222. @end