VLCPlaylistViewController.m 13 KB

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