ios: Some cover view improvements:

Don't allocate new views when reusing a cell.
   If a file item doesn't have an attached image, its filename will be printed in the cell instead.
   Images maintain aspect ratio when scaled.
This commit is contained in:
meancoot 2013-02-21 01:30:28 -05:00
parent cae85b7401
commit 1212116dfb
3 changed files with 47 additions and 12 deletions

View File

@ -40,7 +40,9 @@
self.navigationItem.rightBarButtonItem = [RetroArch_iOS get].settings_button; self.navigationItem.rightBarButtonItem = [RetroArch_iOS get].settings_button;
[self setTitle: [_path lastPathComponent]]; [self setTitle: [_path lastPathComponent]];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"filecell"]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"dircell"];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"textcell"];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"imagecell"];
return self; return self;
} }
@ -55,7 +57,6 @@
return [_list count]; return [_list count];
} }
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ {
RADirectoryItem* path = [_list objectAtIndex: indexPath.row]; RADirectoryItem* path = [_list objectAtIndex: indexPath.row];
@ -69,17 +70,42 @@
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ {
RADirectoryItem* path = [_list objectAtIndex: indexPath.row]; RADirectoryItem* path = [_list objectAtIndex: indexPath.row];
UICollectionViewCell* cell = nil;
UICollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"filecell" forIndexPath:indexPath];
if (path.isDirectory) if (path.isDirectory)
{
cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"dircell" forIndexPath:indexPath];
if (!cell.backgroundView)
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[RetroArch_iOS get].folder_icon]; cell.backgroundView = [[UIImageView alloc] initWithImage:[RetroArch_iOS get].folder_icon];
((UIImageView*)cell.backgroundView).contentMode = UIViewContentModeScaleAspectFit;
}
}
else if (path.coverPath)
{
cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"imagecell" forIndexPath:indexPath];
if (!cell.backgroundView)
{
cell.backgroundView = [UIImageView new];
((UIImageView*)cell.backgroundView).contentMode = UIViewContentModeScaleAspectFit;
}
((UIImageView*)cell.backgroundView).image = [UIImage imageWithContentsOfFile:path.coverPath];
}
else else
{ {
NSString* img = [NSString stringWithFormat:@"%@/.coverart/%@.png", _path, [[path.path lastPathComponent] stringByDeletingPathExtension]]; cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"textcell" forIndexPath:indexPath];
if (ra_ios_is_file(img))
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:img]]; if (!cell.backgroundView)
else {
cell.backgroundView = [[UIImageView alloc] initWithImage:_templateImage]; cell.backgroundView = [UILabel new];
((UILabel*)cell.backgroundView).numberOfLines = 0;
((UILabel*)cell.backgroundView).textAlignment = NSTextAlignmentCenter;
}
((UILabel*)cell.backgroundView).text = [path.path lastPathComponent];
} }
return cell; return cell;

View File

@ -20,6 +20,7 @@ extern NSString* ra_ios_get_browser_root();
@interface RADirectoryItem : NSObject @interface RADirectoryItem : NSObject
@property (strong) NSString* path; @property (strong) NSString* path;
@property (strong) NSString* coverPath;
@property bool isDirectory; @property bool isDirectory;
@end @end

View File

@ -18,7 +18,7 @@
#import "browser.h" #import "browser.h"
@implementation RADirectoryItem @implementation RADirectoryItem
+ (RADirectoryItem*)directoryItemFromPath:(const char*)thePath + (RADirectoryItem*)directoryItemFromPath:(const char*)thePath checkForCovers:(BOOL)checkCovers
{ {
RADirectoryItem* result = [RADirectoryItem new]; RADirectoryItem* result = [RADirectoryItem new];
result.path = [NSString stringWithUTF8String:thePath]; result.path = [NSString stringWithUTF8String:thePath];
@ -27,6 +27,14 @@
if (stat(thePath, &statbuf) == 0) if (stat(thePath, &statbuf) == 0)
result.isDirectory = S_ISDIR(statbuf.st_mode); result.isDirectory = S_ISDIR(statbuf.st_mode);
if (checkCovers && !result.isDirectory)
{
result.coverPath = [NSString stringWithFormat:@"%@/.coverart/%@.png", [result.path stringByDeletingLastPathComponent], [[result.path lastPathComponent] stringByDeletingPathExtension]];
if (!ra_ios_is_file(result.coverPath))
result.coverPath = nil;
}
return result; return result;
} }
@end @end
@ -65,7 +73,7 @@ NSArray* ra_ios_list_directory(NSString* path, NSRegularExpression* regex)
cpath[cpath_end] = 0; cpath[cpath_end] = 0;
strcat(cpath, item->d_name); strcat(cpath, item->d_name);
[result addObject:[RADirectoryItem directoryItemFromPath:cpath]]; [result addObject:[RADirectoryItem directoryItemFromPath:cpath checkForCovers:YES]];
} }
closedir(dir); closedir(dir);