Commit 7c94c41e authored by Carola Nitz's avatar Carola Nitz

first hit at seperating viewpresentation from the underlying media management....

first hit at seperating viewpresentation from the underlying media management. Moved the _foundmedia array into it's own class VLCMediaDataSource.m.
This way a lot of duplicated logic got removed when accessing this array.
That way it won't be accessed without the synchronized block and the UpdateViewContents method was cleaned up as well.
parent 49b540c5
Pipeline #100 failed with stage
in 0 seconds
......@@ -16,4 +16,5 @@
- (UINib *)nibclass;
- (NSManagedObject *)objectAtIndex:(NSUInteger)index;
- (void)shouldReloadTableForSearchString:(NSString *)searchString searchableFiles:(NSArray *)files;
@end
......@@ -24,13 +24,13 @@
#import "VLCFirstStepsViewController.h"
#import "VLCFolderCollectionViewFlowLayout.h"
#import "VLCKeychainCoordinator.h"
#import "VLCMediaDataSource.h"
#import "VLCLibrarySearchDisplayDataSource.h"
#import "VLCMovieViewController.h"
#import "VLCNavigationController.h"
#import "VLCPlaylistCollectionViewCell.h"
#import "VLCPlaylistTableViewCell.h"
#import "NSString+SupportedMedia.h"
#import "VLCPlaybackController+MediaLibrary.h"
#import <CoreSpotlight/CoreSpotlight.h>
......@@ -51,7 +51,6 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
@end
@interface VLCLibraryViewController () <VLCFolderCollectionViewDelegateFlowLayout, LXReorderableCollectionViewDataSource, LXReorderableCollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate, MLMediaLibrary, VLCMediaListDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
NSMutableArray *_foundMedia;
VLCLibraryMode _libraryMode;
VLCLibraryMode _previousLibraryMode;
UIBarButtonItem *_menuButton;
......@@ -65,6 +64,8 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
UILongPressGestureRecognizer *_longPressGestureRecognizer;
VLCLibrarySearchDisplayDataSource *_searchDataSource;
VLCMediaDataSource *_mediaDataSource;
UISearchBar *_searchBar;
UISearchDisplayController *_searchDisplayController;
......@@ -103,6 +104,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
[self updateViewsForCurrentDisplayMode];
_libraryMode = VLCLibraryModeAllFiles;
_searchDataSource = [VLCLibrarySearchDisplayDataSource new];
_mediaDataSource = [VLCMediaDataSource new];
self.emptyLibraryView = [[[NSBundle mainBundle] loadNibNamed:@"VLCEmptyLibraryView" owner:self options:nil] lastObject];
_emptyLibraryView.emptyLibraryLongDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
......@@ -270,7 +272,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
[defaults synchronize];
}
if (_foundMedia.count < 1)
if ([_mediaDataSource numberOfFiles] < 1)
[self updateViewContents];
[[MLMediaLibrary sharedMediaLibrary] performSelector:@selector(libraryDidAppear) withObject:nil afterDelay:1.];
}
......@@ -295,12 +297,9 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
- (void)openMediaObject:(NSManagedObject *)mediaObject
{
if ([mediaObject isKindOfClass:[MLAlbum class]] || [mediaObject isKindOfClass:[MLShow class]]) {
[_mediaDataSource updateContentsForSelection:mediaObject];
BOOL isAlbum = [mediaObject isKindOfClass:[MLAlbum class]];
NSArray* array = isAlbum ? [(MLAlbum *)mediaObject sortedTracks] : [(MLShow *)mediaObject sortedEpisodes];
@synchronized(_foundMedia) {
_foundMedia = [NSMutableArray arrayWithArray:array];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
if (_libraryMode == VLCLibraryModeAllFiles)
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", nil);
......@@ -309,7 +308,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
self.title = [(MLAlbum*)mediaObject name];
[self reloadViews];
} else if ([mediaObject isKindOfClass:[MLLabel class]]) {
MLLabel *folder = (MLLabel*) mediaObject;
[_mediaDataSource updateContentsForSelection:mediaObject];
_inFolder = YES;
[self.navigationController.scrollNavigationBar resetToDefaultPositionWithAnimation:YES];
if (!self.usingTableViewToShowData) {
......@@ -323,12 +322,10 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
}
}
_libraryMode = VLCLibraryModeFolder;
@synchronized(_foundMedia) {
_foundMedia = [NSMutableArray arrayWithArray:[folder sortedFolderItems]];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(backToAllItems:)];
self.navigationItem.leftBarButtonItem.title = NSLocalizedString(@"BUTTON_BACK", nil);
self.title = [folder name];
self.title = [(MLLabel*)mediaObject name];
_removeFromFolderBarButtonItem.enabled = YES;
_createFolderBarButtonItem.enabled = NO;
......@@ -369,55 +366,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
- (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];
}
[[MLMediaLibrary sharedMediaLibrary] removeObject: album];
// 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];
}
[[MLMediaLibrary sharedMediaLibrary] removeObject: show];
// 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
[[MLMediaLibrary sharedMediaLibrary] removeObject: episode];
} 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];
[_mediaDataSource removeMediaObject:managedObject];
if (updateDB) {
[[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
......@@ -425,48 +374,12 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
}
}
- (void)_deleteMediaObject:(MLFile *)mediaObject
{
if (_inFolder)
[self rearrangeFolderTrackNumbersForRemovedItem:mediaObject];
/* stop playback if needed */
VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
if (vpc.isPlaying) {
MLFile *currentlyPlayingFile = [[MLFile fileForURL:vpc.mediaPlayer.media.url] firstObject];
if (currentlyPlayingFile) {
if (currentlyPlayingFile == mediaObject)
[vpc stopPlayback];
}
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folderLocation = [[mediaObject.url path] stringByDeletingLastPathComponent];
NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
NSString *fileName = [mediaObject.path.lastPathComponent stringByDeletingPathExtension];
if (!fileName)
return;
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 removeItemAtURL:mediaObject.url error:nil];
}
- (void)_displayEmptyLibraryViewIfNeeded
{
if (self.emptyLibraryView.superview)
[self.emptyLibraryView removeFromSuperview];
if (_foundMedia.count == 0) {
if ([_mediaDataSource numberOfFiles] == 0) {
_inFolder = (_libraryMode == VLCLibraryModeFolder || _libraryMode == VLCLibraryModeCreateFolder);
self.emptyLibraryView.emptyLibraryLabel.text = _inFolder ? NSLocalizedString(@"FOLDER_EMPTY", nil) : NSLocalizedString(@"EMPTY_LIBRARY", nil);
self.emptyLibraryView.emptyLibraryLongDescriptionLabel.text = _inFolder ? NSLocalizedString(@"FOLDER_EMPTY_LONG", nil) : NSLocalizedString(@"EMPTY_LIBRARY_LONG", nil);
......@@ -483,12 +396,12 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
}
if (self.usingTableViewToShowData)
_tableView.separatorStyle = (_foundMedia.count > 0)? UITableViewCellSeparatorStyleSingleLine:
if (self.usingTableViewToShowData) {
_tableView.separatorStyle = [_mediaDataSource numberOfFiles] > 0 ? UITableViewCellSeparatorStyleSingleLine:
UITableViewCellSeparatorStyleNone;
else
} else {
[self.collectionView.collectionViewLayout invalidateLayout];
}
[self updateViewsForCurrentDisplayMode];
}
......@@ -554,112 +467,40 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
- (void)updateViewContents
{
@synchronized(self) {
_foundMedia = [[NSMutableArray alloc] init];
}
if (![(VLCAppDelegate *)[UIApplication sharedApplication].delegate passcodeValidated]) {
APLog(@"library is locked, won't show contents");
return;
}
self.navigationItem.leftBarButtonItem = _menuButton;
if (_libraryMode == VLCLibraryModeAllAlbums)
self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
else if( _libraryMode == VLCLibraryModeAllSeries)
self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
else
self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
_createFolderBarButtonItem.enabled = (_libraryMode == VLCLibraryModeAllAlbums || _libraryMode == VLCLibraryModeAllSeries) ? NO : YES;
[_mediaDataSource removeAllObjects];
_removeFromFolderBarButtonItem.enabled = NO;
/* add all albums */
if (_libraryMode != VLCLibraryModeAllSeries) {
NSArray *rawAlbums = [MLAlbum allAlbums];
for (MLAlbum *album in rawAlbums) {
if (_libraryMode != VLCLibraryModeAllAlbums) {
if (album.name.length > 0 && album.tracks.count > 1) {
@synchronized(_foundMedia) {
[_foundMedia addObject:album];
}
}
} else {
if (album.name.length > 0) {
@synchronized(_foundMedia) {
[_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) {
@synchronized(_foundMedia) {
[_foundMedia addObject:show];
}
}
}
if (_libraryMode == VLCLibraryModeAllSeries) {
[self reloadViews];
return;
}
/* add all folders*/
NSArray *allFolders = [MLLabel allLabels];
for (MLLabel *folder in allFolders) {
@synchronized(_foundMedia) {
[_foundMedia addObject:folder];
}
}
/* add all remaining files */
NSArray *allFiles = [MLFile allFiles];
for (MLFile *file in allFiles) {
if (file.labels != nil) {
@synchronized(file.labels) {
if (file.labels.count > 0)
continue;
}
}
if (!file.isShowEpisode && !file.isAlbumTrack) {
@synchronized(_foundMedia) {
[_foundMedia addObject:file];
}
}
else if (file.isShowEpisode) {
if (file.showEpisode.show.episodes.count < 2) {
@synchronized(_foundMedia) {
[_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", nil);
[self performSelector:@selector(updateViewContents) withObject:nil afterDelay:0.1];
}
} else if (file.isAlbumTrack) {
if (file.albumTrack.album.tracks.count < 2) {
@synchronized(_foundMedia) {
[_foundMedia addObject:file];
}
}
switch (_libraryMode) {
case VLCLibraryModeAllAlbums: {
self.title = NSLocalizedString(@"LIBRARY_MUSIC", nil);
_createFolderBarButtonItem.enabled = NO;
[_mediaDataSource addAlbumsInAllAlbumMode:YES];
} break;
case VLCLibraryModeAllSeries: {
self.title = NSLocalizedString(@"LIBRARY_SERIES", nil);
_createFolderBarButtonItem.enabled = NO;
[_mediaDataSource addAllShows];
} break;
//Todo: I'm not sure if updateViewContents should be called in VLCLibraryModeFolder
//Here should maybe be an NSAssert to prevent this but for now due to refactoring these calls would've been made in that case
case VLCLibraryModeAllFiles:
case VLCLibraryModeFolder:
case VLCLibraryModeCreateFolder: {
self.title = NSLocalizedString(@"LIBRARY_ALL_FILES", nil);
_createFolderBarButtonItem.enabled = YES;
[_mediaDataSource addAlbumsInAllAlbumMode:NO];
[_mediaDataSource addAllShows];
[_mediaDataSource addAllFolders];
[_mediaDataSource addRemainingFiles];
}
}
[self reloadViews];
}
......@@ -700,14 +541,9 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _foundMedia.count;
return [_mediaDataSource numberOfFiles];
}
// Customize the appearance of table view cells.
......@@ -725,30 +561,14 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
[swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:swipeRight];
NSInteger row = indexPath.row;
@synchronized (_foundMedia) {
if (row < _foundMedia.count)
cell.mediaObject = _foundMedia[row];
}
cell.mediaObject = [_mediaDataSource objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
@synchronized(_foundMedia) {
MLFile* object = _foundMedia[fromIndexPath.item];
[_foundMedia removeObjectAtIndex:fromIndexPath.item];
[_foundMedia insertObject:object atIndex:toIndexPath.item];
if (![object isKindOfClass:[MLFile class]])
return;
object.folderTrackNumber = @(toIndexPath.item - 1);
object = [_foundMedia objectAtIndex:fromIndexPath.item];
if (![object isKindOfClass:[MLFile class]])
return;
object.folderTrackNumber = @(fromIndexPath.item - 1);
}
[_mediaDataSource moveObjectFromIndex:fromIndexPath.item toIndex:toIndexPath.item];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
......@@ -762,17 +582,12 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
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) {
NSInteger row = indexPath.row;
_deleteFromTableView = YES;
if (row < _foundMedia.count)
if (row < [_mediaDataSource numberOfFiles])
[self deleteSelection:indexPath];
}
}
......@@ -791,16 +606,14 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
return;
}
NSArray *visibleCells = [tableView visibleCells];
NSUInteger cellCount = visibleCells.count;
for (NSUInteger x = 0; x < cellCount; x++) {
if ([visibleCells[x] isExpanded])
[visibleCells[x] collapsWithAnimation:NO];
for (VLCPlaylistTableViewCell *cell in [tableView visibleCells]) {
if ([cell isExpanded])
[cell collapsWithAnimation:NO];
}
if (tableView.isEditing) {
if (_libraryMode == VLCLibraryModeCreateFolder) {
_folderObject = _foundMedia[indexPath.row];
_folderObject = [_mediaDataSource objectAtIndex:indexPath.row];
_libraryMode = _previousLibraryMode;
[self updateViewContents];
[self createFolderWithName:nil];
......@@ -817,10 +630,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
if (tableView == self.searchDisplayController.searchResultsTableView) {
selectedObject = [_searchDataSource objectAtIndex:row];
} else {
@synchronized (_foundMedia) {
if (row < _foundMedia.count)
selectedObject = _foundMedia[row];
}
selectedObject = [_mediaDataSource objectAtIndex:row];
}
if (_searchDisplayController.active)
......@@ -833,11 +643,8 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
#pragma mark - Gesture Action
- (void)swipeRightOnTableViewCellGestureAction:(UIGestureRecognizer *)recognizer
{
if (self.tableView.isEditing) {
[self setEditing:NO animated:YES];
} else {
[self setEditing:YES animated:YES];
[self setEditing:!self.tableView.isEditing animated:YES];
if (!self.tableView.isEditing) {
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:[recognizer locationInView:self.tableView]];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:path.row inSection:path.section]
animated:YES
......@@ -856,17 +663,14 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
#pragma mark - Collection View
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _foundMedia.count;
return [_mediaDataSource numberOfFiles];
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
VLCPlaylistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaylistCell" forIndexPath:indexPath];
@synchronized (_foundMedia) {
NSUInteger row = indexPath.row;
if (row < _foundMedia.count)
cell.mediaObject = _foundMedia[row];
}
cell.mediaObject = [_mediaDataSource objectAtIndex:indexPath.row];
cell.collectionView = _collectionView;
......@@ -913,7 +717,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
{
if (self.editing) {
if (_libraryMode == VLCLibraryModeCreateFolder) {
_folderObject = _foundMedia[indexPath.item];
_folderObject = [_mediaDataSource objectAtIndex:indexPath.item];
[self updateViewContents];
[self createFolderWithName:nil];
_libraryMode = _previousLibraryMode;
......@@ -934,12 +738,8 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
[cell showMetadata:NO];
}
NSManagedObject *selectedObject;
NSInteger row = indexPath.row;
@synchronized(_foundMedia) {
if (row < _foundMedia.count)
selectedObject = _foundMedia[row];
}
NSManagedObject *selectedObject = [_mediaDataSource objectAtIndex:indexPath.row];
if (selectedObject != nil)
[self openMediaObject:selectedObject];
}
......@@ -955,45 +755,22 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
- (void)collectionView:(UICollectionView *)collectionView removeItemFromFolderAtIndexPathIfNeeded:(NSIndexPath *)indexPath
{
id mediaObject;
@synchronized(_foundMedia) {
mediaObject = _foundMedia[indexPath.item];
}
if (![mediaObject isKindOfClass:[MLFile class]])
return;
id mediaObject = [_mediaDataSource objectAtIndex:indexPath.item];
MLFile *mediaFile = (MLFile *)mediaObject;
[self rearrangeFolderTrackNumbersForRemovedItem:mediaFile];
mediaFile.labels = nil;
mediaFile.folderTrackNumber = nil;
[_mediaDataSource removeMediaObjectFromFolder:mediaObject];
[self backToAllItems:nil];
}
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath
{
@synchronized(_foundMedia) {
id object = _foundMedia[fromIndexPath.item];
if (![object isKindOfClass:[MLFile class]])
return;
[_foundMedia removeObjectAtIndex:fromIndexPath.item];
[_foundMedia insertObject:object atIndex:toIndexPath.item];
[(MLFile *)object setFolderTrackNumber: @(toIndexPath.item - 1)];
object = _foundMedia[fromIndexPath.item];
if (![object isKindOfClass:[MLFile class]])
return;
[(MLFile *)object setFolderTrackNumber: @(fromIndexPath.item - 1)];
}
[_mediaDataSource moveObjectFromIndex:fromIndexPath.item toIndex:toIndexPath.item];
}
- (void)collectionView:(UICollectionView *)collectionView requestToMoveItemAtIndexPath:(NSIndexPath *)itemPath intoFolderAtIndexPath:(NSIndexPath *)folderPath
{
id folderPathItem;
id itemPathItem;
@synchronized(_foundMedia) {
folderPathItem = _foundMedia[folderPath.item];
itemPathItem = _foundMedia[itemPath.item];
}
id folderPathItem = [_mediaDataSource objectAtIndex:folderPath.item];
id itemPathItem = [_mediaDataSource objectAtIndex:itemPath.item];
BOOL validFileTypeAtFolderPath = ([folderPathItem isKindOfClass:[MLFile class]] || [folderPathItem isKindOfClass:[MLLabel class]]) && [itemPathItem isKindOfClass:[MLFile class]];
......@@ -1014,9 +791,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
MLFile *file = itemPathItem;
[file setLabels:[[NSSet alloc] initWithObjects:folder, nil]];
file.folderTrackNumber = @([folder.files count] - 1);
@synchronized(_foundMedia) {
[_foundMedia removeObjectAtIndex:itemPath.item];
}
[_mediaDataSource removeObjectAtIndex:itemPath.item];
[self updateViewContents];
} else {
_folderObject = folderPathItem;
......@@ -1038,18 +813,6 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
}
#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", nil) message:NSLocalizedString(@"FOLDER_CHOOSE_NAME_MESSAGE", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:@[NSLocalizedString(@"BUTTON_SAVE", nil)]];
......@@ -1081,10 +844,7 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
for (NSInteger i = _indexPaths.count - 1; i >=0; i--) {
NSIndexPath *path = _indexPaths[i];
id mediaObject;
@synchronized(_foundMedia) {
mediaObject = _foundMedia[path.row];
}
id mediaObject = [_mediaDataSource objectAtIndex:path.row];
if ([mediaObject isKindOfClass:[MLLabel class]])
[_indexPaths removeObject:path];
}
......@@ -1093,9 +853,8 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
NSArray *folder = [MLLabel allLabels];
//if we already have folders display them
if ([folder count] > 0) {
@synchronized(self) {
_foundMedia = [NSMutableArray arrayWithArray:folder];
}
[_mediaDataSource removeAllObjects];
[_mediaDataSource addAllFolders];
self.title = NSLocalizedString(@"SELECT_FOLDER", nil);
_previousLibraryMode = _libraryMode;
_libraryMode = VLCLibraryModeCreateFolder;
......@@ -1115,20 +874,10 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
NSIndexPath *path = _indexPaths[i];
id item;
@synchronized(_foundMedia) {
item = _foundMedia[path.row];
}
id item = [_mediaDataSource objectAtIndex:path.row];
if ([item isKindOfClass:[MLFile class]]) {
MLFile *file = (MLFile *)item;
[self rearrangeFolderTrackNumbersForRemovedItem:file];
file.labels = nil;
file.folderTrackNumber = nil;
}
@synchronized(_foundMedia) {
[_foundMedia removeObject:item];
}
[_mediaDataSource removeMediaObjectFromFolder:item];
[_mediaDataSource removeObjectAtIndex:path.row];
}
[self reloadViews];
}
......@@ -1151,12 +900,9 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
}
if (_folderObject != nil) {
id mediaObject;
NSUInteger folderIndex;
@synchronized(_foundMedia) {
folderIndex = [_foundMedia indexOfObject:_folderObject];
mediaObject = _foundMedia[folderIndex];
}
//could be NSNotFound
NSUInteger folderIndex = [_mediaDataSource indexOfObject:_folderObject];
id mediaObject = [_mediaDataSource objectAtIndex:folderIndex];
//item got dragged onto item
if ([mediaObject isKindOfClass:[MLFile class]]) {
......@@ -1168,52 +914,47 @@ static NSString *kUsingTableViewToShowData = @"UsingTableViewToShowData";
NSNumber *folderTrackNumber = [NSNumber numberWithInt:(int)[label files].count - 1];
file.folderTrackNumber = folderTrackNumber;
id item;
@synchronized(_foundMedia) {
[_foundMedia removeObjectAtIndex:folderIndex];
[_foundMedia insertObject:label atIndex:folderIndex];
[_mediaDataSource removeObjectAtIndex:folderIndex];
[_mediaDataSource insertObject:label atIndex:folderIndex];
id item = [_mediaDataSource objectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
item = _foundMedia[((NSIndexPath *)_indexPaths[0]).item];
}
if (![item isKindOfClass:[MLFile class]])
return;
MLFile *itemFile = (MLFile *)item;
itemFile.labels = file.labels;
@synchronized(_foundMedia) {
[_foundMedia removeObjectAtIndex:((NSIndexPath *)_indexPaths[0]).item];
}
[_mediaDataSource 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;
@synchronized(_foundMedia) {
label = _foundMedia[folderIndex];
}
id label = [_mediaDataSource objectAtIndex:folderIndex];
if (![label isKindOfClass:[MLLabel class]])
return;
[_indexPaths sortUsingSelector:@selector(compare:)];
@synchronized(_foundMedia) {
NSUInteger count = _foundMedia.count;
for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
NSIndexPath *path = _indexPaths[i];
if (path.row >= count)
continue;
if (_libraryMode != VLCLibraryModeCreateFolder && ![_foundMedia[path.row] isKindOfClass:[MLFile class]])
continue;
if (_libraryMode == VLCLibraryModeCreateFolder)
[self updateViewContents];
NSUInteger count = [_mediaDataSource numberOfFiles];
for (NSInteger i = [_indexPaths count] - 1; i >= 0; i--) {
NSIndexPath *path = _indexPaths[i];
if (path.row >= count)
continue;
id object = [_mediaDataSource objectAtIndex:path.