VLCPlaylistViewController.m 11 KB

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