VLCPlaylistViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "UINavigationController+Theme.h"
  15. #import "NSString+SupportedMedia.h"
  16. #import "VLCBugreporter.h"
  17. #import "VLCAppDelegate.h"
  18. #import "UIBarButtonItem+Theme.h"
  19. @implementation EmptyLibraryView
  20. @end
  21. @interface VLCPlaylistViewController () <AQGridViewDataSource, AQGridViewDelegate, UITableViewDataSource, UITableViewDelegate> {
  22. NSMutableArray *_foundMedia;
  23. NSUInteger _libraryMode;
  24. UIBarButtonItem *_menuButton;
  25. }
  26. @end
  27. @implementation VLCPlaylistViewController
  28. - (void)loadView {
  29. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  30. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  31. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  32. _tableView.delegate = self;
  33. _tableView.dataSource = self;
  34. self.view = _tableView;
  35. } else {
  36. _gridView = [[AQGridView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  37. _gridView.delegate = self;
  38. _gridView.dataSource = self;
  39. _gridView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"libraryBackground"]];
  40. self.view = _gridView;
  41. }
  42. _libraryMode = kVLCLibraryModeAllFiles;
  43. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  44. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  45. }
  46. #pragma mark -
  47. - (void)viewDidLoad
  48. {
  49. [super viewDidLoad];
  50. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  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. _menuButton.image = [UIImage imageNamed:@"vlc-xmas"];
  61. self.navigationItem.leftBarButtonItem = _menuButton;
  62. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  63. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  64. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  65. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  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:.122 alpha:1.];
  73. }
  74. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  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. [super viewDidAppear:animated];
  89. if (_foundMedia.count < 1)
  90. [self performSelector:@selector(reloadContents) withObject:nil afterDelay:.0];
  91. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  92. }
  93. - (void)viewDidDisappear:(BOOL)animated
  94. {
  95. [super viewDidDisappear:animated];
  96. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  97. }
  98. - (BOOL)canBecomeFirstResponder
  99. {
  100. return YES;
  101. }
  102. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  103. {
  104. if (motion == UIEventSubtypeMotionShake)
  105. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  106. }
  107. - (void)removeMediaObject:(MLFile *)mediaObject
  108. {
  109. NSFileManager *fileManager = [NSFileManager defaultManager];
  110. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  111. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  112. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  113. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  114. return ([obj rangeOfString:fileName].location != NSNotFound);
  115. }];
  116. unsigned int count = indexSet.count;
  117. NSString *additionalFilePath;
  118. NSUInteger currentIndex = [indexSet firstIndex];
  119. for (unsigned int x = 0; x < count; x++) {
  120. additionalFilePath = allfiles[currentIndex];
  121. if ([additionalFilePath isSupportedSubtitleFormat])
  122. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  123. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  124. }
  125. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  126. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  127. [self reloadContents];
  128. }
  129. - (void)_displayEmptyLibraryViewIfNeeded
  130. {
  131. if (_foundMedia.count > 0) {
  132. if (self.emptyLibraryView.superview)
  133. [self.emptyLibraryView removeFromSuperview];
  134. } else {
  135. self.emptyLibraryView.frame = self.view.frame;
  136. [self.view addSubview:self.emptyLibraryView];
  137. }
  138. if (_libraryMode == kVLCLibraryModeAllFiles && _foundMedia.count > 0)
  139. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  140. else
  141. self.navigationItem.rightBarButtonItem = nil;
  142. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  143. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  144. UITableViewCellSeparatorStyleNone;
  145. }
  146. }
  147. - (void)reloadContents
  148. {
  149. if (_libraryMode == kVLCLibraryModeAllAlbums) {
  150. NSArray *rawAlbums = [MLAlbum allAlbums];
  151. _foundMedia = [[NSMutableArray alloc] init];
  152. NSUInteger count = rawAlbums.count;
  153. MLAlbum *album;
  154. for (NSUInteger x = 0; x < count; x++) {
  155. album = rawAlbums[x];
  156. if (album.name.length > 0 && album.tracks.count > 0)
  157. [_foundMedia addObject:album];
  158. }
  159. rawAlbums = nil;
  160. } else if (_libraryMode == kVLCLibraryModeAllSeries) {
  161. NSArray *rawShows = [MLShow allShows];
  162. _foundMedia = [[NSMutableArray alloc] init];
  163. NSUInteger count = rawShows.count;
  164. MLShow *show;
  165. for (NSUInteger x = 0; x < count; x++) {
  166. show = rawShows[x];
  167. if (show.name.length > 0 && show.episodes.count > 0)
  168. [_foundMedia addObject:show];
  169. }
  170. rawShows = nil;
  171. } else
  172. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  173. [self updateViewContents];
  174. }
  175. - (void)updateViewContents
  176. {
  177. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  178. [self.tableView reloadData];
  179. else {
  180. [self.gridView reloadData];
  181. [self.gridView setNeedsLayout];
  182. }
  183. [self _displayEmptyLibraryViewIfNeeded];
  184. }
  185. #pragma mark - Table View
  186. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  187. {
  188. return 1;
  189. }
  190. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  191. {
  192. return _foundMedia.count;
  193. }
  194. // Customize the appearance of table view cells.
  195. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. static NSString *CellIdentifier = @"PlaylistCell";
  198. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  199. if (cell == nil)
  200. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  201. NSInteger row = indexPath.row;
  202. cell.mediaObject = _foundMedia[row];
  203. return cell;
  204. }
  205. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  208. }
  209. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  210. {
  211. return YES;
  212. }
  213. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  214. {
  215. if (editingStyle == UITableViewCellEditingStyleDelete)
  216. [self removeMediaObject: _foundMedia[indexPath.row]];
  217. }
  218. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  219. {
  220. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  221. NSManagedObject *currentObject = _foundMedia[indexPath.row];
  222. if ([currentObject isKindOfClass:[MLAlbum class]]) {
  223. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)currentObject tracks] allObjects]];
  224. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  225. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  226. self.title = [(MLAlbum*)currentObject name];
  227. [self updateViewContents];
  228. } else if ([currentObject isKindOfClass:[MLShow class]]) {
  229. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)currentObject episodes] allObjects]];
  230. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  231. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  232. self.title = [(MLShow*)currentObject name];
  233. [self updateViewContents];
  234. } else {
  235. if (!self.movieViewController)
  236. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  237. if ([currentObject isKindOfClass:[MLFile class]])
  238. self.movieViewController.mediaItem = (MLFile *)currentObject;
  239. else if ([currentObject isKindOfClass:[MLAlbumTrack class]])
  240. self.movieViewController.mediaItem = [(MLAlbumTrack*)currentObject files].anyObject;
  241. else if ([currentObject isKindOfClass:[MLShowEpisode class]])
  242. self.movieViewController.mediaItem = [(MLShowEpisode*)currentObject files].anyObject;
  243. [self.navigationController pushViewController:self.movieViewController animated:YES];
  244. }
  245. }
  246. #pragma mark - AQGridView
  247. - (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
  248. {
  249. return _foundMedia.count;
  250. }
  251. - (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
  252. {
  253. static NSString *AQCellIdentifier = @"AQPlaylistCell";
  254. VLCPlaylistGridView *cell = (VLCPlaylistGridView *)[gridView dequeueReusableCellWithIdentifier:AQCellIdentifier];
  255. if (cell == nil) {
  256. cell = [[[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistGridView" owner:self options:nil] lastObject];
  257. cell.selectionStyle = AQGridViewCellSelectionStyleNone;
  258. cell.gridView = gridView;
  259. }
  260. cell.mediaObject = _foundMedia[index];
  261. return cell;
  262. }
  263. - (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
  264. {
  265. return [VLCPlaylistGridView preferredSize];
  266. }
  267. - (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
  268. {
  269. [self.gridView deselectItemAtIndex:index animated:YES];
  270. NSManagedObject *currentObject = _foundMedia[index];
  271. if ([currentObject isKindOfClass:[MLAlbum class]]) {
  272. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)currentObject tracks] allObjects]];
  273. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  274. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  275. self.title = [(MLAlbum*)currentObject name];
  276. [self updateViewContents];
  277. } else if ([currentObject isKindOfClass:[MLShow class]]) {
  278. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)currentObject episodes] allObjects]];
  279. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  280. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  281. self.title = [(MLShow*)currentObject name];
  282. [self updateViewContents];
  283. } else {
  284. if (!self.movieViewController)
  285. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  286. if ([currentObject isKindOfClass:[MLFile class]])
  287. self.movieViewController.mediaItem = (MLFile *)currentObject;
  288. else if ([currentObject isKindOfClass:[MLAlbumTrack class]])
  289. self.movieViewController.mediaItem = [(MLAlbumTrack*)currentObject files].anyObject;
  290. else if ([currentObject isKindOfClass:[MLShowEpisode class]])
  291. self.movieViewController.mediaItem = [(MLShowEpisode*)currentObject files].anyObject;
  292. [self.navigationController pushViewController:self.movieViewController animated:YES];
  293. }
  294. }
  295. - (void)gridView:(AQGridView *)aGridView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndex:(NSUInteger)index
  296. {
  297. if (editingStyle == UITableViewCellEditingStyleDelete)
  298. [self removeMediaObject: _foundMedia[index]];
  299. }
  300. #pragma mark - UI implementation
  301. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  302. {
  303. if (_libraryMode != kVLCLibraryModeAllFiles)
  304. return;
  305. [super setEditing:editing animated:animated];
  306. UIBarButtonItem *editButton = self.editButtonItem;
  307. NSString *editImage = editing? @"doneButton": @"button";
  308. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  309. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  310. barMetrics:UIBarMetricsDefault];
  311. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  312. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  313. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  314. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  315. [self.gridView setEditing:editing];
  316. else
  317. [self.tableView setEditing:editing animated:YES];
  318. }
  319. - (IBAction)leftButtonAction:(id)sender
  320. {
  321. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  322. }
  323. - (IBAction)backToAllItems:(id)sender
  324. {
  325. self.navigationItem.leftBarButtonItem = _menuButton;
  326. [self setLibraryMode:_libraryMode];
  327. }
  328. /* deprecated in iOS 6 */
  329. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  330. {
  331. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  332. return YES;
  333. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  334. }
  335. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  336. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  337. /* introduced in iOS 6 */
  338. - (NSUInteger)supportedInterfaceOrientations {
  339. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  340. return UIInterfaceOrientationMaskAll;
  341. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  342. UIInterfaceOrientationMaskPortrait;
  343. }
  344. /* introduced in iOS 6 */
  345. - (BOOL)shouldAutorotate {
  346. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  347. }
  348. #pragma mark - coin coin
  349. - (void)openMovieFromURL:(NSURL *)url
  350. {
  351. if (!self.movieViewController)
  352. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  353. if (self.navigationController.topViewController != self.movieViewController)
  354. [self.navigationController pushViewController:self.movieViewController animated:YES];
  355. self.movieViewController.url = url;
  356. }
  357. - (void)setLibraryMode:(NSUInteger)mode
  358. {
  359. _libraryMode = mode;
  360. if (_libraryMode == kVLCLibraryModeAllAlbums)
  361. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  362. else if( _libraryMode == kVLCLibraryModeAllSeries)
  363. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  364. else
  365. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  366. [self reloadContents];
  367. }
  368. @end