source: branches/CoreData/SapphireFrappliance/MetaData/SapphireMObjects/SapphireEpisode.m @ 627

Revision 627, 7.5 KB checked in by gbooker, 6 years ago (diff)

Made an easier way to import sub episodes from the importer and forced a less hacky way to import from the old plist

Line 
1#import "SapphireEpisode.h"
2#import "SapphireSeason.h"
3#import "SapphireSubEpisode.h"
4#import "SapphireMediaPreview.h"
5#import "SapphireFileMetaData.h"
6#import "SapphireMetaDataSupport.h"
7#import "SapphireXMLEpisodeData.h"
8#import "SapphireXMLData.h"
9#import "SapphireApplianceController.h"
10
11@implementation SapphireEpisode
12
13+ (SapphireEpisode *)episode:(int)ep inSeason:(int)season forShow:(NSString *)show withPath:(NSString *)showPath inContext:(NSManagedObjectContext *)moc
14{
15        SapphireSeason *tvseason = [SapphireSeason season:season forShow:show withPath:showPath inContext:moc];
16        NSEnumerator *epEnum = [tvseason.episodesSet objectEnumerator];
17        SapphireEpisode *tvep;
18        while((tvep = [epEnum nextObject]) != nil)
19        {
20                if([SapphireSubEpisode subEpisode:ep inEpisode:tvep] != nil)
21                        return tvep;
22        }
23       
24        SapphireEpisode *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireEpisodeName inManagedObjectContext:moc];
25        ret.season = tvseason;
26        ret.tvShow = tvseason.tvShow;
27        [ret addSubEpisodesObject:[SapphireSubEpisode createSubEpisode:ep inEpisode:ret]];
28        return ret;
29}
30
31- (void) dealloc
32{
33        [sortedSubEpisodes release];
34        [super dealloc];
35}
36
37- (void)createSortedEpisodes
38{
39        if(sortedSubEpisodes != nil)
40                return;
41       
42        NSArray *unsorted = [self.subEpisodesSet allObjects];
43        sortedSubEpisodes = [[unsorted sortedArrayUsingSelector:@selector(compare:)] retain];
44}
45
46- (void)insertDictionary:(NSDictionary *)dict
47{
48        SapphireSubEpisode *sub = [self.subEpisodesSet anyObject];
49        int secondEp = [[dict objectForKey:META_EPISODE_2_NUMBER_KEY] intValue];
50        int setIndex = -1;
51        if(secondEp != 0)
52                setIndex = 0;
53        [sub insertDictionary:dict epIndex:setIndex];
54        if(secondEp != 0)
55        {
56                SapphireSubEpisode *sub = [SapphireSubEpisode createSubEpisode:secondEp inEpisode:self];
57                [sub insertDictionary:dict epIndex:1];
58                [self addSubEpisodesObject:sub];
59        }
60        else
61                [self.subEpisodesSet setSet:[NSSet setWithObject:sub]];
62}
63
64+ (SapphireEpisode *)episodeWithDictionary:(NSDictionary *)dict inContext:(NSManagedObjectContext *)moc
65{
66        NSString *show = [dict objectForKey:META_SHOW_NAME_KEY];
67        int season = [[dict objectForKey:META_SEASON_NUMBER_KEY] intValue];
68        int ep = [[dict objectForKey:META_EPISODE_NUMBER_KEY] intValue];
69       
70        if(show == nil || season == 0)
71                return nil;
72       
73        NSString *showPath = [dict objectForKey:META_SHOW_IDENTIFIER_KEY];
74        SapphireEpisode *ret = [SapphireEpisode episode:ep inSeason:season forShow:show withPath:showPath inContext:moc];
75        [ret insertDictionary:dict];
76        return ret;
77}
78
79- (NSNumber *)episodeNumber
80{
81        NSNumber *ret = self.xml.episodeNumber;
82        if(ret != nil)
83                return ret;
84        [self createSortedEpisodes];
85        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).episodeNumber;
86}
87
88- (int)episodeNumberValue
89{
90        return [[self episodeNumber] intValue];
91}
92
93- (int)lastEpisodeNumberValue
94{
95        int ret = self.xml.episodeNumberValue;
96        if(ret != 0)
97                return ret;
98        [self createSortedEpisodes];
99        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:sortedSubEpisodes.count - 1]).episodeNumberValue;
100}
101
102- (int)absoluteEpisodeNumberValue
103{
104        int ret = self.xml.absoluteEpisodeNumberValue;
105        if(ret != 0)
106                return ret;
107        [self createSortedEpisodes];
108        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).absoluteEpisodeNumberValue;
109}
110
111- (NSString *)episodeTitle
112{
113        NSString *ret = self.xml.xml.title;
114        if(ret != nil)
115                return ret;
116        [self createSortedEpisodes];
117        NSMutableArray *subs = [[sortedSubEpisodes valueForKey:@"episodeTitle"] mutableCopy];
118        [subs removeObject:[NSNull null]];
119        ret = [subs componentsJoinedByString:@" / "];
120        [subs release];
121        return ret;
122}
123
124- (NSString *)episodeDescription
125{
126        NSString *ret = self.xml.xml.contentDescription;
127        if(ret != nil)
128                return ret;
129        [self createSortedEpisodes];
130        NSMutableArray *subs = [[sortedSubEpisodes valueForKey:@"episodeDescription"] mutableCopy];
131        [subs removeObject:[NSNull null]];
132        ret = [subs componentsJoinedByString:@" / "];
133        [subs release];
134        return ret;
135}
136
137- (NSComparisonResult)compare:(SapphireEpisode *)other
138{
139        NSComparisonResult result = [self.season compare:other.season];
140        if(result != NSOrderedSame)
141                return result;
142       
143        return [[self episodeNumber] compare:[other episodeNumber]];
144}
145
146- (NSDate *)airDate
147{
148        [self createSortedEpisodes];
149        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).airDate;
150}
151
152- (NSComparisonResult)airDateCompare:(SapphireEpisode *)other
153{
154        return [[self airDate] compare:[other airDate]];
155}
156
157- (void)insertDisplayMetaData:(NSMutableDictionary *)dict
158{
159        id value = [self episodeTitle];
160        if(value != nil)
161                [dict setObject:value forKey:META_TITLE_KEY];
162        value = [self episodeDescription];
163        if(value != nil)
164                [dict setObject:value forKey:META_DESCRIPTION_KEY];
165        value = [[sortedSubEpisodes objectAtIndex:0] airDate];
166        if(value != nil)
167                [dict setObject:value forKey:META_SHOW_AIR_DATE];
168        int season = [[self season] seasonNumberValue];
169        int ep = [self episodeNumberValue];
170        if(season != 0)
171                [dict setObject:[[self season] seasonNumber] forKey:BRLocalizedString(META_SEASON_NUMBER_KEY, @"Season in metadata display")];
172        if(ep != 0)
173                [dict setObject:[NSNumber numberWithInt:ep] forKey:BRLocalizedString(META_EPISODE_NUMBER_KEY, @"Epsiode in metadata display")];
174        if(ep != 0 && season != 0)
175        {
176                int lastEp = [self lastEpisodeNumberValue];
177                NSString *key = BRLocalizedString(META_EPISODE_AND_SEASON_KEY, @"Season / Epsiode in metadata display");
178                if(lastEp != nil && lastEp != ep)
179                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d-%d", [[self tvShow] name], season, ep, lastEp] forKey:key];
180                else
181                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d", [[self tvShow] name], season, lastEp] forKey:key];         
182        }
183}
184
185- (void)insertAdditionalEpisode:(NSDictionary *)dict
186{
187        SapphireSubEpisode *sub = [SapphireSubEpisode createSubEpisode:[[dict objectForKey:META_EPISODE_NUMBER_KEY] intValue] inEpisode:self];
188        [sub insertDictionary:dict epIndex:-1];
189        [self addSubEpisodesObject:sub];
190}
191
192- (NSString *)path
193{
194        [self createSortedEpisodes];
195        return [[sortedSubEpisodes objectAtIndex:0] path];
196}
197
198- (NSString *)coverArtPath
199{
200        NSString *ret = [[SapphireMetaDataSupport collectionArtPath] stringByAppendingPathComponent:[self path]];
201        NSString *file = searchCoverArtExtForPath(ret);
202        if(file == nil)
203                return [self.season coverArtPath];
204        return ret;
205}
206
207- (NSNumber *)watched
208{
209        NSSet *files = self.filesSet;
210        if([files count] < 2)
211                return [[files anyObject] watched];
212       
213        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController unwatchedPredicate]];
214        if([remain count])
215                return [NSNumber numberWithBool:YES];
216        return [NSNumber numberWithBool:NO];
217}
218
219- (NSNumber *)favorite
220{
221        NSSet *files = self.filesSet;
222        if([files count] < 2)
223                return [[files anyObject] favorite];
224       
225        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController favoritePredicate]];
226        if([remain count])
227                return [NSNumber numberWithBool:YES];
228        return [NSNumber numberWithBool:NO];
229}
230
231/*Overrides*/
232- (void)addSubEpisodes:(NSSet*)value_
233{
234        [sortedSubEpisodes release];
235        sortedSubEpisodes = nil;
236        [super addSubEpisodes:value_];
237}
238
239- (void)removeSubEpisodes:(NSSet*)value_
240{
241        [sortedSubEpisodes release];
242        sortedSubEpisodes = nil;
243        [super removeSubEpisodes:value_];
244}
245
246- (void)addSubEpisodesObject:(SapphireSubEpisode*)value_
247{
248        [sortedSubEpisodes release];
249        sortedSubEpisodes = nil;
250        [super addSubEpisodesObject:value_];
251}
252
253- (void)removeSubEpisodesObject:(SapphireSubEpisode*)value_
254{
255        [sortedSubEpisodes release];
256        sortedSubEpisodes = nil;
257        [super removeSubEpisodesObject:value_]; 
258}
259
260- (void)clearPredicateCache
261{
262        [self.season clearPredicateCache];
263}
264
265@end
Note: See TracBrowser for help on using the repository browser.