VLCPlaylistViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*****************************************************************************
  2. * VLCPlaylistViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Tamas Timar <ttimar.vlc # gmail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCMovieViewController.h"
  16. #import "VLCPlaylistTableViewCell.h"
  17. #import "VLCPlaylistCollectionViewCell.h"
  18. #import "UINavigationController+Theme.h"
  19. #import "NSString+SupportedMedia.h"
  20. #import "VLCBugreporter.h"
  21. #import "VLCAppDelegate.h"
  22. #import "UIBarButtonItem+Theme.h"
  23. @implementation EmptyLibraryView
  24. @end
  25. @interface VLCPlaylistViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary> {
  26. NSMutableArray *_foundMedia;
  27. VLCLibraryMode _libraryMode;
  28. UIBarButtonItem *_menuButton;
  29. }
  30. @property (nonatomic, strong) UITableView *tableView;
  31. @property (nonatomic, strong) UICollectionView *collectionView;
  32. @property (nonatomic, strong) EmptyLibraryView *emptyLibraryView;
  33. @end
  34. @implementation VLCPlaylistViewController
  35. - (void)loadView {
  36. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  37. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  38. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  39. _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
  40. _tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  41. _tableView.delegate = self;
  42. _tableView.dataSource = self;
  43. _tableView.opaque = YES;
  44. self.view = _tableView;
  45. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  46. UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewLongTouchGestureAction:)];
  47. [self.view addGestureRecognizer:gestureRecognizer];
  48. }
  49. } else {
  50. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  51. _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
  52. _collectionView.alwaysBounceVertical = YES;
  53. _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  54. _collectionView.delegate = self;
  55. _collectionView.dataSource = self;
  56. _collectionView.opaque = YES;
  57. _collectionView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  58. self.view = _collectionView;
  59. if (SYSTEM_RUNS_IOS7_OR_LATER)
  60. [_collectionView registerNib:[UINib nibWithNibName:@"VLCFuturePlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  61. else
  62. [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
  63. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  64. }
  65. _libraryMode = VLCLibraryModeAllFiles;
  66. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  67. self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
  68. _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
  69. _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
  70. }
  71. #pragma mark -
  72. - (void)viewDidLoad
  73. {
  74. [super viewDidLoad];
  75. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  76. _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
  77. /* After day 354 of the year, the usual VLC cone is replaced by another cone
  78. * wearing a Father Xmas hat.
  79. * Note: this icon doesn't represent an endorsement of The Coca-Cola Company
  80. * and should not be confused with the idea of religious statements or propagation there off
  81. */
  82. NSCalendar *gregorian =
  83. [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  84. NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
  85. if (dayOfYear >= 354)
  86. _menuButton.image = [UIImage imageNamed:@"vlc-xmas"];
  87. self.navigationItem.leftBarButtonItem = _menuButton;
  88. if (SYSTEM_RUNS_IOS7_OR_LATER)
  89. self.editButtonItem.tintColor = [UIColor whiteColor];
  90. else {
  91. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  92. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  93. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  94. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  95. }
  96. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  97. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  98. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  99. if (SYSTEM_RUNS_IOS7_OR_LATER)
  100. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  101. }
  102. - (void)viewWillAppear:(BOOL)animated
  103. {
  104. [super viewWillAppear:animated];
  105. [self _displayEmptyLibraryViewIfNeeded];
  106. }
  107. - (void)viewDidAppear:(BOOL)animated
  108. {
  109. [super viewDidAppear:animated];
  110. if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
  111. self.navigationItem.rightBarButtonItem = nil;
  112. self.navigationItem.leftBarButtonItem = nil;
  113. self.title = @"";
  114. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
  115. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
  116. [self.emptyLibraryView.activityIndicator startAnimating];
  117. self.emptyLibraryView.frame = self.view.bounds;
  118. [self.view addSubview:self.emptyLibraryView];
  119. [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
  120. [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
  121. return;
  122. }
  123. if (_foundMedia.count < 1)
  124. [self updateViewContents];
  125. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  126. }
  127. - (void)viewDidDisappear:(BOOL)animated
  128. {
  129. [super viewDidDisappear:animated];
  130. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  131. }
  132. - (BOOL)canBecomeFirstResponder
  133. {
  134. return YES;
  135. }
  136. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  137. {
  138. if (motion == UIEventSubtypeMotionShake)
  139. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  140. }
  141. - (void)openMediaObject:(NSManagedObject *)mediaObject
  142. {
  143. if ([mediaObject isKindOfClass:[MLAlbum class]]) {
  144. _foundMedia = [NSMutableArray arrayWithArray:[[(MLAlbum *)mediaObject tracks] allObjects]];
  145. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  146. if (_libraryMode == VLCLibraryModeAllFiles)
  147. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  148. else
  149. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  150. self.title = [(MLAlbum*)mediaObject name];
  151. [self reloadViews];
  152. } else if ([mediaObject isKindOfClass:[MLShow class]]) {
  153. _foundMedia = [NSMutableArray arrayWithArray:[[(MLShow *)mediaObject episodes] allObjects]];
  154. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  155. if (_libraryMode == VLCLibraryModeAllFiles)
  156. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  157. else
  158. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  159. self.title = [(MLShow*)mediaObject name];
  160. [self reloadViews];
  161. } else
  162. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaFromManagedObject:mediaObject];
  163. }
  164. - (void)removeMediaObject:(MLFile *)mediaObject
  165. {
  166. NSFileManager *fileManager = [NSFileManager defaultManager];
  167. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  168. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  169. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  170. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  171. return ([obj rangeOfString:fileName].location != NSNotFound);
  172. }];
  173. unsigned int count = indexSet.count;
  174. NSString *additionalFilePath;
  175. NSUInteger currentIndex = [indexSet firstIndex];
  176. for (unsigned int x = 0; x < count; x++) {
  177. additionalFilePath = allfiles[currentIndex];
  178. if ([additionalFilePath isSupportedSubtitleFormat])
  179. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  180. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  181. }
  182. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  183. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  184. [self updateViewContents];
  185. }
  186. - (void)_displayEmptyLibraryViewIfNeeded
  187. {
  188. if (self.emptyLibraryView.superview)
  189. [self.emptyLibraryView removeFromSuperview];
  190. if (_foundMedia.count == 0) {
  191. self.emptyLibraryView.frame = self.view.bounds;
  192. [self.view addSubview:self.emptyLibraryView];
  193. }
  194. if (_libraryMode == VLCLibraryModeAllFiles && _foundMedia.count > 0)
  195. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  196. else
  197. self.navigationItem.rightBarButtonItem = nil;
  198. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  199. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  200. UITableViewCellSeparatorStyleNone;
  201. } else
  202. [self.collectionView.collectionViewLayout invalidateLayout];
  203. }
  204. - (void)libraryUpgradeComplete
  205. {
  206. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  207. self.navigationItem.leftBarButtonItem = _menuButton;
  208. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  209. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  210. [self.emptyLibraryView.activityIndicator stopAnimating];
  211. [self.emptyLibraryView removeFromSuperview];
  212. [self updateViewContents];
  213. }
  214. - (void)libraryWasUpdated
  215. {
  216. [self updateViewContents];
  217. }
  218. - (void)updateViewContents
  219. {
  220. _foundMedia = [[NSMutableArray alloc] init];
  221. /* add all albums */
  222. if (_libraryMode != VLCLibraryModeAllSeries) {
  223. NSArray *rawAlbums = [MLAlbum allAlbums];
  224. for (MLAlbum *album in rawAlbums) {
  225. if (album.name.length > 0 && album.tracks.count > 1)
  226. [_foundMedia addObject:album];
  227. }
  228. }
  229. if (_libraryMode == VLCLibraryModeAllAlbums) {
  230. [self reloadViews];
  231. return;
  232. }
  233. /* add all shows */
  234. NSArray *rawShows = [MLShow allShows];
  235. for (MLShow *show in rawShows) {
  236. if (show.name.length > 0 && show.episodes.count > 1)
  237. [_foundMedia addObject:show];
  238. }
  239. if (_libraryMode == VLCLibraryModeAllSeries) {
  240. [self reloadViews];
  241. return;
  242. }
  243. /* add all remaining files */
  244. NSArray *allFiles = [MLFile allFiles];
  245. for (MLFile *file in allFiles) {
  246. if (!file.isShowEpisode && !file.isAlbumTrack)
  247. [_foundMedia addObject:file];
  248. else if (file.isShowEpisode) {
  249. if (file.showEpisode.show.episodes.count < 2)
  250. [_foundMedia addObject:file];
  251. } else if (file.isAlbumTrack) {
  252. if (file.albumTrack.album.tracks.count < 2)
  253. [_foundMedia addObject:file];
  254. }
  255. }
  256. [self reloadViews];
  257. }
  258. - (void)reloadViews
  259. {
  260. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  261. [self.tableView reloadData];
  262. else
  263. [self.collectionView reloadData];
  264. [self _displayEmptyLibraryViewIfNeeded];
  265. }
  266. #pragma mark - Table View
  267. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  268. {
  269. return 1;
  270. }
  271. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  272. {
  273. return _foundMedia.count;
  274. }
  275. // Customize the appearance of table view cells.
  276. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  277. {
  278. static NSString *CellIdentifier = @"PlaylistCell";
  279. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  280. if (cell == nil)
  281. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  282. NSInteger row = indexPath.row;
  283. cell.mediaObject = _foundMedia[row];
  284. return cell;
  285. }
  286. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  287. {
  288. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  289. }
  290. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  291. {
  292. return YES;
  293. }
  294. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  295. {
  296. if (editingStyle == UITableViewCellEditingStyleDelete) {
  297. //Delete all tracks from an album
  298. if ([_foundMedia[indexPath.row] isKindOfClass:[MLAlbum class]]) {
  299. MLAlbum *album = _foundMedia[indexPath.row];
  300. NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  301. for (MLAlbumTrack *track in iterAlbumTrack) {
  302. NSSet *iterFiles = [NSSet setWithSet:track.files];
  303. for (MLFile *file in iterFiles) {
  304. [self removeMediaObject:file];
  305. }
  306. }
  307. //Delete all episodes from a show
  308. } else if ([_foundMedia[indexPath.row] isKindOfClass:[MLShow class]]) {
  309. MLShow *show = _foundMedia[indexPath.row];
  310. NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  311. for (MLShowEpisode *episode in iterShowEpisodes) {
  312. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  313. for (MLFile *file in iterFiles) {
  314. [self removeMediaObject:file];
  315. }
  316. }
  317. //everything else
  318. } else {
  319. [self removeMediaObject: _foundMedia[indexPath.row]];
  320. }
  321. }
  322. }
  323. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  324. {
  325. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  326. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  327. [self openMediaObject:selectedObject];
  328. }
  329. #pragma mark - table view gestures
  330. - (void)tableViewLongTouchGestureAction:(UIGestureRecognizer *)recognizer
  331. {
  332. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  333. UITableViewCell *cell = [(UITableView *)self.view cellForRowAtIndexPath:path];
  334. CGRect frame = cell.frame;
  335. if (frame.size.height > 90.)
  336. frame.size.height = 90.;
  337. else if (recognizer.state == UIGestureRecognizerStateBegan)
  338. frame.size.height = 180;
  339. void (^animationBlock)() = ^() {
  340. cell.frame = frame;
  341. };
  342. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  343. cell.frame = frame;
  344. [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
  345. };
  346. NSTimeInterval animationDuration = .2;
  347. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  348. }
  349. #pragma mark - Collection View
  350. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  351. {
  352. return _foundMedia.count;
  353. }
  354. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  355. {
  356. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  357. cell.mediaObject = _foundMedia[indexPath.row];
  358. cell.collectionView = _collectionView;
  359. [cell setEditing:self.editing animated:NO];
  360. return cell;
  361. }
  362. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  363. {
  364. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  365. if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
  366. return CGSizeMake(341., 190.);
  367. else
  368. return CGSizeMake(384., 216.);
  369. }
  370. return CGSizeMake(298.0, 220.0);
  371. }
  372. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  373. {
  374. if (SYSTEM_RUNS_IOS7_OR_LATER)
  375. return UIEdgeInsetsMake(0., 0., 0., 0.);
  376. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  377. }
  378. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  379. {
  380. if (SYSTEM_RUNS_IOS7_OR_LATER)
  381. return 0.;
  382. return 10.0;
  383. }
  384. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  385. {
  386. if (SYSTEM_RUNS_IOS7_OR_LATER)
  387. return 0.;
  388. return 10.0;
  389. }
  390. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  391. {
  392. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  393. [self openMediaObject:selectedObject];
  394. }
  395. #pragma mark - UI implementation
  396. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  397. {
  398. if (_libraryMode != VLCLibraryModeAllFiles)
  399. return;
  400. [super setEditing:editing animated:animated];
  401. UIBarButtonItem *editButton = self.editButtonItem;
  402. NSString *editImage = editing? @"doneButton": @"button";
  403. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  404. if (SYSTEM_RUNS_IOS7_OR_LATER)
  405. editButton.tintColor = [UIColor whiteColor];
  406. else {
  407. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  408. barMetrics:UIBarMetricsDefault];
  409. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  410. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  411. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  412. }
  413. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  414. NSArray *visibleCells = self.collectionView.visibleCells;
  415. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  416. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  417. [aCell setEditing:editing animated:animated];
  418. }];
  419. } else
  420. [self.tableView setEditing:editing animated:YES];
  421. }
  422. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  423. {
  424. if (_libraryMode != VLCLibraryModeAllFiles)
  425. return UITableViewCellEditingStyleNone;
  426. return UITableViewCellEditingStyleDelete;
  427. }
  428. - (IBAction)leftButtonAction:(id)sender
  429. {
  430. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  431. if (self.isEditing)
  432. [self setEditing:NO animated:YES];
  433. }
  434. - (IBAction)backToAllItems:(id)sender
  435. {
  436. self.navigationItem.leftBarButtonItem = _menuButton;
  437. [self setLibraryMode:_libraryMode];
  438. }
  439. #pragma mark - coin coin
  440. - (void)setLibraryMode:(VLCLibraryMode)mode
  441. {
  442. _libraryMode = mode;
  443. if (_libraryMode == VLCLibraryModeAllAlbums)
  444. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  445. else if( _libraryMode == VLCLibraryModeAllSeries)
  446. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  447. else
  448. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  449. [self updateViewContents];
  450. }
  451. #pragma mark - autorotation
  452. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  453. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  454. /* introduced in iOS 6 */
  455. - (NSUInteger)supportedInterfaceOrientations {
  456. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  457. return UIInterfaceOrientationMaskAll;
  458. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  459. UIInterfaceOrientationMaskPortrait;
  460. }
  461. /* introduced in iOS 6 */
  462. - (BOOL)shouldAutorotate {
  463. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  464. }
  465. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  466. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  467. [self.collectionView.collectionViewLayout invalidateLayout];
  468. }
  469. @end