VLCPlaylistViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. } else
  204. [self.collectionView.collectionViewLayout invalidateLayout];
  205. }
  206. - (void)libraryUpgradeComplete
  207. {
  208. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  209. self.navigationItem.leftBarButtonItem = _menuButton;
  210. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  211. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  212. [self.emptyLibraryView.activityIndicator stopAnimating];
  213. [self.emptyLibraryView removeFromSuperview];
  214. [self updateViewContents];
  215. }
  216. - (void)libraryWasUpdated
  217. {
  218. [self updateViewContents];
  219. }
  220. - (void)updateViewContents
  221. {
  222. _foundMedia = [[NSMutableArray alloc] init];
  223. /* add all albums */
  224. if (_libraryMode != VLCLibraryModeAllSeries) {
  225. NSArray *rawAlbums = [MLAlbum allAlbums];
  226. for (MLAlbum *album in rawAlbums) {
  227. if (album.name.length > 0 && album.tracks.count > 1)
  228. [_foundMedia addObject:album];
  229. }
  230. }
  231. if (_libraryMode == VLCLibraryModeAllAlbums) {
  232. [self reloadViews];
  233. return;
  234. }
  235. /* add all shows */
  236. NSArray *rawShows = [MLShow allShows];
  237. for (MLShow *show in rawShows) {
  238. if (show.name.length > 0 && show.episodes.count > 1)
  239. [_foundMedia addObject:show];
  240. }
  241. if (_libraryMode == VLCLibraryModeAllSeries) {
  242. [self reloadViews];
  243. return;
  244. }
  245. /* add all remaining files */
  246. NSArray *allFiles = [MLFile allFiles];
  247. for (MLFile *file in allFiles) {
  248. if (!file.isShowEpisode && !file.isAlbumTrack)
  249. [_foundMedia addObject:file];
  250. else if (file.isShowEpisode) {
  251. if (file.showEpisode.show.episodes.count < 2)
  252. [_foundMedia addObject:file];
  253. } else if (file.isAlbumTrack) {
  254. if (file.albumTrack.album.tracks.count < 2)
  255. [_foundMedia addObject:file];
  256. }
  257. }
  258. [self reloadViews];
  259. }
  260. - (void)reloadViews
  261. {
  262. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  263. [self.tableView reloadData];
  264. else
  265. [self.collectionView reloadData];
  266. [self _displayEmptyLibraryViewIfNeeded];
  267. }
  268. #pragma mark - Table View
  269. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  270. {
  271. return 1;
  272. }
  273. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  274. {
  275. return _foundMedia.count;
  276. }
  277. // Customize the appearance of table view cells.
  278. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. static NSString *CellIdentifier = @"PlaylistCell";
  281. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  282. if (cell == nil)
  283. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  284. NSInteger row = indexPath.row;
  285. cell.mediaObject = _foundMedia[row];
  286. return cell;
  287. }
  288. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  289. {
  290. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  291. }
  292. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. return YES;
  295. }
  296. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298. if (editingStyle == UITableViewCellEditingStyleDelete) {
  299. //Delete all tracks from an album
  300. if ([_foundMedia[indexPath.row] isKindOfClass:[MLAlbum class]]) {
  301. MLAlbum *album = _foundMedia[indexPath.row];
  302. NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  303. for (MLAlbumTrack *track in iterAlbumTrack) {
  304. NSSet *iterFiles = [NSSet setWithSet:track.files];
  305. for (MLFile *file in iterFiles) {
  306. [self removeMediaObject:file];
  307. }
  308. }
  309. //Delete all episodes from a show
  310. } else if ([_foundMedia[indexPath.row] isKindOfClass:[MLShow class]]) {
  311. MLShow *show = _foundMedia[indexPath.row];
  312. NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  313. for (MLShowEpisode *episode in iterShowEpisodes) {
  314. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  315. for (MLFile *file in iterFiles) {
  316. [self removeMediaObject:file];
  317. }
  318. }
  319. //everything else
  320. } else {
  321. [self removeMediaObject: _foundMedia[indexPath.row]];
  322. }
  323. }
  324. }
  325. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  326. {
  327. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  328. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  329. [self openMediaObject:selectedObject];
  330. }
  331. #pragma mark - table view gestures
  332. - (void)tableViewLongTouchGestureAction:(UIGestureRecognizer *)recognizer
  333. {
  334. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  335. UITableViewCell *cell = [(UITableView *)self.view cellForRowAtIndexPath:path];
  336. CGRect frame = cell.frame;
  337. if (frame.size.height > 90.)
  338. frame.size.height = 90.;
  339. else if (recognizer.state == UIGestureRecognizerStateBegan)
  340. frame.size.height = 180;
  341. void (^animationBlock)() = ^() {
  342. cell.frame = frame;
  343. };
  344. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  345. cell.frame = frame;
  346. [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
  347. };
  348. NSTimeInterval animationDuration = .2;
  349. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  350. }
  351. #pragma mark - Collection View
  352. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  353. {
  354. return _foundMedia.count;
  355. }
  356. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  357. {
  358. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  359. cell.mediaObject = _foundMedia[indexPath.row];
  360. cell.collectionView = _collectionView;
  361. [cell setEditing:self.editing animated:NO];
  362. return cell;
  363. }
  364. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  365. {
  366. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  367. if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
  368. return CGSizeMake(341., 190.);
  369. else
  370. return CGSizeMake(384., 216.);
  371. }
  372. return CGSizeMake(298.0, 220.0);
  373. }
  374. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  375. {
  376. if (SYSTEM_RUNS_IOS7_OR_LATER)
  377. return UIEdgeInsetsMake(0., 0., 0., 0.);
  378. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  379. }
  380. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  381. {
  382. if (SYSTEM_RUNS_IOS7_OR_LATER)
  383. return 0.;
  384. return 10.0;
  385. }
  386. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  387. {
  388. if (SYSTEM_RUNS_IOS7_OR_LATER)
  389. return 0.;
  390. return 10.0;
  391. }
  392. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  393. {
  394. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  395. [self openMediaObject:selectedObject];
  396. }
  397. #pragma mark - UI implementation
  398. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  399. {
  400. if (_libraryMode != VLCLibraryModeAllFiles)
  401. return;
  402. [super setEditing:editing animated:animated];
  403. UIBarButtonItem *editButton = self.editButtonItem;
  404. NSString *editImage = editing? @"doneButton": @"button";
  405. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  406. if (SYSTEM_RUNS_IOS7_OR_LATER)
  407. editButton.tintColor = [UIColor whiteColor];
  408. else {
  409. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  410. barMetrics:UIBarMetricsDefault];
  411. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  412. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  413. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  414. }
  415. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  416. NSArray *visibleCells = self.collectionView.visibleCells;
  417. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  418. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  419. [aCell setEditing:editing animated:animated];
  420. }];
  421. } else
  422. [self.tableView setEditing:editing animated:YES];
  423. }
  424. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  425. {
  426. if (_libraryMode != VLCLibraryModeAllFiles)
  427. return UITableViewCellEditingStyleNone;
  428. return UITableViewCellEditingStyleDelete;
  429. }
  430. - (IBAction)leftButtonAction:(id)sender
  431. {
  432. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  433. if (self.isEditing)
  434. [self setEditing:NO animated:YES];
  435. }
  436. - (IBAction)backToAllItems:(id)sender
  437. {
  438. self.navigationItem.leftBarButtonItem = _menuButton;
  439. [self setLibraryMode:_libraryMode];
  440. }
  441. #pragma mark - coin coin
  442. - (void)setLibraryMode:(VLCLibraryMode)mode
  443. {
  444. _libraryMode = mode;
  445. if (_libraryMode == VLCLibraryModeAllAlbums)
  446. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  447. else if( _libraryMode == VLCLibraryModeAllSeries)
  448. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  449. else
  450. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  451. [self updateViewContents];
  452. }
  453. #pragma mark - autorotation
  454. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  455. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  456. /* introduced in iOS 6 */
  457. - (NSUInteger)supportedInterfaceOrientations {
  458. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  459. return UIInterfaceOrientationMaskAll;
  460. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  461. UIInterfaceOrientationMaskPortrait;
  462. }
  463. /* introduced in iOS 6 */
  464. - (BOOL)shouldAutorotate {
  465. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  466. }
  467. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  468. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  469. [self.collectionView.collectionViewLayout invalidateLayout];
  470. }
  471. @end