VLCPlaylistViewController.m 23 KB

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