VLCPlaylistViewController.m 22 KB

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