VLCPlaylistViewController.m 17 KB

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