VLCPlaylistViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "VLCAddMediaViewController.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.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT", @"");
  40. self.navigationItem.leftBarButtonItem = addButton;
  41. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  42. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  43. _gridView.separatorStyle = AQGridViewCellSeparatorStyleEmptySpace;
  44. _gridView.alwaysBounceVertical = YES;
  45. _gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  46. } else {
  47. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  48. _tableView.separatorColor = [UIColor colorWithWhite:.2 alpha:1.];
  49. }
  50. self.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  51. self.emptyLibraryLongDescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
  52. self.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  53. self.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  54. [self.emptyLibraryLongDescriptionLabel sizeToFit];
  55. }
  56. - (void)viewWillAppear:(BOOL)animated
  57. {
  58. [super viewWillAppear:animated];
  59. [self _displayEmptyLibraryViewIfNeeded];
  60. }
  61. - (void)viewDidAppear:(BOOL)animated
  62. {
  63. [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:.3];
  64. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  65. [super viewDidAppear:animated];
  66. }
  67. - (void)viewDidDisappear:(BOOL)animated
  68. {
  69. [super viewDidDisappear:animated];
  70. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  71. }
  72. - (void)removeMediaObject:(MLFile *)mediaObject
  73. {
  74. [[NSFileManager defaultManager] removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  75. [self updateViewContents];
  76. }
  77. - (void)_displayEmptyLibraryViewIfNeeded
  78. {
  79. if (_foundMedia.count > 0) {
  80. if (self.emptyLibraryView.superview)
  81. [self.emptyLibraryView removeFromSuperview];
  82. } else {
  83. self.emptyLibraryView.frame = self.view.frame;
  84. [self.view addSubview:self.emptyLibraryView];
  85. }
  86. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  87. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  88. UITableViewCellSeparatorStyleNone;
  89. }
  90. }
  91. - (void)updateViewContents
  92. {
  93. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  94. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  95. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  96. [self.tableView reloadData];
  97. else {
  98. [self.gridView reloadData];
  99. [self.gridView setNeedsDisplay];
  100. }
  101. [self _displayEmptyLibraryViewIfNeeded];
  102. }
  103. #pragma mark - Table View
  104. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  105. {
  106. return 1;
  107. }
  108. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  109. {
  110. return _foundMedia.count;
  111. }
  112. // Customize the appearance of table view cells.
  113. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. static NSString *CellIdentifier = @"PlaylistCell";
  116. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  117. if (cell == nil)
  118. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  119. cell.mediaObject = _foundMedia[indexPath.row];
  120. return cell;
  121. }
  122. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  123. {
  124. return YES;
  125. }
  126. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. if (editingStyle == UITableViewCellEditingStyleDelete)
  129. [self removeMediaObject: _foundMedia[indexPath.row]];
  130. }
  131. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. MLFile *mediaObject = _foundMedia[indexPath.row];
  134. if (!self.movieViewController)
  135. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  136. self.movieViewController.mediaItem = mediaObject;
  137. [self.navigationController pushViewController:self.movieViewController animated:YES];
  138. }
  139. #pragma mark - AQGridView
  140. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  141. {
  142. return _foundMedia.count;
  143. }
  144. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  145. {
  146. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  147. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  148. if (cell == nil) {
  149. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  150. cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  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. #pragma mark - UI implementation
  169. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  170. {
  171. if (_editMode != editing)
  172. _editMode = editing;
  173. else
  174. _editMode = !editing;
  175. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  176. [self.gridView setEditing:_editMode];
  177. else
  178. [self.tableView setEditing:_editMode animated:YES];
  179. if (_editMode) {
  180. self.editButtonItem.style = UIBarButtonItemStyleDone;
  181. self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
  182. } else {
  183. self.editButtonItem.style = UIBarButtonItemStylePlain;
  184. self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
  185. }
  186. }
  187. - (IBAction)leftButtonAction:(id)sender
  188. {
  189. if (self.addMediaViewController == nil)
  190. self.addMediaViewController = [[VLCAddMediaViewController alloc] initWithNibName:@"VLCAddMediaViewController" bundle:nil];
  191. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  192. self.addMediaViewController.contentSizeForViewInPopover = self.addMediaViewController.view.frame.size;
  193. if (self.addMediaPopoverController == nil) {
  194. self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.addMediaViewController];
  195. self.addMediaPopoverController.delegate = self;
  196. }
  197. if (self.addMediaPopoverController.popoverVisible)
  198. [self.addMediaPopoverController dismissPopoverAnimated:YES];
  199. else
  200. [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
  201. permittedArrowDirections:UIPopoverArrowDirectionUp
  202. animated:YES];
  203. } else
  204. [self.navigationController presentViewController:self.addMediaViewController animated:YES completion:NULL];
  205. }
  206. /* deprecated in iOS 6 */
  207. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  208. {
  209. return (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (_foundMedia.count > 0);
  210. }
  211. /* introduced in iOS 6 */
  212. - (NSUInteger)supportedInterfaceOrientations {
  213. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAll: UIInterfaceOrientationMaskPortrait;
  214. }
  215. /* introduced in iOS 6 */
  216. - (BOOL)shouldAutorotate {
  217. return (_foundMedia.count > 0);
  218. }
  219. #pragma mark - coin coin
  220. - (void)openMovieFromURL:(NSURL *)url
  221. {
  222. if (!self.movieViewController)
  223. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  224. self.movieViewController.url = url;
  225. [self.navigationController pushViewController:self.movieViewController animated:YES];
  226. }
  227. @end