VLCPlaylistViewController.m 12 KB

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