VLCPlaylistViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  84. self.emptyLibraryView.frame = self.tableView.frame;
  85. else
  86. self.emptyLibraryView.frame = self.gridView.frame;
  87. [self.view addSubview:self.emptyLibraryView];
  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. }
  151. cell.mediaObject = _foundMedia[index];
  152. return cell;
  153. }
  154. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  155. {
  156. return [VLCPlaylistGridView preferredSize];
  157. }
  158. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  159. {
  160. [self.gridView deselectItemAtIndex:index animated:YES];
  161. MLFile *mediaObject = _foundMedia[index];
  162. if (!self.movieViewController)
  163. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  164. self.movieViewController.mediaItem = mediaObject;
  165. [self.navigationController pushViewController:self.movieViewController animated:YES];
  166. }
  167. #pragma mark - UI implementation
  168. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  169. {
  170. if (_editMode != editing)
  171. _editMode = editing;
  172. else
  173. _editMode = !editing;
  174. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  175. [self.gridView setEditing:_editMode];
  176. else
  177. [self.tableView setEditing:_editMode animated:YES];
  178. if (_editMode) {
  179. self.editButtonItem.style = UIBarButtonItemStyleDone;
  180. self.editButtonItem.title = NSLocalizedString(@"BUTTON_DONE",@"");
  181. } else {
  182. self.editButtonItem.style = UIBarButtonItemStylePlain;
  183. self.editButtonItem.title = NSLocalizedString(@"BUTTON_EDIT",@"");
  184. }
  185. }
  186. - (IBAction)leftButtonAction:(id)sender
  187. {
  188. if (self.addMediaViewController == nil)
  189. self.addMediaViewController = [[VLCAddMediaViewController alloc] initWithNibName:@"VLCAddMediaViewController" bundle:nil];
  190. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  191. self.addMediaViewController.contentSizeForViewInPopover = self.addMediaViewController.view.frame.size;
  192. if (self.addMediaPopoverController == nil) {
  193. self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.addMediaViewController];
  194. self.addMediaPopoverController.delegate = self;
  195. }
  196. if (self.addMediaPopoverController.popoverVisible)
  197. [self.addMediaPopoverController dismissPopoverAnimated:YES];
  198. else
  199. [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
  200. permittedArrowDirections:UIPopoverArrowDirectionUp
  201. animated:YES];
  202. } else
  203. [self.navigationController presentViewController:self.addMediaViewController animated:YES completion:NULL];
  204. }
  205. /* deprecated in iOS 6 */
  206. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  207. {
  208. if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
  209. return YES;
  210. if (_foundMedia.count > 0)
  211. return YES;
  212. else
  213. return NO;
  214. }
  215. /* introduced in iOS 6 */
  216. - (NSUInteger)supportedInterfaceOrientations {
  217. if (_foundMedia.count > 0)
  218. return UIInterfaceOrientationMaskAll;
  219. else
  220. return UIInterfaceOrientationMaskPortrait;
  221. }
  222. /* introduced in iOS 6 */
  223. - (BOOL)shouldAutorotate {
  224. if (_foundMedia.count > 0)
  225. return YES;
  226. else
  227. return NO;
  228. }
  229. #pragma mark - coin coin
  230. - (void)openMovieFromURL:(NSURL *)url
  231. {
  232. if (!self.movieViewController)
  233. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:@"VLCMovieViewController" bundle:nil];
  234. self.movieViewController.url = url;
  235. [self.navigationController pushViewController:self.movieViewController animated:YES];
  236. }
  237. @end