1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282 |
- /*****************************************************************************
- * VLCPlaylistViewController.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2013-2014 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- * Gleb Pinigin <gpinigin # gmail.com>
- * Tamas Timar <ttimar.vlc # gmail.com>
- * Carola Nitz <nitz.carola # gmail.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCPlaylistViewController.h"
- #import "VLCMovieViewController.h"
- #import "VLCPlaylistTableViewCell.h"
- #import "VLCPlaylistCollectionViewCell.h"
- #import "UINavigationController+Theme.h"
- #import "NSString+SupportedMedia.h"
- #import "VLCBugreporter.h"
- #import "VLCAppDelegate.h"
- #import "UIBarButtonItem+Theme.h"
- #import "VLCFirstStepsViewController.h"
- #import "VLCFolderCollectionViewFlowLayout.h"
- #import "LXReorderableCollectionViewFlowLayout.h"
- #import "VLCAlertView.h"
- /* prefs keys */
- static NSString *kDisplayedFirstSteps = @"Did we display the first steps tutorial?";
- @implementation EmptyLibraryView
- - (IBAction)learnMore:(id)sender
- {
- UIViewController *firstStepsVC = [[VLCFirstStepsViewController alloc] initWithNibName:nil bundle:nil];
- UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:firstStepsVC];
- navCon.modalPresentationStyle = UIModalPresentationFormSheet;
- [navCon loadTheme];
- [self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
- }
- @end
- @interface VLCPlaylistViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary, VLCMediaListDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
- NSMutableArray *_foundMedia;
- VLCLibraryMode _libraryMode;
- VLCLibraryMode _previousLibraryMode;
- UIBarButtonItem *_menuButton;
- NSMutableArray *_indexPaths;
- id _folderObject;
- VLCFolderCollectionViewFlowLayout *_folderLayout;
- LXReorderableCollectionViewFlowLayout *_reorderLayout;
- BOOL inFolder;
- UILongPressGestureRecognizer *_longPressGestureRecognizer;
- NSInteger _mediaToPlayIndex;
- VLCMediaList *_list;
- NSArray *_tracks;
- NSMutableArray *_searchData;
- UISearchBar *_searchBar;
- UISearchDisplayController *_searchDisplayController;
- BOOL _usingTableViewToShowData;
- }
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) UICollectionView *collectionView;
- @property (nonatomic, strong) EmptyLibraryView *emptyLibraryView;
- @end
- @implementation VLCPlaylistViewController
- + (void)initialize
- {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- [defaults registerDefaults:@{kDisplayedFirstSteps : [NSNumber numberWithBool:NO]}];
- }
- - (void)loadView
- {
- _usingTableViewToShowData = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
- if (_usingTableViewToShowData) {
- _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
- _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
- _tableView.rowHeight = [VLCPlaylistTableViewCell heightOfCell];
- _tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.opaque = YES;
- self.view = _tableView;
- } else {
- _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
- _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_folderLayout];
- _collectionView.alwaysBounceVertical = YES;
- _collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.opaque = YES;
- _collectionView.backgroundColor = [UIColor VLCDarkBackgroundColor];
- self.view = _collectionView;
- _longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_collectionViewHandleLongPressGesture:)];
- [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- [_collectionView registerNib:[UINib nibWithNibName:@"VLCFuturePlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
- else
- [_collectionView registerNib:[UINib nibWithNibName:@"VLCPlaylistCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PlaylistCell"];
- self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
- }
- _libraryMode = VLCLibraryModeAllFiles;
- self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
- _emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
- _emptyLibraryView.emptyLibraryLongDescriptionLabel.numberOfLines = 0;
- }
- #pragma mark -
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
- _menuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(leftButtonAction:)];
- self.navigationItem.leftBarButtonItem = _menuButton;
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- self.editButtonItem.tintColor = [UIColor whiteColor];
- else {
- [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"button"]
- forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- [self.editButtonItem setBackgroundImage:[UIImage imageNamed:@"buttonHighlight"]
- forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
- }
- _emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
- _emptyLibraryView.emptyLibraryLongDescriptionLabel.text = NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
- [_emptyLibraryView.emptyLibraryLongDescriptionLabel sizeToFit];
- [_emptyLibraryView.learnMoreButton setTitle:NSLocalizedString(@"BUTTON_LEARN_MORE", @"") forState:UIControlStateNormal];
- UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
- [self setToolbarItems:@[createFolderItem, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_RENAME", @"") target:self andSelector:@selector(renameSelection)], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteSelection)]]];
- self.navigationController.toolbar.barStyle = UIBarStyleBlack;
- if (SYSTEM_RUNS_IOS7_OR_LATER) {
- self.navigationController.toolbar.tintColor = [UIColor whiteColor];
- [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
- } else
- [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"bottomBlackBar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
- if (_usingTableViewToShowData) {
- _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
- UINavigationBar *navBar = self.navigationController.navigationBar;
- _searchBar.barTintColor = navBar.barTintColor;
- _searchBar.tintColor = navBar.tintColor;
- _searchBar.translucent = navBar.translucent;
- _searchBar.opaque = navBar.opaque;
- _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
- _searchDisplayController.delegate = self;
- _searchDisplayController.searchResultsDataSource = self;
- _searchDisplayController.searchResultsDelegate = self;
- _searchBar.delegate = self;
- UITapGestureRecognizer *tapTwiceGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwiceGestureAction:)];
- [tapTwiceGesture setNumberOfTapsRequired:2];
- [self.navigationController.navigationBar addGestureRecognizer:tapTwiceGesture];
- }
- _searchData = [[NSMutableArray alloc] init];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self.collectionView.collectionViewLayout invalidateLayout];
- [self _displayEmptyLibraryViewIfNeeded];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- if (![[defaults objectForKey:kDisplayedFirstSteps] boolValue]) {
- [self.emptyLibraryView performSelector:@selector(learnMore:) withObject:nil afterDelay:1.];
- [defaults setObject:[NSNumber numberWithBool:YES] forKey:kDisplayedFirstSteps];
- [defaults synchronize];
- }
- if ([[MLMediaLibrary sharedMediaLibrary] libraryNeedsUpgrade]) {
- self.navigationItem.rightBarButtonItem = nil;
- self.navigationItem.leftBarButtonItem = nil;
- self.title = @"";
- self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"UPGRADING_LIBRARY", @"");
- self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = YES;
- [self.emptyLibraryView.activityIndicator startAnimating];
- self.emptyLibraryView.frame = self.view.bounds;
- [self.view addSubview:self.emptyLibraryView];
- [[MLMediaLibrary sharedMediaLibrary] setDelegate: self];
- [[MLMediaLibrary sharedMediaLibrary] performSelectorInBackground:@selector(upgradeLibrary) withObject:nil];
- return;
- }
- if (_foundMedia.count < 1)
- [self updateViewContents];
- [[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- [[MLMediaLibrary sharedMediaLibrary] libraryDidDisappear];
- }
- - (BOOL)canBecomeFirstResponder
- {
- return YES;
- }
- - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
- {
- if (motion == UIEventSubtypeMotionShake)
- [[VLCBugreporter sharedInstance] handleBugreportRequest];
- }
- - (void)openMediaObject:(NSManagedObject *)mediaObject
- {
- if ([mediaObject isKindOfClass:[MLAlbum class]]) {
- _foundMedia = [NSMutableArray arrayWithArray:[(MLAlbum *)mediaObject sortedTracks]];
- self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
- if (_libraryMode == VLCLibraryModeAllFiles)
- self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
- else
- [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_MUSIC", @"")];
- self.title = [(MLAlbum*)mediaObject name];
- [self reloadViews];
- } else if ([mediaObject isKindOfClass:[MLShow class]]) {
- _foundMedia = [NSMutableArray arrayWithArray:[(MLShow *)mediaObject sortedEpisodes]];
- self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
- if (_libraryMode == VLCLibraryModeAllFiles)
- self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
- else
- [self.navigationItem.leftBarButtonItem setTitle:NSLocalizedString(@"LIBRARY_SERIES", @"")];
- self.title = [(MLShow*)mediaObject name];
- [self reloadViews];
- } else if ([mediaObject isKindOfClass:[MLLabel class]]) {
- MLLabel *folder = (MLLabel*) mediaObject;
- inFolder = YES;
- if (!_usingTableViewToShowData) {
- if (![self.collectionView.collectionViewLayout isEqual:_reorderLayout]) {
- for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
- if (recognizer == _folderLayout.panGestureRecognizer || recognizer == _folderLayout.longPressGestureRecognizer || recognizer == _longPressGestureRecognizer)
- [self.collectionView removeGestureRecognizer:recognizer];
- }
- _reorderLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
- [self.collectionView setCollectionViewLayout:_reorderLayout animated:NO];
- }
- }
- _foundMedia = [NSMutableArray arrayWithArray:[folder sortedFolderItems]];
- self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
- self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", @"");
- self.title = [folder name];
- UIBarButtonItem *removeFromFolder = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(removeFromFolder)];
- NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
- toolbarItems[0] = removeFromFolder;
- self.toolbarItems = toolbarItems;
- [self reloadViews];
- return;
- } else
- [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaFromManagedObject:mediaObject];
- }
- - (void)removeMediaObject:(id)managedObject updateDatabase:(BOOL)updateDB
- {
- // delete all tracks from an album
- if ([managedObject isKindOfClass:[MLAlbum class]]) {
- MLAlbum *album = managedObject;
- NSSet *iterAlbumTrack = [NSSet setWithSet:album.tracks];
- for (MLAlbumTrack *track in iterAlbumTrack) {
- NSSet *iterFiles = [NSSet setWithSet:track.files];
- for (MLFile *file in iterFiles)
- [self _deleteMediaObject:file];
- }
- // delete all episodes from a show
- } else if ([managedObject isKindOfClass:[MLShow class]]) {
- MLShow *show = managedObject;
- NSSet *iterShowEpisodes = [NSSet setWithSet:show.episodes];
- for (MLShowEpisode *episode in iterShowEpisodes) {
- NSSet *iterFiles = [NSSet setWithSet:episode.files];
- for (MLFile *file in iterFiles)
- [self _deleteMediaObject:file];
- }
- // delete all files from an episode
- } else if ([managedObject isKindOfClass:[MLShowEpisode class]]) {
- MLShowEpisode *episode = managedObject;
- NSSet *iterFiles = [NSSet setWithSet:episode.files];
- for (MLFile *file in iterFiles)
- [self _deleteMediaObject:file];
- // delete all files from a track
- } else if ([managedObject isKindOfClass:[MLAlbumTrack class]]) {
- MLAlbumTrack *track = managedObject;
- NSSet *iterFiles = [NSSet setWithSet:track.files];
- for (MLFile *file in iterFiles)
- [self _deleteMediaObject:file];
- } else if ([managedObject isKindOfClass:[MLLabel class]]) {
- MLLabel *folder = managedObject;
- NSSet *iterFiles = [NSSet setWithSet:folder.files];
- [folder removeFiles:folder.files];
- for (MLFile *file in iterFiles)
- [self _deleteMediaObject:file];
- [[MLMediaLibrary sharedMediaLibrary] removeObject:folder];
- }
- else
- [self _deleteMediaObject:managedObject];
- if (updateDB) {
- [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
- [self updateViewContents];
- }
- }
- - (void)_deleteMediaObject:(MLFile *)mediaObject
- {
- if (inFolder)
- [self rearrangeFolderTrackNumbersForRemovedItem:mediaObject];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *folderLocation = [[[NSURL URLWithString:mediaObject.url] path] stringByDeletingLastPathComponent];
- NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
- NSString *fileName = [[[[NSURL URLWithString:mediaObject.url] path] lastPathComponent] stringByDeletingPathExtension];
- NSIndexSet *indexSet = [allfiles indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
- return ([obj rangeOfString:fileName].location != NSNotFound);
- }];
- NSUInteger count = indexSet.count;
- NSString *additionalFilePath;
- NSUInteger currentIndex = [indexSet firstIndex];
- for (unsigned int x = 0; x < count; x++) {
- additionalFilePath = allfiles[currentIndex];
- if ([additionalFilePath isSupportedSubtitleFormat])
- [fileManager removeItemAtPath:[folderLocation stringByAppendingPathComponent:additionalFilePath] error:nil];
- currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
- }
- [fileManager removeItemAtPath:[[NSURL URLWithString:mediaObject.url] path] error:nil];
- }
- - (void)_displayEmptyLibraryViewIfNeeded
- {
- if (self.emptyLibraryView.superview)
- [self.emptyLibraryView removeFromSuperview];
- if (_foundMedia.count == 0) {
- self.emptyLibraryView.emptyLibraryLabel.text = inFolder ? NSLocalizedString(@"FOLDER_EMPTY", @"") : NSLocalizedString(@"EMPTY_LIBRARY", @"");
- self.emptyLibraryView.emptyLibraryLongDescriptionLabel.text = inFolder ? NSLocalizedString(@"FOLDER_EMPTY_LONG", @"") : NSLocalizedString(@"EMPTY_LIBRARY_LONG", @"");
- self.emptyLibraryView.learnMoreButton.hidden = inFolder;
- self.emptyLibraryView.frame = self.view.bounds;
- [self.view addSubview:self.emptyLibraryView];
- self.navigationItem.rightBarButtonItem = nil;
- } else
- self.navigationItem.rightBarButtonItem = self.editButtonItem;
- if (_usingTableViewToShowData)
- _tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
- UITableViewCellSeparatorStyleNone;
- else
- [self.collectionView.collectionViewLayout invalidateLayout];
- }
- - (void)libraryUpgradeComplete
- {
- self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
- self.navigationItem.leftBarButtonItem = _menuButton;
- self.emptyLibraryView.emptyLibraryLongDescriptionLabel.hidden = NO;
- self.emptyLibraryView.emptyLibraryLabel.text = NSLocalizedString(@"EMPTY_LIBRARY", @"");
- [self.emptyLibraryView.activityIndicator stopAnimating];
- [self.emptyLibraryView removeFromSuperview];
- [self updateViewContents];
- }
- - (void)libraryWasUpdated
- {
- [self updateViewContents];
- }
- - (void)updateViewContents
- {
- _foundMedia = [[NSMutableArray alloc] init];
- self.navigationItem.leftBarButtonItem = _menuButton;
- if (_libraryMode == VLCLibraryModeAllAlbums)
- self.title = NSLocalizedString(@"LIBRARY_MUSIC", @"");
- else if( _libraryMode == VLCLibraryModeAllSeries)
- self.title = NSLocalizedString(@"LIBRARY_SERIES", @"");
- else
- self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", @"");
- /* add all albums */
- if (_libraryMode != VLCLibraryModeAllSeries) {
- NSArray *rawAlbums = [MLAlbum allAlbums];
- for (MLAlbum *album in rawAlbums) {
- if (album.name.length > 0 && album.tracks.count > 1)
- [_foundMedia addObject:album];
- }
- }
- if (_libraryMode == VLCLibraryModeAllAlbums) {
- [self reloadViews];
- return;
- }
- /* add all shows */
- NSArray *rawShows = [MLShow allShows];
- for (MLShow *show in rawShows) {
- if (show.name.length > 0 && show.episodes.count > 1)
- [_foundMedia addObject:show];
- }
- if (_libraryMode == VLCLibraryModeAllSeries) {
- [self reloadViews];
- return;
- }
- /* add all folders*/
- NSArray *allFolders = [MLLabel allLabels];
- for (MLLabel *folder in allFolders)
- [_foundMedia addObject:folder];
- /* add all remaining files */
- NSArray *allFiles = [MLFile allFiles];
- for (MLFile *file in allFiles) {
- if (file.labels.count > 0) continue;
- if (!file.isShowEpisode && !file.isAlbumTrack)
- [_foundMedia addObject:file];
- else if (file.isShowEpisode) {
- if (file.showEpisode.show.episodes.count < 2)
- [_foundMedia addObject:file];
- /* older MediaLibraryKit versions don't send a show name in a popular
- * corner case. hence, we need to work-around here and force a reload
- * afterwards as this could lead to the 'all my shows are gone'
- * syndrome (see #10435, #10464, #10432 et al) */
- if (file.showEpisode.show.name.length == 0) {
- file.showEpisode.show.name = NSLocalizedString(@"UNTITLED_SHOW", @"");
- [self performSelector:@selector(updateViewContents) withObject:nil afterDelay:0.1];
- }
- } else if (file.isAlbumTrack) {
- if (file.albumTrack.album.tracks.count < 2)
- [_foundMedia addObject:file];
- }
- }
- [self reloadViews];
- }
- - (void)reloadViews
- {
- if (_usingTableViewToShowData)
- [self.tableView reloadData];
- else
- [self.collectionView reloadData];
- [self _displayEmptyLibraryViewIfNeeded];
- }
- #pragma mark - Table View
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (tableView == self.searchDisplayController.searchResultsTableView)
- return _searchData.count;
- return _foundMedia.count;
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"PlaylistCell";
- VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [VLCPlaylistTableViewCell cellWithReuseIdentifier:CellIdentifier];
- UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightGestureAction:)];
- [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
- [cell addGestureRecognizer:swipeRight];
- NSInteger row = indexPath.row;
- if (tableView == self.searchDisplayController.searchResultsTableView)
- cell.mediaObject = _searchData[row];
- else
- cell.mediaObject = _foundMedia[row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
- {
- MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
- [_foundMedia removeObjectAtIndex:fromIndexPath.item];
- [_foundMedia insertObject:object atIndex:toIndexPath.item];
- object.folderTrackNumber = @(toIndexPath.item - 1);
- object = [_foundMedia objectAtIndex:fromIndexPath.item];
- object.folderTrackNumber = @(fromIndexPath.item - 1);
- }
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return inFolder;
- }
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
- cell.multipleSelectionBackgroundView.backgroundColor = cell.backgroundColor;
- }
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle == UITableViewCellEditingStyleDelete)
- [self removeMediaObject: _foundMedia[indexPath.row] updateDatabase:YES];
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (tableView.isEditing) {
- if (_libraryMode == VLCLibraryModeCreateFolder) {
- _folderObject = _foundMedia[indexPath.row];
- _libraryMode = _previousLibraryMode;
- [self updateViewContents];
- [self createFolderWithName:nil];
- }
- return;
- }
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- NSManagedObject *selectedObject;
- if (tableView == self.searchDisplayController.searchResultsTableView)
- selectedObject = _searchData[indexPath.row];
- else
- selectedObject = _foundMedia[indexPath.row];
- if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
- _tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
- NSUInteger count = _tracks.count;
- _list = [[VLCMediaList alloc] init];
- _list.delegate = self;
- MLFile *file;
- VLCMedia *media;
- for (NSInteger x = count - 1; x > -1; x--) {
- file = [(MLAlbumTrack*)_tracks[x] files].anyObject;
- media = [VLCMedia mediaWithURL: [NSURL URLWithString:file.url]];
- [media parse];
- [_list addMedia:media];
- }
- _mediaToPlayIndex = indexPath.row;
- } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
- MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
- _tracks = [folder sortedFolderItems];
- NSUInteger count = _tracks.count;
- _list = [[VLCMediaList alloc] init];
- _list.delegate = self;
- MLFile *file;
- for (NSInteger x = count - 1; x > -1; x--) {
- file = (MLFile *)_tracks[x];
- [_list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
- }
- _mediaToPlayIndex = indexPath.row;
- }
- if (_searchDisplayController.active == YES)
- [_searchDisplayController setActive:NO animated:YES];
- [self openMediaObject:selectedObject];
- }
- #pragma mark - VLCMedialistDelegate
- - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
- {
- if (index == _tracks.count - 1) {
- NSManagedObject *selectedObject = _foundMedia[_mediaToPlayIndex];
- [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:_list atIndex:(int)[_tracks indexOfObject:selectedObject]];
- }
- }
- #pragma mark - Gesture Action
- - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
- {
- if ([[self.editButtonItem title] isEqualToString:NSLocalizedString(@"BUTTON_CANCEL",@"")])
- [self setEditing:NO animated:YES];
- else {
- [self setEditing:YES animated:YES];
- NSIndexPath *path = [(UITableView *)self.view indexPathForRowAtPoint:[recognizer locationInView:self.view]];
- [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
- animated:YES
- scrollPosition:UITableViewScrollPositionNone];
- }
- }
- - (void)tapTwiceGestureAction:(UIGestureRecognizer *)recognizer
- {
- _searchBar.hidden = !_searchBar.hidden;
- if (_searchBar.hidden)
- self.tableView.tableHeaderView = nil;
- else
- self.tableView.tableHeaderView = _searchBar;
- }
- #pragma mark - Collection View
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return _foundMedia.count;
- }
- - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
- cell.mediaObject = _foundMedia[indexPath.row];
- cell.collectionView = _collectionView;
- [cell setEditing:self.editing animated:NO];
- return cell;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- if (SYSTEM_RUNS_IOS7_OR_LATER) {
- if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
- return CGSizeMake(341., 190.);
- else
- return CGSizeMake(384., 216.);
- }
- return CGSizeMake(298.0, 220.0);
- }
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
- {
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- return UIEdgeInsetsZero;
- return UIEdgeInsetsMake(0.0, 34.0, 0.0, 34.0);
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
- {
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- return 0.;
- return 10.0;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
- {
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- return 0.;
- return 10.0;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- if (self.editing) {
- if (_libraryMode == VLCLibraryModeCreateFolder) {
- _folderObject = _foundMedia[indexPath.item];
- [self createFolderWithName:nil];
- _libraryMode = _previousLibraryMode;
- }
- [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
- return;
- }
- NSManagedObject *selectedObject = _foundMedia[indexPath.row];
- if ([selectedObject isKindOfClass:[MLAlbumTrack class]]) {
- VLCMediaList *list;
- NSArray *tracks = [[(MLAlbumTrack*)selectedObject album] sortedTracks];
- NSUInteger count = tracks.count;
- list = [[VLCMediaList alloc] init];
- MLFile *file;
- for (NSInteger x = count - 1; x > -1; x--) {
- file = [(MLAlbumTrack*)tracks[x] files].anyObject;
- [list addMedia:[VLCMedia mediaWithURL: [NSURL URLWithString:file.url]]];
- }
- [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[tracks indexOfObject:selectedObject]];
- } else if ([selectedObject isKindOfClass:[MLFile class]] && [((MLFile *)selectedObject).labels count] > 0) {
- VLCMediaList *list;
- MLLabel *folder = [((MLFile *)selectedObject).labels anyObject];
- NSArray *folderTracks = [folder sortedFolderItems];
- NSUInteger count = folderTracks.count;
- list = [[VLCMediaList alloc] init];
- MLFile *file;
- for (NSInteger x = count - 1; x > -1; x--) {
- file = (MLFile *)folderTracks[x];
- [list addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:file.url]]];
- }
- [(VLCAppDelegate *)[UIApplication sharedApplication].delegate openMediaList:list atIndex:(int)[folderTracks indexOfObject:selectedObject]];
- } else
- [self openMediaObject:selectedObject];
- }
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- [(VLCPlaylistCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] selectionUpdate];
- }
- - (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
- {
- MLFile *mediaObject = (MLFile *)_foundMedia[indexPath.item];
- [self rearrangeFolderTrackNumbersForRemovedItem:mediaObject];
- mediaObject.labels = nil;
- mediaObject.folderTrackNumber = nil;
- [self backToAllItems:nil];
- }
- - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
- {
- MLFile* object = [_foundMedia objectAtIndex:fromIndexPath.item];
- [_foundMedia removeObjectAtIndex:fromIndexPath.item];
- [_foundMedia insertObject:object atIndex:toIndexPath.item];
- object.folderTrackNumber = @(toIndexPath.item - 1);
- object = [_foundMedia objectAtIndex:fromIndexPath.item];
- object.folderTrackNumber = @(fromIndexPath.item - 1);
- }
- - (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
- {
- BOOL validFileTypeAtFolderPath = ([_foundMedia[folderPath.item] isKindOfClass:[MLFile class]] || [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]]) && [_foundMedia[itemPath.item] isKindOfClass:[MLFile class]];
- if (!validFileTypeAtFolderPath) {
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_INVALID_TYPE_TITLE", @"") message:NSLocalizedString(@"FOLDER_INVALID_TYPE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
- alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
- [self updateViewContents];
- };
- [alert show];
- return;
- }
- BOOL isFolder = [_foundMedia[folderPath.item] isKindOfClass:[MLLabel class]];
- if (isFolder){
- MLLabel *folder = _foundMedia[folderPath.item];
- MLFile *file = _foundMedia[itemPath.item];
- [file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
- file.folderTrackNumber = @([folder.files count] - 1);
- [_foundMedia removeObjectAtIndex:itemPath.item];
- [self updateViewContents];
- } else {
- _folderObject = _foundMedia[folderPath.item];
- _indexPaths = [NSMutableArray arrayWithArray:@[itemPath]];
- [self showCreateFolderAlert];
- }
- }
- #pragma mark - Folder implementation
- - (void)rearrangeFolderTrackNumbersForRemovedItem:(MLFile *) mediaObject
- {
- MLLabel *label = [mediaObject.labels anyObject];
- NSSet *allFiles = label.files;
- for (MLFile *file in allFiles) {
- if (file.folderTrackNumber > mediaObject.folderTrackNumber) {
- int value = [file.folderTrackNumber intValue];
- file.folderTrackNumber = [NSNumber numberWithInt:value - 1];
- }
- }
- }
- - (void)showCreateFolderAlert
- {
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_CHOOSE_NAME_TITLE", @"") message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", @"") cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", @"")]];
- [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
- [[alert textFieldAtIndex:0] setText:NSLocalizedString(@"FOLDER_NAME_PLACEHOLDER", @"")];
- [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
- __weak VLCAlertView *weakAlert = alert;
- alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
- if (cancelled)
- [self updateViewContents];
- else
- [self createFolderWithName:[weakAlert textFieldAtIndex:0].text];
- };
- [alert show];
- }
- - (void)createFolder
- {
- if (_libraryMode == VLCLibraryModeCreateFolder) {
- _libraryMode = _previousLibraryMode;
- [self updateViewContents];
- [self showCreateFolderAlert];
- return;
- }
- if (!_usingTableViewToShowData)
- _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
- else
- _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
- for (NSInteger i = _indexPaths.count - 1; i >=0; i--) {
- NSIndexPath *path = _indexPaths[i];
- id mediaObject;
- if (!_usingTableViewToShowData)
- mediaObject = _foundMedia[path.item];
- else
- mediaObject = _foundMedia[path.row];
- if ([mediaObject isKindOfClass:[MLLabel class]])
- [_indexPaths removeObject:path];
- }
- if ([_indexPaths count] != 0) {
- NSArray *folder = [MLLabel allLabels];
- //if we already have folders display them
- if ([folder count] > 0) {
- _foundMedia = [NSMutableArray arrayWithArray:folder];
- self.title = NSLocalizedString(@"SELECT_FOLDER", @"");
- _previousLibraryMode = _libraryMode;
- _libraryMode = VLCLibraryModeCreateFolder;
- [self reloadViews];
- return;
- }
- }
- //no selected items or no existing folder ask for foldername
- [self showCreateFolderAlert];
- }
- - (void)removeFromFolder
- {
- if (!_usingTableViewToShowData)
- _indexPaths = [NSMutableArray arrayWithArray:[self.collectionView indexPathsForSelectedItems]];
- else
- _indexPaths = [NSMutableArray arrayWithArray:[self.tableView indexPathsForSelectedRows]];
- [_indexPaths sortUsingSelector:@selector(compare:)];
- for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
- NSIndexPath *path = _indexPaths[i];
- MLFile *file = (MLFile *)_foundMedia[_usingTableViewToShowData ? path.row : path.item];
- MLLabel *folder = [file.labels anyObject];
- [self rearrangeFolderTrackNumbersForRemovedItem:file];
- file.labels = nil;
- file.folderTrackNumber = nil;
- [_foundMedia removeObject:file];
- if ([folder.files count] == 0) {
- [self removeMediaObject:folder updateDatabase:YES];
- [self setEditing:NO];
- [self backToAllItems:nil];
- }
- }
- [self reloadViews];
- }
- - (void)createFolderWithName:(NSString *)folderName
- {
- NSArray *labels = [MLLabel allLabels];
- for (MLLabel *label in labels){
- if ([label.name isEqualToString:folderName]) {
- _folderObject = nil;
- _indexPaths = nil;
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_TITLE", @"") message:NSLocalizedString(@"FOLDER_NAME_DUPLICATE_MESSAGE", @"") cancelButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"BUTTON_OK", @"")]];
- alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
- [self updateViewContents];
- };
- [alert show];
- return;
- }
- }
- if (_folderObject != nil) {
- NSUInteger folderIndex = [_foundMedia indexOfObject:_folderObject];
- //item got dragged onto item
- if ([_foundMedia[folderIndex] isKindOfClass:[MLFile class]]) {
- MLFile *file = _foundMedia[folderIndex];
- MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
- label.name = folderName;
- file.labels = [NSSet setWithObjects:label,nil];
- NSNumber *folderTrackNumber = [NSNumber numberWithInt:(int)[label files].count - 1];
- file.folderTrackNumber = folderTrackNumber;
- [_foundMedia removeObjectAtIndex:folderIndex];
- [_foundMedia insertObject:label atIndex:folderIndex];
- MLFile *itemFile = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
- itemFile.labels = file.labels;
- [_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
- itemFile.folderTrackNumber = @([label files].count - 1);
- } else {
- //item got dragged onto folder or items should be added to folder
- MLLabel *label = _foundMedia[folderIndex];
- [_indexPaths sortUsingSelector:@selector(compare:)];
- for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
- NSIndexPath *path = _indexPaths[i];
- if (_libraryMode != VLCLibraryModeCreateFolder && ![_foundMedia[path.row] isKindOfClass:[MLFile class]])
- continue;
- if (_libraryMode == VLCLibraryModeCreateFolder)
- [self updateViewContents];
- MLFile *file = _foundMedia[path.row];
- file.labels = [NSSet setWithObjects:label, nil];
- [_foundMedia removeObjectAtIndex:path.row];
- file.folderTrackNumber = @([label files].count - 1);
- }
- }
- _folderObject = nil;
- } else {
- //create new folder
- MLLabel *label = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"Label"];
- label.name = folderName;
- //if items were selected
- if ([_indexPaths count] != 0) {
- [_indexPaths sortUsingSelector:@selector(compare:)];
- for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
- NSIndexPath *path = _indexPaths[i];
- if (!_usingTableViewToShowData) {
- MLFile *file = _foundMedia[path.item];
- file.labels = [NSSet setWithObjects:label, nil];
- file.folderTrackNumber = @([label files].count - 1);
- [_foundMedia removeObjectAtIndex:path.item];
- } else {
- MLFile *file = _foundMedia[path.row];
- file.labels = [NSSet setWithObjects:label, nil];
- file.folderTrackNumber = @([label files].count - 1);
- [_foundMedia removeObjectAtIndex:path.row];
- }
- }
- }
- }
- _indexPaths = nil;
- [self setEditing:NO];
- [self updateViewContents];
- }
- - (void)_collectionViewHandleLongPressGesture:(UIGestureRecognizer *) sender
- {
- [self setEditing:YES animated:YES];
- }
- #pragma mark - UI implementation
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated
- {
- [super setEditing:editing animated:animated];
- UIBarButtonItem *editButton = self.editButtonItem;
- NSString *editImage = editing? @"doneButton": @"button";
- NSString *editImageHighlight = editing? @"doneButtonHighlight": @"buttonHighlight";
- if (SYSTEM_RUNS_IOS7_OR_LATER)
- editButton.tintColor = [UIColor whiteColor];
- else {
- [editButton setBackgroundImage:[UIImage imageNamed:editImage] forState:UIControlStateNormal
- barMetrics:UIBarMetricsDefault];
- [editButton setBackgroundImage:[UIImage imageNamed:editImageHighlight]
- forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
- [editButton setTitleTextAttributes: editing ? @{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} : @{UITextAttributeTextShadowColor : [UIColor VLCDarkTextShadowColor], UITextAttributeTextColor : [UIColor whiteColor]} forState:UIControlStateNormal];
- }
- if (!_usingTableViewToShowData) {
- NSArray *visibleCells = self.collectionView.visibleCells;
- [visibleCells enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- VLCPlaylistCollectionViewCell *aCell = (VLCPlaylistCollectionViewCell*)obj;
- [aCell setEditing:editing animated:animated];
- }];
- self.collectionView.allowsMultipleSelection = editing;
- /* UIKit doesn't clear the selection automagically if we leave the editing mode
- * so we need to do so manually */
- if (!editing) {
- [self.collectionView addGestureRecognizer:_longPressGestureRecognizer];
- NSArray *selectedItems = [self.collectionView indexPathsForSelectedItems];
- NSUInteger count = selectedItems.count;
- for (NSUInteger x = 0; x < count; x++)
- [self.collectionView deselectItemAtIndexPath:selectedItems[x] animated:NO];
- } else
- [self.collectionView removeGestureRecognizer:_longPressGestureRecognizer];
- } else {
- self.tableView.allowsMultipleSelectionDuringEditing = editing;
- [self.tableView setEditing:editing animated:YES];
- [self.editButtonItem setTitle:editing ? NSLocalizedString(@"BUTTON_CANCEL",@"") : NSLocalizedString(@"BUTTON_EDIT", @"")];
- }
- if (_libraryMode == VLCLibraryModeCreateFolder) {
- _libraryMode = _previousLibraryMode;
- _indexPaths = nil;
- [self updateViewContents];
- }
- self.navigationController.toolbarHidden = !editing;
- }
- - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleDelete;
- }
- - (IBAction)leftButtonAction:(id)sender
- {
- [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
- if (self.isEditing)
- [self setEditing:NO animated:YES];
- }
- - (IBAction)backToAllItems:(id)sender
- {
- if (!_usingTableViewToShowData) {
- if (![self.collectionView.collectionViewLayout isEqual:_folderLayout]) {
- //for some reason the Gesturerecognizer block themselves if not removed manually
- for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
- if (recognizer == _reorderLayout.panGestureRecognizer || recognizer == _reorderLayout.longPressGestureRecognizer)
- [self.collectionView removeGestureRecognizer:recognizer];
- }
- _folderLayout = [[VLCFolderCollectionViewFlowLayout alloc] init];
- [self.collectionView setCollectionViewLayout:_folderLayout animated:NO];
- [_collectionView addGestureRecognizer:_longPressGestureRecognizer];
- }
- }
- inFolder = NO;
- UIBarButtonItem *createFolderItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(createFolder)];
- NSMutableArray *toolbarItems = [self.toolbarItems mutableCopy];
- toolbarItems[0] = createFolderItem;
- self.toolbarItems = toolbarItems;
- [self setLibraryMode:_libraryMode];
- [self updateViewContents];
- }
- - (void)_endEditingWithHardReset:(BOOL)hardReset
- {
- [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
- if (hardReset)
- [self updateViewContents];
- else
- [self reloadViews];
- [self setEditing:NO animated:YES];
- }
- - (void)deleteSelection
- {
- NSArray *indexPaths;
- if (!_usingTableViewToShowData)
- indexPaths = [self.collectionView indexPathsForSelectedItems];
- else
- indexPaths = [self.tableView indexPathsForSelectedRows];
- NSUInteger count = indexPaths.count;
- NSMutableArray *objects = [[NSMutableArray alloc] initWithCapacity:count];
- for (NSUInteger x = 0; x < count; x++)
- [objects addObject:_foundMedia[[indexPaths[x] row]]];
- for (NSUInteger x = 0; x < count; x++)
- [self removeMediaObject:objects[x] updateDatabase:NO];
- [self _endEditingWithHardReset:YES];
- }
- - (void)renameSelection
- {
- NSArray *indexPaths;
- if (!_usingTableViewToShowData)
- indexPaths = [self.collectionView indexPathsForSelectedItems];
- else
- indexPaths = [self.tableView indexPathsForSelectedRows];
- if (indexPaths.count < 1) {
- [self _endEditingWithHardReset:NO];
- return;
- }
- NSString *itemName;
- if (!_usingTableViewToShowData)
- itemName = [(VLCPlaylistCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPaths[0]] titleLabel].text;
- else
- itemName = [(VLCPlaylistTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPaths[0]] titleLabel].text;
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"RENAME_MEDIA_TO", @""), itemName] message:nil cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:@[NSLocalizedString(@"BUTTON_RENAME", @"")]];
- [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
- [[alert textFieldAtIndex:0] setText:itemName];
- [[alert textFieldAtIndex:0] setClearButtonMode:UITextFieldViewModeAlways];
- __weak VLCAlertView *weakAlert = alert;
- alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {
- if (cancelled)
- [self _endEditingWithHardReset:NO];
- else
- [self renameMediaObjectTo:[weakAlert textFieldAtIndex:0].text];
- };
- [alert show];
- }
- - (void)renameMediaObjectTo:(NSString*)newName
- {
- NSArray *indexPaths;
- if (!_usingTableViewToShowData)
- indexPaths = [self.collectionView indexPathsForSelectedItems];
- else
- indexPaths = [self.tableView indexPathsForSelectedRows];
- if (indexPaths.count < 1)
- return;
- id mediaObject = _foundMedia[[indexPaths[0] row]];
- if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShowEpisode class]] || [mediaObject isKindOfClass:[MLShow class]] || [mediaObject isKindOfClass:[MLLabel class]] )
- [mediaObject setName:newName];
- else
- [mediaObject setTitle:newName];
- if (!_usingTableViewToShowData)
- [self.collectionView deselectItemAtIndexPath:indexPaths[0] animated:YES];
- else
- [self.tableView deselectRowAtIndexPath:indexPaths[0] animated:YES];
- if (indexPaths.count > 1)
- [self renameSelection];
- else
- [self _endEditingWithHardReset:NO];
- }
- #pragma mark - coin coin
- - (void)setLibraryMode:(VLCLibraryMode)mode
- {
- _libraryMode = mode;
- [self updateViewContents];
- }
- #pragma mark - autorotation
- // RootController is responsible for supporting interface orientation(iOS6.0+), i.e. navigation controller
- // so this will not work as intended without "voodoo magic"(UINavigationController category, subclassing, etc)
- /* introduced in iOS 6 */
- - (NSUInteger)supportedInterfaceOrientations
- {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- return UIInterfaceOrientationMaskAll;
- return (_foundMedia.count > 0)? UIInterfaceOrientationMaskAllButUpsideDown:
- UIInterfaceOrientationMaskPortrait;
- }
- /* introduced in iOS 6 */
- - (BOOL)shouldAutorotate
- {
- return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (_foundMedia.count > 0);
- }
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- {
- [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
- if (!_usingTableViewToShowData)
- [self.collectionView.collectionViewLayout invalidateLayout];
- }
- #pragma mark - Search Display Controller Delegate
- - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
- {
- NSInteger listCount = _foundMedia.count;
- [_searchData removeAllObjects];
- NSManagedObject *item;
- NSRange nameRange;
- for (int i = 0; i < listCount; i++) {
- item = _foundMedia[i];
- if ([item isKindOfClass:[MLAlbum class]]) {
- nameRange = [[(MLAlbum*)item name] rangeOfString:searchString options:NSCaseInsensitiveSearch];
- if (nameRange.location == NSNotFound) {
- NSString *releaseYear = [(MLAlbum *)item releaseYear];
- if (releaseYear)
- nameRange = [releaseYear rangeOfString:searchString options:NSCaseInsensitiveSearch];
- /* user didn't search for our album name or year, let's do a deeper search */
- if (nameRange.location == NSNotFound) {
- NSArray *tracks = [(MLAlbum*)item sortedTracks];
- NSUInteger trackCount = tracks.count;
- for (NSUInteger x = 0; x < trackCount; x++) {
- nameRange = [self _processItem:tracks[x] withString:searchString];
- if (nameRange.location != NSNotFound)
- break;
- }
- }
- }
- } else if ([item isKindOfClass:[MLShow class]]) {
- nameRange = [[(MLShow*)item name] rangeOfString:searchString options:NSCaseInsensitiveSearch];
- /* user didn't search for our show name, let's do a deeper search */
- if (nameRange.location == NSNotFound) {
- NSArray *episodes = [(MLShow*)item sortedEpisodes];
- NSUInteger episodeCount = episodes.count;
- NSString *name;
- for (NSUInteger x = 0; x < episodeCount; x++) {
- name = [(MLShowEpisode*)episodes[x] name];
- if (name)
- nameRange = [name rangeOfString:searchString options:NSCaseInsensitiveSearch];
- if (nameRange.location != NSNotFound) {
- break;
- }
- }
- }
- } else if ([item isKindOfClass:[MLLabel class]]) {
- nameRange = [[(MLLabel*)item name] rangeOfString:searchString options:NSCaseInsensitiveSearch];
- /* user didn't search for our label name, let's do a deeper search */
- if (nameRange.location == NSNotFound) {
- NSArray *files = [(MLLabel*)item sortedFolderItems];
- NSUInteger fileCount = files.count;
- for (NSUInteger x = 0; x < fileCount; x++) {
- nameRange = [self _processItem:files[x] withString:searchString];
- if (nameRange.location != NSNotFound)
- break;
- }
- }
- } else // simple file
- nameRange = [self _processItem:(MLFile*)item withString:searchString];
- if (nameRange.location != NSNotFound)
- [_searchData addObject:item];
- }
- return YES;
- }
- - (NSRange)_processItem:(NSManagedObject *)file withString:(NSString *)searchString
- {
- NSRange nameRange;
- nameRange = [[(MLFile *)file title] rangeOfString:searchString options:NSCaseInsensitiveSearch];
- if (nameRange.location == NSNotFound) {
- NSString *artist = [(MLFile *)file artist];
- if (artist)
- nameRange = [artist rangeOfString:searchString options:NSCaseInsensitiveSearch];
- if (nameRange.location == NSNotFound && ![file isKindOfClass:[MLAlbumTrack class]]) {
- NSString *releaseYear = [(MLFile *)file releaseYear];
- if (releaseYear)
- nameRange = [releaseYear rangeOfString:searchString options:NSCaseInsensitiveSearch];
- }
- }
- return nameRange;
- }
- - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
- {
- tableView.rowHeight = 90.;
- tableView.backgroundColor = [UIColor blackColor];
- }
- @end
|