This repository has been archived by the owner on May 8, 2020. It is now read-only.
/
DCTImageCacheAttributes.m
92 lines (72 loc) · 2.69 KB
/
DCTImageCacheAttributes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// DCTImageCacheAttributes.m
// DCTImageCache
//
// Created by Daniel Tull on 09.12.2012.
// Copyright (c) 2012 Daniel Tull. All rights reserved.
//
#import "_DCTImageCacheAttributes.h"
NSString *const DCTImageCacheAttributesKey = @"DCTImageCacheAttributesKey";
NSString *const DCTImageCacheAttributesSize = @"DCTImageCacheAttributesSize";
NSString *const DCTImageCacheAttributesCreatedBefore = @"DCTImageCacheAttributesCreatedBefore";
CGSize const DCTImageCacheAttributesNullSize = {-CGFLOAT_MAX, -CGFLOAT_MAX};
@implementation DCTImageCacheAttributes
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [self init];
if (!self) return nil;
_dictionary = [dictionary copy];
return self;
}
- (NSString *)identifier {
return [self.dictionary description];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p; key = %@; size = %@; createdBefore = %@>",
NSStringFromClass([self class]),
self,
self.key,
[self _sizeString],
self.createdBefore];
}
- (NSFetchRequest *)_fetchRequest {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:[_DCTImageCacheItem entityName]];
NSMutableArray *predicates = [[NSMutableArray alloc] initWithCapacity:3];
NSString *key = self.key;
if (key.length > 0) {
NSPredicate *keyPredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.key, key];
[predicates addObject:keyPredicate];
}
NSString *sizeString = [self _sizeString];
if (sizeString.length > 0) {
NSPredicate *sizePredicate = [NSPredicate predicateWithFormat:@"%K == %@", _DCTImageCacheItemAttributes.sizeString, sizeString];
[predicates addObject:sizePredicate];
}
NSDate *createdBefore = self.createdBefore;
if (createdBefore) {
NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"%K < %@", _DCTImageCacheItemAttributes.date, createdBefore];
[predicates addObject:datePredicate];
}
fetchRequest.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
return fetchRequest;
}
- (void)_setupCacheItemProperties:(_DCTImageCacheItem *)cacheItem {
cacheItem.key = self.key;
cacheItem.sizeString = [self _sizeString];
}
- (NSString *)_sizeString {
NSValue *value = [self.dictionary objectForKey:DCTImageCacheAttributesSize];
if (!value) return @"";
return NSStringFromCGSize([value CGSizeValue]);
}
- (CGSize)size {
NSValue *value = [self.dictionary objectForKey:DCTImageCacheAttributesSize];
if (!value) return DCTImageCacheAttributesNullSize;
return [value CGSizeValue];
}
- (NSString *)key {
return [self.dictionary objectForKey:DCTImageCacheAttributesKey];
}
- (NSDate *)createdBefore {
return [self.dictionary objectForKey:DCTImageCacheAttributesCreatedBefore];
}
@end