VLCPlaylistViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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:.0];
  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. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  101. [self updateViewContents];
  102. }
  103. - (void)_displayEmptyLibraryViewIfNeeded
  104. {
  105. if (_foundMedia.count > 0) {
  106. if (self.emptyLibraryView.superview)
  107. [self.emptyLibraryView removeFromSuperview];
  108. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  109. } else {
  110. self.emptyLibraryView.frame = self.view.frame;
  111. [self.view addSubview:self.emptyLibraryView];
  112. self.navigationItem.rightBarButtonItem = nil;
  113. }
  114. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  115. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  116. UITableViewCellSeparatorStyleNone;
  117. }
  118. }
  119. - (void)updateViewContents
  120. {
  121. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  122. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  123. [self.tableView reloadData];
  124. else {
  125. [self.gridView reloadData];
  126. }
  127. [self _displayEmptyLibraryViewIfNeeded];
  128. }
  129. #pragma mark - Table View
  130. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  131. {
  132. return 1;
  133. }
  134. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  135. {
  136. return _foundMedia.count;
  137. }
  138. // Customize the appearance of table view cells.
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. static NSString *CellIdentifier = @"PlaylistCell";
  142. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  143. if (cell == nil)
  144. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  145. NSInteger row = indexPath.row;
  146. cell.mediaObject = _foundMedia[row];
  147. return cell;
  148. }
  149. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  152. }
  153. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. return YES;
  156. }
  157. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. if (editingStyle == UITableViewCellEditingStyleDelete)
  160. [self removeMediaObject: _foundMedia[indexPath.row]];
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. MLFile *mediaObject = _foundMedia[indexPath.row];
  165. if (!self.movieViewController)
  166. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  167. self.movieViewController.mediaItem = mediaObject;
  168. [self.navigationController pushViewController:self.movieViewController animated:YES];
  169. }
  170. #pragma mark - AQGridView
  171. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  172. {
  173. return _foundMedia.count;
  174. }
  175. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  176. {
  177. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  178. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  179. if (cell == nil) {
  180. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  181. cell.selectionStyle = AQGridViewCellSelectionStyleGlow;
  182. cell.gridView = gridView;
  183. }
  184. cell.mediaObject = _foundMedia[index];
  185. return cell;
  186. }
  187. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  188. {
  189. return [VLCPlaylistGridView preferredSize];
  190. }
  191. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  192. {
  193. [self.gridView deselectItemAtIndex:index animated:YES];
  194. MLFile *mediaObject = _foundMedia[index];
  195. if (!self.movieViewController)
  196. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  197. self.movieViewController.mediaItem = mediaObject;
  198. [self.navigationController pushViewController:self.movieViewController animated:YES];
  199. }
  200. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  201. {
  202. if (editingStyle == UITableViewCellEditingStyleDelete)
  203. [self removeMediaObject: _foundMedia[index]];
  204. }
  205. #pragma mark - UI implementation
  206. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  207. {
  208. [super setEditing:editing animated:animated];
  209. UIBarButtonItem *editButton = self.editButtonItem;
  210. NSString *editImage = editing? @"doneButton": @"button";
  211. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  212. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  213. barMetrics:UIBarMetricsDefault];
  214. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  215. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  216. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  217. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  218. [self.gridView setEditing:editing];
  219. else
  220. [self.tableView setEditing:editing animated:YES];
  221. }
  222. - (IBAction)leftButtonAction:(id)sender
  223. {
  224. if (self.menuViewController == nil)
  225. self.menuViewController = [[VLCMenuViewController alloc] initWithNibName:nil bundle:nil];
  226. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  227. self.menuViewController.contentSizeForViewInPopover = self.menuViewController.view.frame.size;
  228. if (self.addMediaPopoverController == nil) {
  229. self.addMediaPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.menuViewController];
  230. self.addMediaPopoverController.delegate = self;
  231. }
  232. if (self.addMediaPopoverController.popoverVisible)
  233. [self.addMediaPopoverController dismissPopoverAnimated:YES];
  234. else
  235. [self.addMediaPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem
  236. permittedArrowDirections:UIPopoverArrowDirectionUp
  237. animated:YES];
  238. } else
  239. [self.navigationController presentViewController:self.menuViewController animated:YES completion:NULL];
  240. }
  241. /* deprecated in iOS 6 */
  242. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  243. {
  244. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  245. return YES;
  246. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  247. }
  248. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  249. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  250. /* introduced in iOS 6 */
  251. - (NSUInteger)supportedInterfaceOrientations {
  252. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  253. return UIInterfaceOrientationMaskAll;
  254. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  255. UIInterfaceOrientationMaskPortrait;
  256. }
  257. /* introduced in iOS 6 */
  258. - (BOOL)shouldAutorotate {
  259. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  260. }
  261. #pragma mark - coin coin
  262. - (void)openMovieFromURL:(NSURL *)url
  263. {
  264. if (!self.movieViewController)
  265. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  266. self.movieViewController.url = url;
  267. [self.navigationController pushViewController:self.movieViewController animated:YES];
  268. }
  269. @end