Commit 62cb86fd authored by Gleb Pinigin's avatar Gleb Pinigin

Extract thumbnails cache as a separate class

parent 0a6f87f1
......@@ -10,10 +10,8 @@
#import "VLCPlaylistGridView.h"
#import "VLCLinearProgressIndicator.h"
#import "VLCAppDelegate.h"
#import "AQGridView.h"
#define MAX_CACHE_SIZE 27 // three times the number of items shown on iPad
#import "VLCThumbnailsCache.h"
@interface VLCPlaylistGridView (Hack)
@property (nonatomic, retain) NSString *reuseIdentifier;
......@@ -99,7 +97,7 @@
[self configureForMLFile:mediaObject];
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]]) {
......@@ -116,7 +114,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
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]]) {
MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
......@@ -124,7 +122,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
self.thumbnailView.image = [self thumbnailForMediaFile:anyFileFromEpisode];
self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
}
}
......@@ -240,42 +238,4 @@
self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
}
// Can be extracted outside of VLCPlaylistGridView
- (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
......@@ -10,10 +10,9 @@
#import "VLCPlaylistTableViewCell.h"
#import "VLCLinearProgressIndicator.h"
#import "VLCThumbnailsCache.h"
#import <MediaLibraryKit/MLAlbum.h>
#define MAX_CACHE_SIZE 21 // three times the number of items shown on iPhone 5
@implementation VLCPlaylistTableViewCell
+ (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
......@@ -88,7 +87,7 @@
[self configureForMLFile:mediaObject];
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]]) {
......@@ -105,7 +104,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
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]]) {
......@@ -114,7 +113,7 @@
if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
self.thumbnailView.image = [self thumbnailForMediaFile:anyFileFromEpisode];
self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
}
}
......@@ -219,44 +218,4 @@
self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
}
#pragma mark - thumbnails cache
// Can be extracted outside of VLCPlaylistTableViewCell
- (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
//
// VLCThumbnailsCache.h
// VLC for iOS
//
// Created by Gleb on 9/13/13.
// Copyright (c) 2013 VideoLAN. All rights reserved.
//
// Refer to the COPYING file of the official project for license.
//
#import <Foundation/Foundation.h>
@interface VLCThumbnailsCache : NSObject
+ (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile;
@end
//
// VLCThumbnailsCache.m
// VLC for iOS
//
// Created by Gleb on 9/13/13.
// Copyright (c) 2013 VideoLAN. All rights reserved.
//
// Refer to the COPYING file of the official project for license.
//
#import "VLCThumbnailsCache.h"
static NSInteger MaxCacheSize;
static NSMutableArray *_thumbnailCacheIndex = nil;
static NSMutableDictionary *_thumbnailCache = nil;
@implementation VLCThumbnailsCache
#define MAX_CACHE_SIZE_IPHONE 21 // three times the number of items shown on iPhone 5
#define MAX_CACHE_SIZE_IPAD 27 // three times the number of items shown on iPad
+(void)initialize
{
MaxCacheSize = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?
MAX_CACHE_SIZE_IPAD: MAX_CACHE_SIZE_IPHONE;
// TODO Consider to use NSCache
_thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MaxCacheSize];
_thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MaxCacheSize];
}
+ (UIImage *)thumbnailForMediaFile:(MLFile *)mediaFile
{
if (mediaFile == nil || mediaFile.objectID == nil)
return nil;
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 >= MaxCacheSize) {
[_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
[_thumbnailCacheIndex removeLastObject];
}
displayedImage = mediaFile.computedThumbnail;
if (displayedImage) {
[_thumbnailCache setObject:displayedImage forKey:objID];
[_thumbnailCacheIndex insertObject:objID atIndex:0];
}
}
return displayedImage;
}
@end
......@@ -280,6 +280,7 @@
A7A0E9F9174BA66000162F25 /* papasscode_marker.png in Resources */ = {isa = PBXBuildFile; fileRef = A7A0E9F1174BA66000162F25 /* papasscode_marker.png */; };
A7A0E9FA174BA66000162F25 /* papasscode_marker@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = A7A0E9F2174BA66000162F25 /* papasscode_marker@2x.png */; };
A7A0E9FB174BA66000162F25 /* PAPasscodeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A7A0E9F4174BA66000162F25 /* PAPasscodeViewController.m */; };
A7B5315F17E35B6E00EAE4B3 /* VLCThumbnailsCache.m in Sources */ = {isa = PBXBuildFile; fileRef = A7B5315E17E35B6E00EAE4B3 /* VLCThumbnailsCache.m */; };
A7C3025E175A53D400AD4388 /* NSString+SupportedMedia.m in Sources */ = {isa = PBXBuildFile; fileRef = A7C3025D175A53D400AD4388 /* NSString+SupportedMedia.m */; };
A7CB0DB11716F72600050CF3 /* PlayingExternally@2x~iphone.png in Resources */ = {isa = PBXBuildFile; fileRef = A7CB0DAD1716F72600050CF3 /* PlayingExternally@2x~iphone.png */; };
A7CB0DB21716F72600050CF3 /* PlayingExternally~iphone.png in Resources */ = {isa = PBXBuildFile; fileRef = A7CB0DAE1716F72600050CF3 /* PlayingExternally~iphone.png */; };
......@@ -771,6 +772,8 @@
A7A0E9F2174BA66000162F25 /* papasscode_marker@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "papasscode_marker@2x.png"; sourceTree = "<group>"; };
A7A0E9F3174BA66000162F25 /* PAPasscodeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PAPasscodeViewController.h; sourceTree = "<group>"; };
A7A0E9F4174BA66000162F25 /* PAPasscodeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PAPasscodeViewController.m; sourceTree = "<group>"; };
A7B5315D17E35B6E00EAE4B3 /* VLCThumbnailsCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VLCThumbnailsCache.h; sourceTree = "<group>"; };
A7B5315E17E35B6E00EAE4B3 /* VLCThumbnailsCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VLCThumbnailsCache.m; sourceTree = "<group>"; };
A7C3025C175A53D400AD4388 /* NSString+SupportedMedia.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+SupportedMedia.h"; sourceTree = "<group>"; };
A7C3025D175A53D400AD4388 /* NSString+SupportedMedia.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+SupportedMedia.m"; sourceTree = "<group>"; };
A7CB0DAD1716F72600050CF3 /* PlayingExternally@2x~iphone.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "PlayingExternally@2x~iphone.png"; sourceTree = "<group>"; };
......@@ -1231,6 +1234,8 @@
7DA6209A170A0CE500643D11 /* VLCPlaylistTableViewCell.m */,
7D6B07F51716D45B003280C4 /* VLCPlaylistGridView.h */,
7D6B07F61716D45B003280C4 /* VLCPlaylistGridView.m */,
A7B5315D17E35B6E00EAE4B3 /* VLCThumbnailsCache.h */,
A7B5315E17E35B6E00EAE4B3 /* VLCThumbnailsCache.m */,
);
name = "Everything Playlist";
sourceTree = "<group>";
......@@ -1989,6 +1994,7 @@
7D02B0D217B6BE850099030D /* VLCLocalServerFolderListViewController.m in Sources */,
7DC72D6317B7ED24008A26D0 /* WhiteRaccoon.m in Sources */,
7DC72D6917B820C9008A26D0 /* VLCNetworkLoginViewController.m in Sources */,
A7B5315F17E35B6E00EAE4B3 /* VLCThumbnailsCache.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
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