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

Revision 599, 7.2 KB checked in by gbooker, 7 years ago (diff)
  • Moved some files around
  • Added sorting ability
  • Included new display setting menu to change sort/predicates
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        [sub insertDictionary:dict epIndex:0];
50        int secondEp = [[dict objectForKey:META_EPISODE_2_NUMBER_KEY] intValue];
51        if(secondEp != 0)
52        {
53                SapphireSubEpisode *sub = [SapphireSubEpisode createSubEpisode:secondEp inEpisode:self];
54                [sub insertDictionary:dict epIndex:1];
55                [self addSubEpisodesObject:sub];
56        }
57        else
58                [self.subEpisodesSet setSet:[NSSet setWithObject:sub]];
59}
60
61+ (SapphireEpisode *)episodeWithDictionary:(NSDictionary *)dict inContext:(NSManagedObjectContext *)moc
62{
63        NSString *show = [dict objectForKey:META_SHOW_NAME_KEY];
64        int season = [[dict objectForKey:META_SEASON_NUMBER_KEY] intValue];
65        int ep = [[dict objectForKey:META_EPISODE_NUMBER_KEY] intValue];
66       
67        if(show == nil || season == 0)
68                return nil;
69       
70        NSString *showPath = [dict objectForKey:META_SHOW_IDENTIFIER_KEY];
71        SapphireEpisode *ret = [SapphireEpisode episode:ep inSeason:season forShow:show withPath:showPath inContext:moc];
72        [ret insertDictionary:dict];
73        return ret;
74}
75
76- (NSNumber *)episodeNumber
77{
78        NSNumber *ret = self.xml.episodeNumber;
79        if(ret != nil)
80                return ret;
81        [self createSortedEpisodes];
82        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).episodeNumber;
83}
84
85- (int)episodeNumberValue
86{
87        return [[self episodeNumber] intValue];
88}
89
90- (int)lastEpisodeNumberValue
91{
92        int ret = self.xml.episodeNumberValue;
93        if(ret != 0)
94                return ret;
95        [self createSortedEpisodes];
96        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:sortedSubEpisodes.count - 1]).episodeNumberValue;
97}
98
99- (int)absoluteEpisodeNumberValue
100{
101        int ret = self.xml.absoluteEpisodeNumberValue;
102        if(ret != 0)
103                return ret;
104        [self createSortedEpisodes];
105        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).absoluteEpisodeNumberValue;
106}
107
108- (NSString *)episodeTitle
109{
110        NSString *ret = self.xml.xml.title;
111        if(ret != nil)
112                return ret;
113        [self createSortedEpisodes];
114        NSMutableArray *subs = [[sortedSubEpisodes valueForKey:@"episodeTitle"] mutableCopy];
115        [subs removeObject:[NSNull null]];
116        ret = [subs componentsJoinedByString:@" / "];
117        [subs release];
118        return ret;
119}
120
121- (NSString *)episodeDescription
122{
123        NSString *ret = self.xml.xml.contentDescription;
124        if(ret != nil)
125                return ret;
126        [self createSortedEpisodes];
127        NSMutableArray *subs = [[sortedSubEpisodes valueForKey:@"episodeDescription"] mutableCopy];
128        [subs removeObject:[NSNull null]];
129        ret = [subs componentsJoinedByString:@" / "];
130        [subs release];
131        return ret;
132}
133
134- (NSComparisonResult)compare:(SapphireEpisode *)other
135{
136        NSComparisonResult result = [self.season compare:other.season];
137        if(result != NSOrderedSame)
138                return result;
139       
140        return [[self episodeNumber] compare:[other episodeNumber]];
141}
142
143- (NSDate *)airDate
144{
145        [self createSortedEpisodes];
146        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).airDate;
147}
148
149- (NSComparisonResult)airDateCompare:(SapphireEpisode *)other
150{
151        return [[self airDate] compare:[other airDate]];
152}
153
154- (void)insertDisplayMetaData:(NSMutableDictionary *)dict
155{
156        id value = [self episodeTitle];
157        if(value != nil)
158                [dict setObject:value forKey:META_TITLE_KEY];
159        value = [self episodeDescription];
160        if(value != nil)
161                [dict setObject:value forKey:META_DESCRIPTION_KEY];
162        value = [[sortedSubEpisodes objectAtIndex:0] airDate];
163        if(value != nil)
164                [dict setObject:value forKey:META_SHOW_AIR_DATE];
165        int season = [[self season] seasonNumberValue];
166        int ep = [self episodeNumberValue];
167        if(season != 0)
168                [dict setObject:[[self season] seasonNumber] forKey:BRLocalizedString(META_SEASON_NUMBER_KEY, @"Season in metadata display")];
169        if(ep != 0)
170                [dict setObject:[NSNumber numberWithInt:ep] forKey:BRLocalizedString(META_EPISODE_NUMBER_KEY, @"Epsiode in metadata display")];
171        if(ep != 0 && season != 0)
172        {
173                int lastEp = [self lastEpisodeNumberValue];
174                NSString *key = BRLocalizedString(META_EPISODE_AND_SEASON_KEY, @"Season / Epsiode in metadata display");
175                if(lastEp != nil && lastEp != ep)
176                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d-%d", [[self tvShow] name], season, ep, lastEp] forKey:key];
177                else
178                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d", [[self tvShow] name], season, lastEp] forKey:key];         
179        }
180}
181
182- (NSString *)path
183{
184        [self createSortedEpisodes];
185        return [[sortedSubEpisodes objectAtIndex:0] path];
186}
187
188- (NSString *)coverArtPath
189{
190        NSString *ret = [[SapphireMetaDataSupport collectionArtPath] stringByAppendingPathComponent:[self path]];
191        NSString *file = searchCoverArtExtForPath(ret);
192        if(file == nil)
193                return [self.season coverArtPath];
194        return ret;
195}
196
197- (NSNumber *)watched
198{
199        NSSet *files = self.filesSet;
200        if([files count] < 2)
201                return [[files anyObject] watched];
202       
203        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController unwatchedPredicate]];
204        if([remain count])
205                return [NSNumber numberWithBool:YES];
206        return [NSNumber numberWithBool:NO];
207}
208
209- (NSNumber *)favorite
210{
211        NSSet *files = self.filesSet;
212        if([files count] < 2)
213                return [[files anyObject] favorite];
214       
215        NSArray *remain = [[files allObjects] filteredArrayUsingPredicate:[SapphireApplianceController favoritePredicate]];
216        if([remain count])
217                return [NSNumber numberWithBool:YES];
218        return [NSNumber numberWithBool:NO];
219}
220
221/*Overrides*/
222- (void)addSubEpisodes:(NSSet*)value_
223{
224        [sortedSubEpisodes release];
225        sortedSubEpisodes = nil;
226        [super addSubEpisodes:value_];
227}
228
229- (void)removeSubEpisodes:(NSSet*)value_
230{
231        [sortedSubEpisodes release];
232        sortedSubEpisodes = nil;
233        [super removeSubEpisodes:value_];
234}
235
236- (void)addSubEpisodesObject:(SapphireSubEpisode*)value_
237{
238        [sortedSubEpisodes release];
239        sortedSubEpisodes = nil;
240        [super addSubEpisodesObject:value_];
241}
242
243- (void)removeSubEpisodesObject:(SapphireSubEpisode*)value_
244{
245        [sortedSubEpisodes release];
246        sortedSubEpisodes = nil;
247        [super removeSubEpisodesObject:value_]; 
248}
249
250- (void)clearPredicateCache
251{
252        [self.season clearPredicateCache];
253}
254
255@end
Note: See TracBrowser for help on using the repository browser.