VLCPlaylistViewController.m 13 KB

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