VLCPlaylistViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "VLCPlaylistCollectionViewCell.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. #ifndef UIStatusBarStyleLightContent
  20. #define UIStatusBarStyleLightContent 1
  21. #endif
  22. @implementation EmptyLibraryView
  23. @end
  24. @interface VLCPlaylistViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary> {
  25. NSMutableArray *_foundMedia;
  26. VLCLibraryMode _libraryMode;
  27. UIBarButtonItem *_menuButton;
  28. }
  29. @property (nonatomic, strong) UITableView *tableView;
  30. @property (nonatomic, strong) UICollectionView *collectionView;
  31. @property (nonatomic, strong) EmptyLibraryView *emptyLibraryView;
  32. @end
  33. @implementation VLCPlaylistViewController
  34. - (void)loadView {
  35. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  36. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  37. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  38. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  39. _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  40. _tableView.delegate = self;
  41. _tableView.dataSource = self;
  42. self.view = _tableView;
  43. } else {
  44. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  45. _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
  46. _collectionView.alwaysBounceVertical = YES;
  47. _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  48. _collectionView.delegate = self;
  49. _collectionView.dataSource = self;
  50. self.view = _collectionView;
  51. [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  52. self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"libraryBackground"]];
  53. }
  54. _libraryMode = VLCLibraryModeAllFiles;
  55. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  56. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  57. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
  58. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  59. }
  60. #pragma mark -
  61. - (void)viewDidLoad
  62. {
  63. [super viewDidLoad];
  64. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  65. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  66. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  67. * wearing a Father Xmas hat.
  68. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  69. * and should not be confused with the idea of religious statements or propagation there off
  70. */
  71. NSCalendar *gregorian =
  72. [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  73. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
  74. if (dayOfYear >= 354)
  75. _menuButton.image = [UIImage imageNamed:@"vlc-xmas"];
  76. self.navigationItem.leftBarButtonItem = _menuButton;
  77. if (SYSTEM_RUNS_IN_THE_FUTURE)
  78. self.editButtonItem.tintColor = [UIColor whiteColor];
  79. else {
  80. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  81. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  82. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  83. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  84. }
  85. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  86. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  87. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  88. }
  89. - (void)viewWillAppear:(BOOL)animated
  90. {
  91. [super viewWillAppear:animated];
  92. [self _displayEmptyLibraryViewIfNeeded];
  93. }
  94. - (void)viewDidAppear:(BOOL)animated
  95. {
  96. [super viewDidAppear:animated];
  97. if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
  98. self.navigationItem.rightBarButtonItem = nil;
  99. self.navigationItem.leftBarButtonItem = nil;
  100. self.title = @"";
  101. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
  102. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
  103. [self.emptyLibraryView.activityIndicator startAnimating];
  104. self.emptyLibraryView.frame = self.view.bounds;
  105. [self.view addSubview:self.emptyLibraryView];
  106. [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
  107. [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
  108. return;
  109. }
  110. if (_foundMedia.count < 1)
  111. [self updateViewContents];
  112. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  113. if (SYSTEM_RUNS_IN_THE_FUTURE)
  114. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  115. }
  116. - (void)viewDidDisappear:(BOOL)animated
  117. {
  118. [super viewDidDisappear:animated];
  119. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  120. }
  121. - (BOOL)canBecomeFirstResponder
  122. {
  123. return YES;
  124. }
  125. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  126. {
  127. if (motion == UIEventSubtypeMotionShake)
  128. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  129. }
  130. - (void)openMediaObject:(NSManagedObject *)mediaObject
  131. {
  132. if ([mediaObject isKindOfClass:[MLAlbum class]]) {
  133. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)mediaObject tracks] allObjects]];
  134. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  135. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  136. self.title = [(MLAlbum*)mediaObject name];
  137. [self reloadViews];
  138. } else if ([mediaObject isKindOfClass:[MLShow class]]) {
  139. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)mediaObject episodes] allObjects]];
  140. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  141. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  142. self.title = [(MLShow*)mediaObject name];
  143. [self reloadViews];
  144. } else {
  145. if (!self.movieViewController)
  146. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  147. if ([mediaObject isKindOfClass:[MLFile class]])
  148. self.movieViewController.mediaItem = (MLFile *)mediaObject;
  149. else if ([mediaObject isKindOfClass:[MLAlbumTrack class]])
  150. self.movieViewController.mediaItem = [(MLAlbumTrack*)mediaObject files].anyObject;
  151. else if ([mediaObject isKindOfClass:[MLShowEpisode class]])
  152. self.movieViewController.mediaItem = [(MLShowEpisode*)mediaObject files].anyObject;
  153. [self.navigationController pushViewController:self.movieViewController animated:YES];
  154. }
  155. }
  156. - (void)removeMediaObject:(MLFile *)mediaObject
  157. {
  158. NSFileManager *fileManager = [NSFileManager defaultManager];
  159. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  160. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  161. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  162. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  163. return ([obj rangeOfString:fileName].location != NSNotFound);
  164. }];
  165. unsigned int count = indexSet.count;
  166. NSString *additionalFilePath;
  167. NSUInteger currentIndex = [indexSet firstIndex];
  168. for (unsigned int x = 0; x < count; x++) {
  169. additionalFilePath = allfiles[currentIndex];
  170. if ([additionalFilePath isSupportedSubtitleFormat])
  171. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  172. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  173. }
  174. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  175. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  176. [self updateViewContents];
  177. }
  178. - (void)_displayEmptyLibraryViewIfNeeded
  179. {
  180. if (self.emptyLibraryView.superview)
  181. [self.emptyLibraryView removeFromSuperview];
  182. if (_foundMedia.count == 0) {
  183. self.emptyLibraryView.frame = self.view.bounds;
  184. [self.view addSubview:self.emptyLibraryView];
  185. }
  186. if (_libraryMode == VLCLibraryModeAllFiles && _foundMedia.count > 0)
  187. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  188. else
  189. self.navigationItem.rightBarButtonItem = nil;
  190. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  191. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  192. UITableViewCellSeparatorStyleNone;
  193. }
  194. }
  195. - (void)libraryUpgradeComplete
  196. {
  197. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  198. self.navigationItem.leftBarButtonItem = _menuButton;
  199. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  200. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  201. [self.emptyLibraryView.activityIndicator stopAnimating];
  202. [self.emptyLibraryView removeFromSuperview];
  203. [self updateViewContents];
  204. }
  205. - (void)libraryWasUpdated
  206. {
  207. NSLog(@"libraryWasUpdated");
  208. [self updateViewContents];
  209. }
  210. - (void)updateViewContents
  211. {
  212. if (_libraryMode == VLCLibraryModeAllAlbums) {
  213. NSArray *rawAlbums = [MLAlbum allAlbums];
  214. _foundMedia = [[NSMutableArray alloc] init];
  215. for (MLAlbum *album in rawAlbums) {
  216. if (album.name.length > 0 && album.tracks.count > 0)
  217. [_foundMedia addObject:album];
  218. }
  219. } else if (_libraryMode == VLCLibraryModeAllSeries) {
  220. NSArray *rawShows = [MLShow allShows];
  221. _foundMedia = [[NSMutableArray alloc] init];
  222. for (MLShow *show in rawShows) {
  223. if (show.name.length > 0 && show.episodes.count > 0)
  224. [_foundMedia addObject:show];
  225. }
  226. } else
  227. _foundMedia = [NSMutableArray arrayWithArray:[MLFile allFiles]];
  228. [self reloadViews];
  229. }
  230. - (void)reloadViews
  231. {
  232. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  233. [self.tableView reloadData];
  234. else
  235. [self.collectionView reloadData];
  236. [self _displayEmptyLibraryViewIfNeeded];
  237. }
  238. #pragma mark - Table View
  239. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  240. {
  241. return 1;
  242. }
  243. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  244. {
  245. return _foundMedia.count;
  246. }
  247. // Customize the appearance of table view cells.
  248. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  249. {
  250. static NSString *CellIdentifier = @"PlaylistCell";
  251. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  252. if (cell == nil)
  253. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  254. NSInteger row = indexPath.row;
  255. cell.mediaObject = _foundMedia[row];
  256. return cell;
  257. }
  258. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  259. {
  260. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  261. }
  262. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  263. {
  264. return YES;
  265. }
  266. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  267. {
  268. if (editingStyle == UITableViewCellEditingStyleDelete)
  269. [self removeMediaObject: _foundMedia[indexPath.row]];
  270. }
  271. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  272. {
  273. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  274. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  275. [self openMediaObject:selectedObject];
  276. }
  277. #pragma mark - Collection View
  278. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  279. {
  280. return _foundMedia.count;
  281. }
  282. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  283. {
  284. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  285. cell.mediaObject = _foundMedia[indexPath.row];
  286. cell.collectionView = _collectionView;
  287. [cell setEditing:self.editing animated:NO];
  288. return cell;
  289. }
  290. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  291. {
  292. return CGSizeMake(298.0, 220.0);
  293. }
  294. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  295. {
  296. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  297. }
  298. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  301. [self openMediaObject:selectedObject];
  302. }
  303. #pragma mark - UI implementation
  304. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  305. {
  306. if (_libraryMode != VLCLibraryModeAllFiles)
  307. return;
  308. [super setEditing:editing animated:animated];
  309. UIBarButtonItem *editButton = self.editButtonItem;
  310. NSString *editImage = editing? @"doneButton": @"button";
  311. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  312. if (SYSTEM_RUNS_IN_THE_FUTURE)
  313. editButton.tintColor = [UIColor whiteColor];
  314. else {
  315. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  316. barMetrics:UIBarMetricsDefault];
  317. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  318. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  319. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  320. }
  321. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  322. NSArray *visibleCells = self.collectionView.visibleCells;
  323. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  324. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  325. [aCell setEditing:editing animated:animated];
  326. }];
  327. } else
  328. [self.tableView setEditing:editing animated:YES];
  329. }
  330. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  331. {
  332. if (_libraryMode != VLCLibraryModeAllFiles)
  333. return UITableViewCellEditingStyleNone;
  334. return UITableViewCellEditingStyleDelete;
  335. }
  336. - (IBAction)leftButtonAction:(id)sender
  337. {
  338. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  339. }
  340. - (IBAction)backToAllItems:(id)sender
  341. {
  342. self.navigationItem.leftBarButtonItem = _menuButton;
  343. [self setLibraryMode:_libraryMode];
  344. }
  345. #pragma mark - coin coin
  346. - (void)openMovieFromURL:(NSURL *)url
  347. {
  348. if (!self.movieViewController)
  349. self.movieViewController = [[VLCMovieViewController alloc] initWithNibName:nil bundle:nil];
  350. if (self.navigationController.topViewController != self.movieViewController)
  351. [self.navigationController pushViewController:self.movieViewController animated:YES];
  352. self.movieViewController.url = url;
  353. }
  354. - (void)setLibraryMode:(VLCLibraryMode)mode
  355. {
  356. _libraryMode = mode;
  357. if (_libraryMode == VLCLibraryModeAllAlbums)
  358. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  359. else if( _libraryMode == VLCLibraryModeAllSeries)
  360. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  361. else
  362. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  363. [self updateViewContents];
  364. }
  365. #pragma mark - autorotation
  366. /* deprecated in iOS 6 */
  367. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  368. {
  369. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  370. return YES;
  371. return (_foundMedia.count > 0) || toInterfaceOrientation == UIInterfaceOrientationPortrait;
  372. }
  373. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  374. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  375. /* introduced in iOS 6 */
  376. - (NSUInteger)supportedInterfaceOrientations {
  377. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  378. return UIInterfaceOrientationMaskAll;
  379. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  380. UIInterfaceOrientationMaskPortrait;
  381. }
  382. /* introduced in iOS 6 */
  383. - (BOOL)shouldAutorotate {
  384. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  385. }
  386. @end