Commit b3ac2b75 authored by Felix Paul Kühne's avatar Felix Paul Kühne

VLCPlaylistCollectionViewCell: deploy VLCThumbnailsCache

parent e493f4ce
...@@ -9,10 +9,8 @@ ...@@ -9,10 +9,8 @@
// //
#import "VLCPlaylistCollectionViewCell.h" #import "VLCPlaylistCollectionViewCell.h"
#import "VLCPlaylistViewController.h" #import "VLCPlaylistViewController.h"
#import "VLCThumbnailsCache.h"
#define MAX_CACHE_SIZE 27 // three times the number of items shown on iPad
@implementation VLCPlaylistCollectionViewCell @implementation VLCPlaylistCollectionViewCell
...@@ -74,7 +72,7 @@ ...@@ -74,7 +72,7 @@
[self configureForMLFile:mediaObject]; [self configureForMLFile:mediaObject];
if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack) { if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack) {
self.thumbnailView.image = [self thumbnailForMediaFile:mediaObject]; self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
} }
} else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) { } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
MLAlbum *mediaObject = (MLAlbum *)self.mediaObject; MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
...@@ -90,7 +88,7 @@ ...@@ -90,7 +88,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) { if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject; MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
self.thumbnailView.image = [self thumbnailForMediaFile:anyFileFromAnyEpisode]; self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
} }
} else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) { } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject; MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
...@@ -98,7 +96,7 @@ ...@@ -98,7 +96,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) { if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
MLFile *anyFileFromEpisode = mediaObject.files.anyObject; MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
self.thumbnailView.image = [self thumbnailForMediaFile:anyFileFromEpisode]; self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
} }
} }
...@@ -216,42 +214,4 @@ ...@@ -216,42 +214,4 @@
self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue; self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
} }
// Can be extracted outside of VLCPlaylistCollectionViewCell
- (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile {
if (mediaFile == nil || mediaFile.objectID == nil)
return nil;
static NSMutableArray *_thumbnailCacheIndex;
static NSMutableDictionary *_thumbnailCache;
if (!_thumbnailCache)
_thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
if (!_thumbnailCacheIndex)
_thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
NSManagedObjectID *objID = mediaFile.objectID;
UIImage *displayedImage = nil;
if ([_thumbnailCacheIndex containsObject:objID]) {
[_thumbnailCacheIndex removeObject:objID];
[_thumbnailCacheIndex insertObject:objID atIndex:0];
displayedImage = [_thumbnailCache objectForKey:objID];
if (!displayedImage && mediaFile.computedThumbnail) {
displayedImage = mediaFile.computedThumbnail;
[_thumbnailCache setObject:displayedImage forKey:objID];
}
} else {
if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
[_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
[_thumbnailCacheIndex removeLastObject];
}
displayedImage = mediaFile.computedThumbnail;
if (displayedImage) {
[_thumbnailCache setObject:displayedImage forKey:objID];
[_thumbnailCacheIndex insertObject:objID atIndex:0];
}
}
return displayedImage;
}
@end @end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment