source: trunk/SapphireFrappliance/MetaData/SapphireMObjects/SapphireTVShow.m @ 849

Revision 849, 4.6 KB checked in by gbooker, 6 years ago (diff)

Added back the cover art for the virtual directories that has been long lost

Line 
1#import "SapphireTVShow.h"
2#import "SapphireSeason.h"
3#import "SapphireEpisode.h"
4#import "SapphireSubEpisode.h"
5#import "CoreDataSupportFunctions.h"
6#import "SapphireMetaDataSupport.h"
7#import "NSString-Extensions.h"
8#import "SapphireFileSorter.h"
9#import "SapphireTVTranslation.h"
10
11@implementation SapphireTVShow
12
13static NSArray *allowedSorts = nil;
14
15+ (void)load
16{
17        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil];
18}
19
20+ (SapphireTVShow *)show:(NSString *)show withPath:(NSString *)showPath inContext:(NSManagedObjectContext *)moc
21{
22        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", show];
23        SapphireTVShow *ret = (SapphireTVShow *)doSingleFetchRequest(SapphireTVShowName, moc, predicate);
24        if(ret != nil)
25        {
26                if(ret.showPath == nil)
27                        ret.showPath = showPath;
28                return ret;
29        }
30       
31        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:moc];
32        ret.name = show;
33        ret.showPath = showPath;
34        return ret;
35}
36
37+ (SapphireTVShow *)showWithPath:(NSString *)path inContext:(NSManagedObjectContext *)moc
38{
39        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"showPath == %@", path];
40        return (SapphireTVShow *)doSingleFetchRequest(SapphireTVShowName, moc, predicate);
41}
42
43+ (void)upgradeV1ShowsFromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
44{
45        NSArray *oldShows = doFetchRequest(SapphireTVShowName, oldMoc, nil);
46        NSEnumerator *showEnum = [oldShows objectEnumerator];
47        NSManagedObject *oldShow;
48        while((oldShow = [showEnum nextObject]) != nil)
49        {
50                SapphireTVShow *newShow = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:newMoc];
51                newShow.name = [oldShow valueForKey:@"name"];
52                newShow.showDescription = [oldShow valueForKey:@"showDescription"];
53                newShow.showID = [oldShow valueForKey:@"showID"];
54                newShow.showPath = [oldShow valueForKey:@"showPath"];
55               
56                NSEnumerator *translationEnum = [[oldShow valueForKey:@"translations"] objectEnumerator];
57                NSManagedObject *translation;
58                while((translation = [translationEnum nextObject]) != nil)
59                {
60                        [SapphireTVTranslation upgradeV1TVTranslation:translation toShow:newShow];
61                }
62               
63                NSEnumerator *seasonEnum = [[oldShow valueForKey:@"seasons"] objectEnumerator];
64                NSManagedObject *season;
65                while((season = [seasonEnum nextObject]) != nil)
66                {
67                        [SapphireSeason upgradeV1Season:season toShow:newShow];
68                }
69        }
70}
71
72- (NSComparisonResult)compare:(SapphireTVShow *)other
73{
74        return [self.name nameCompare:other.name];
75}
76
77- (NSString *)dirNameValue
78{
79        return @"seasonName";
80}
81
82- (NSPredicate *)metaFileFetchPredicate
83{
84        return [NSPredicate predicateWithFormat:@"tvEpisode.tvShow == %@", self];
85}
86
87- (NSArray *)fileSorters
88{
89        return allowedSorts;
90}
91
92- (void)defaultDirectorySort:(NSMutableArray *)dirs
93{
94        [dirs sortUsingSelector:@selector(compare:)];
95}
96
97- (NSString *)path
98{
99        return [NSString stringWithFormat:@"@TV/%@", self.name];
100}
101
102- (NSString *)classDefaultCoverPath
103{
104        return [[NSBundle bundleForClass:[self class]] pathForResource:@"TV" ofType:@"png"];
105}
106
107static inline NSArray *getEpsFromFiles(NSManagedObjectContext *moc, NSArray *files)
108{
109        NSSet *epIds = [NSSet setWithArray:[files valueForKeyPath:@"tvEpisode.objectID"]];
110       
111        NSPredicate *epPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", epIds];
112        return doFetchRequest(SapphireEpisodeName, moc, epPredicate);
113}
114
115- (NSMutableArray *)internalDirectoryFetchFromFiles:(NSArray *)files
116{
117        NSManagedObjectContext *moc = [self managedObjectContext];
118        NSArray *eps = getEpsFromFiles(moc, files);
119       
120        NSSet *seasonIds = [NSSet setWithArray:[eps valueForKeyPath:@"season.objectID"]];
121        NSPredicate *seasonPred = [NSPredicate predicateWithFormat:@"SELF IN %@", seasonIds];
122        NSMutableArray *ret = [doFetchRequest(SapphireSeasonName, moc, seasonPred) mutableCopy];
123        return [ret autorelease];
124}
125
126- (void)prefetch:(NSArray *)files
127{
128        NSManagedObjectContext *moc = [self managedObjectContext];
129        NSArray *eps = getEpsFromFiles(moc, files);
130       
131        NSPredicate *subEpsPred = [NSPredicate predicateWithFormat:@"episode IN %@", eps];
132        doFetchRequest(SapphireSubEpisodeName, moc, subEpsPred);
133}
134
135- (BOOL)shouldDelete
136{
137        return [self.episodesSet count] == 0;
138}
139
140-(void)removeEpisodes:(NSSet*)removedEpisodes
141{
142        [super removeEpisodes:removedEpisodes];
143        if([self.episodesSet count] == 0)
144                [SapphireMetaDataSupport setObjectForPendingDelete:self];
145}
146
147- (void)removeEpisodesObject:(SapphireEpisode*)removedEpisode
148{
149        [super removeEpisodesObject:removedEpisode];
150        if([self.episodesSet count] == 0)
151                [SapphireMetaDataSupport setObjectForPendingDelete:self];
152}
153
154@end
Note: See TracBrowser for help on using the repository browser.