VLCPlaylistViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. self.navigationItem.leftBarButtonItem = _menuButton;
  87. if (SYSTEM_RUNS_IOS7_OR_LATER)
  88. self.editButtonItem.tintColor = [UIColor whiteColor];
  89. else {
  90. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
  91. forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  92. [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
  93. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  94. }
  95. _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  96. _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
  97. [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
  98. [self setToolbarItems:@[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_RENAME", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(renameSelection)], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteSelection)]]];
  99. self.navigationController.toolbar.barStyle = UIBarStyleBlack;
  100. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  101. self.navigationController.toolbar.tintColor = [UIColor whiteColor];
  102. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  103. } else
  104. [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
  105. }
  106. - (void)viewWillAppear:(BOOL)animated
  107. {
  108. [super viewWillAppear:animated];
  109. [self.collectionView.collectionViewLayout invalidateLayout];
  110. [self _displayEmptyLibraryViewIfNeeded];
  111. }
  112. - (void)viewDidAppear:(BOOL)animated
  113. {
  114. [super viewDidAppear:animated];
  115. if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
  116. self.navigationItem.rightBarButtonItem = nil;
  117. self.navigationItem.leftBarButtonItem = nil;
  118. self.title = @"";
  119. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
  120. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
  121. [self.emptyLibraryView.activityIndicator startAnimating];
  122. self.emptyLibraryView.frame = self.view.bounds;
  123. [self.view addSubview:self.emptyLibraryView];
  124. [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
  125. [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
  126. return;
  127. }
  128. if (_foundMedia.count < 1)
  129. [self updateViewContents];
  130. [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
  131. }
  132. - (void)viewDidDisappear:(BOOL)animated
  133. {
  134. [super viewDidDisappear:animated];
  135. [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
  136. }
  137. - (BOOL)canBecomeFirstResponder
  138. {
  139. return YES;
  140. }
  141. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  142. {
  143. if (motion == UIEventSubtypeMotionShake)
  144. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  145. }
  146. - (void)openMediaObject:(NSManagedObject *)mediaObject
  147. {
  148. if ([mediaObject isKindOfClass:[MLAlbum class]]) {
  149. _foundMedia = [NSMutableArray arrayWithArray:[(MLAlbum *)mediaObject sortedTracks]];
  150. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  151. if (_libraryMode == VLCLibraryModeAllFiles)
  152. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  153. else
  154. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
  155. self.title = [(MLAlbum*)mediaObject name];
  156. [self reloadViews];
  157. } else if ([mediaObject isKindOfClass:[MLShow class]]) {
  158. _foundMedia = [NSMutableArray arrayWithArray:[(MLShow *)mediaObject sortedEpisodes]];
  159. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
  160. if (_libraryMode == VLCLibraryModeAllFiles)
  161. self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
  162. else
  163. [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
  164. self.title = [(MLShow*)mediaObject name];
  165. [self reloadViews];
  166. } else
  167. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaFromManagedObject:mediaObject];
  168. }
  169. - (void)removeMediaObject:(id)managedObject updateDatabase:(BOOL)updateDB
  170. {
  171. // delete all tracks from an album
  172. if ([managedObject isKindOfClass:[MLAlbum class]]) {
  173. MLAlbum *album = managedObject;
  174. NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
  175. for (MLAlbumTrack *track in iterAlbumTrack) {
  176. NSSet *iterFiles = [NSSet setWithSet:track.files];
  177. for (MLFile *file in iterFiles)
  178. [self _deleteMediaObject:file];
  179. }
  180. // delete all episodes from a show
  181. } else if ([managedObject isKindOfClass:[MLShow class]]) {
  182. MLShow *show = managedObject;
  183. NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
  184. for (MLShowEpisode *episode in iterShowEpisodes) {
  185. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  186. for (MLFile *file in iterFiles)
  187. [self _deleteMediaObject:file];
  188. }
  189. // delete all files from an episode
  190. } else if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
  191. MLShowEpisode *episode = managedObject;
  192. NSSet *iterFiles = [NSSet setWithSet:episode.files];
  193. for (MLFile *file in iterFiles)
  194. [self _deleteMediaObject:file];
  195. // delete all files from a track
  196. } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
  197. MLAlbumTrack *track = managedObject;
  198. NSSet *iterFiles = [NSSet setWithSet:track.files];
  199. for (MLFile *file in iterFiles)
  200. [self _deleteMediaObject:file];
  201. } else
  202. [self _deleteMediaObject:managedObject];
  203. if (updateDB) {
  204. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  205. [self updateViewContents];
  206. }
  207. }
  208. - (void)_deleteMediaObject:(MLFile *)mediaObject
  209. {
  210. NSFileManager *fileManager = [NSFileManager defaultManager];
  211. NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
  212. NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
  213. NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
  214. NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  215. return ([obj rangeOfString:fileName].location != NSNotFound);
  216. }];
  217. unsigned int count = indexSet.count;
  218. NSString *additionalFilePath;
  219. NSUInteger currentIndex = [indexSet firstIndex];
  220. for (unsigned int x = 0; x < count; x++) {
  221. additionalFilePath = allfiles[currentIndex];
  222. if ([additionalFilePath isSupportedSubtitleFormat])
  223. [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
  224. currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
  225. }
  226. [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
  227. }
  228. - (void)_displayEmptyLibraryViewIfNeeded
  229. {
  230. if (self.emptyLibraryView.superview)
  231. [self.emptyLibraryView removeFromSuperview];
  232. if (_foundMedia.count == 0) {
  233. self.emptyLibraryView.frame = self.view.bounds;
  234. [self.view addSubview:self.emptyLibraryView];
  235. self.navigationItem.rightBarButtonItem = nil;
  236. } else
  237. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  238. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  239. _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
  240. UITableViewCellSeparatorStyleNone;
  241. } else
  242. [self.collectionView.collectionViewLayout invalidateLayout];
  243. }
  244. - (void)libraryUpgradeComplete
  245. {
  246. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  247. self.navigationItem.leftBarButtonItem = _menuButton;
  248. self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
  249. self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
  250. [self.emptyLibraryView.activityIndicator stopAnimating];
  251. [self.emptyLibraryView removeFromSuperview];
  252. [self updateViewContents];
  253. }
  254. - (void)libraryWasUpdated
  255. {
  256. [self updateViewContents];
  257. }
  258. - (void)updateViewContents
  259. {
  260. _foundMedia = [[NSMutableArray alloc] init];
  261. self.navigationItem.leftBarButtonItem = _menuButton;
  262. if (_libraryMode == VLCLibraryModeAllAlbums)
  263. self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
  264. else if( _libraryMode == VLCLibraryModeAllSeries)
  265. self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
  266. else
  267. self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
  268. /* add all albums */
  269. if (_libraryMode != VLCLibraryModeAllSeries) {
  270. NSArray *rawAlbums = [MLAlbum allAlbums];
  271. for (MLAlbum *album in rawAlbums) {
  272. if (album.name.length > 0 && album.tracks.count > 1)
  273. [_foundMedia addObject:album];
  274. }
  275. }
  276. if (_libraryMode == VLCLibraryModeAllAlbums) {
  277. [self reloadViews];
  278. return;
  279. }
  280. /* add all shows */
  281. NSArray *rawShows = [MLShow allShows];
  282. for (MLShow *show in rawShows) {
  283. if (show.name.length > 0 && show.episodes.count > 1)
  284. [_foundMedia addObject:show];
  285. }
  286. if (_libraryMode == VLCLibraryModeAllSeries) {
  287. [self reloadViews];
  288. return;
  289. }
  290. /* add all remaining files */
  291. NSArray *allFiles = [MLFile allFiles];
  292. for (MLFile *file in allFiles) {
  293. if (!file.isShowEpisode && !file.isAlbumTrack)
  294. [_foundMedia addObject:file];
  295. else if (file.isShowEpisode) {
  296. if (file.showEpisode.show.episodes.count < 2)
  297. [_foundMedia addObject:file];
  298. } else if (file.isAlbumTrack) {
  299. if (file.albumTrack.album.tracks.count < 2)
  300. [_foundMedia addObject:file];
  301. }
  302. }
  303. [self reloadViews];
  304. }
  305. - (void)reloadViews
  306. {
  307. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  308. [self.tableView reloadData];
  309. else
  310. [self.collectionView reloadData];
  311. [self _displayEmptyLibraryViewIfNeeded];
  312. }
  313. #pragma mark - Table View
  314. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  315. {
  316. return 1;
  317. }
  318. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  319. {
  320. return _foundMedia.count;
  321. }
  322. // Customize the appearance of table view cells.
  323. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  324. {
  325. static NSString *CellIdentifier = @"PlaylistCell";
  326. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  327. if (cell == nil)
  328. cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
  329. NSInteger row = indexPath.row;
  330. cell.mediaObject = _foundMedia[row];
  331. return cell;
  332. }
  333. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  334. {
  335. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  336. cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
  337. }
  338. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  339. {
  340. return YES;
  341. }
  342. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  343. {
  344. if (editingStyle == UITableViewCellEditingStyleDelete)
  345. [self removeMediaObject: _foundMedia[indexPath.row] updateDatabase:YES];
  346. }
  347. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  348. {
  349. if (tableView.isEditing)
  350. return;
  351. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  352. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  353. [self openMediaObject:selectedObject];
  354. }
  355. #pragma mark - table view gestures
  356. - (void)tableViewLongTouchGestureAction:(UIGestureRecognizer *)recognizer
  357. {
  358. NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
  359. UITableViewCell *cell = [(UITableView *)self.view cellForRowAtIndexPath:path];
  360. CGRect frame = cell.frame;
  361. if (frame.size.height > 90.)
  362. frame.size.height = 90.;
  363. else if (recognizer.state == UIGestureRecognizerStateBegan)
  364. frame.size.height = 180;
  365. void (^animationBlock)() = ^() {
  366. cell.frame = frame;
  367. };
  368. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  369. cell.frame = frame;
  370. [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
  371. };
  372. NSTimeInterval animationDuration = .2;
  373. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  374. }
  375. #pragma mark - Collection View
  376. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  377. {
  378. return _foundMedia.count;
  379. }
  380. - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  381. {
  382. VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
  383. cell.mediaObject = _foundMedia[indexPath.row];
  384. cell.collectionView = _collectionView;
  385. [cell setEditing:self.editing animated:NO];
  386. return cell;
  387. }
  388. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  389. {
  390. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  391. if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
  392. return CGSizeMake(341., 190.);
  393. else
  394. return CGSizeMake(384., 216.);
  395. }
  396. return CGSizeMake(298.0, 220.0);
  397. }
  398. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  399. {
  400. if (SYSTEM_RUNS_IOS7_OR_LATER)
  401. return UIEdgeInsetsMake(0., 0., 0., 0.);
  402. return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
  403. }
  404. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  405. {
  406. if (SYSTEM_RUNS_IOS7_OR_LATER)
  407. return 0.;
  408. return 10.0;
  409. }
  410. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  411. {
  412. if (SYSTEM_RUNS_IOS7_OR_LATER)
  413. return 0.;
  414. return 10.0;
  415. }
  416. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  417. {
  418. if (self.editing) {
  419. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  420. return;
  421. }
  422. NSManagedObject *selectedObject = _foundMedia[indexPath.row];
  423. [self openMediaObject:selectedObject];
  424. }
  425. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  426. {
  427. [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
  428. }
  429. #pragma mark - UI implementation
  430. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  431. {
  432. [super setEditing:editing animated:animated];
  433. UIBarButtonItem *editButton = self.editButtonItem;
  434. NSString *editImage = editing? @"doneButton": @"button";
  435. NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
  436. if (SYSTEM_RUNS_IOS7_OR_LATER)
  437. editButton.tintColor = [UIColor whiteColor];
  438. else {
  439. [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
  440. barMetrics:UIBarMetricsDefault];
  441. [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
  442. forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  443. [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor colorWithWhite:0. alpha:.37], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
  444. }
  445. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  446. NSArray *visibleCells = self.collectionView.visibleCells;
  447. [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  448. VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
  449. [aCell setEditing:editing animated:animated];
  450. }];
  451. self.collectionView.allowsMultipleSelection = editing;
  452. /* UIKit doesn't clear the selection automagically if we leave the editing mode
  453. * so we need to do so manually */
  454. if (!editing) {
  455. NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
  456. NSUInteger count = selectedItems.count;
  457. for (NSUInteger x = 0; x < count; x++)
  458. [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
  459. }
  460. } else {
  461. self.tableView.allowsMultipleSelectionDuringEditing = editing;
  462. [self.tableView setEditing:editing animated:YES];
  463. [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
  464. }
  465. self.navigationController.toolbarHidden = !editing;
  466. }
  467. - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  468. {
  469. return UITableViewCellEditingStyleDelete;
  470. }
  471. - (IBAction)leftButtonAction:(id)sender
  472. {
  473. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  474. if (self.isEditing)
  475. [self setEditing:NO animated:YES];
  476. }
  477. - (IBAction)backToAllItems:(id)sender
  478. {
  479. [self setLibraryMode:_libraryMode];
  480. [self updateViewContents];
  481. }
  482. - (void)_endEditingWithHardReset:(BOOL)hardReset
  483. {
  484. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  485. if (hardReset)
  486. [self updateViewContents];
  487. else
  488. [self reloadViews];
  489. [self setEditing:NO animated:YES];
  490. }
  491. - (void)deleteSelection
  492. {
  493. NSArray *indexPaths;
  494. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  495. indexPaths = [self.collectionView indexPathsForSelectedItems];
  496. else
  497. indexPaths = [self.tableView indexPathsForSelectedRows];
  498. NSUInteger count = indexPaths.count;
  499. NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
  500. for (NSUInteger x = 0; x < count; x++)
  501. [objects addObject:_foundMedia[[indexPaths[x] row]]];
  502. for (NSUInteger x = 0; x < count; x++)
  503. [self removeMediaObject:objects[x] updateDatabase:NO];
  504. [self _endEditingWithHardReset:YES];
  505. }
  506. - (void)renameSelection
  507. {
  508. NSArray *indexPaths;
  509. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  510. indexPaths = [self.collectionView indexPathsForSelectedItems];
  511. else
  512. indexPaths = [self.tableView indexPathsForSelectedRows];
  513. if (indexPaths.count < 1) {
  514. [self _endEditingWithHardReset:NO];
  515. return;
  516. }
  517. NSString *itemName;
  518. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  519. itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
  520. else
  521. itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
  522. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_RENAME", @""), nil];
  523. [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
  524. [[alert textFieldAtIndex:0] setText:itemName];
  525. [alert show];
  526. }
  527. - (void)renameMediaObjectTo:(NSString*)newName
  528. {
  529. NSArray *indexPaths;
  530. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  531. indexPaths = [self.collectionView indexPathsForSelectedItems];
  532. else
  533. indexPaths = [self.tableView indexPathsForSelectedRows];
  534. id mediaObject = _foundMedia[[indexPaths[0] row]];
  535. if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]])
  536. [mediaObject setName:newName];
  537. else
  538. [mediaObject setTitle:newName];
  539. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  540. [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
  541. else
  542. [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
  543. if (indexPaths.count > 1)
  544. [self renameSelection];
  545. else
  546. [self _endEditingWithHardReset:NO];
  547. }
  548. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  549. {
  550. if (buttonIndex == 1)
  551. [self renameMediaObjectTo:[alertView textFieldAtIndex:0].text];
  552. else
  553. [self _endEditingWithHardReset:NO];
  554. }
  555. #pragma mark - coin coin
  556. - (void)setLibraryMode:(VLCLibraryMode)mode
  557. {
  558. _libraryMode = mode;
  559. [self updateViewContents];
  560. }
  561. #pragma mark - autorotation
  562. // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
  563. // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
  564. /* introduced in iOS 6 */
  565. - (NSUInteger)supportedInterfaceOrientations {
  566. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  567. return UIInterfaceOrientationMaskAll;
  568. return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
  569. UIInterfaceOrientationMaskPortrait;
  570. }
  571. /* introduced in iOS 6 */
  572. - (BOOL)shouldAutorotate {
  573. return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
  574. }
  575. - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  576. [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
  577. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  578. [self.collectionView.collectionViewLayout invalidateLayout];
  579. }
  580. @end