VLCPlaylistViewController.m 13 KB

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