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

Revision 544, 4.9 KB checked in by gbooker, 6 years ago (diff)

Now builds without warnings or errors

Line 
1#import "SapphireEpisode.h"
2#import "SapphireSeason.h"
3#import "SapphireSubEpisode.h"
4#import "SapphireFile.h"
5#import "SapphireMediaPreview.h"
6
7@implementation SapphireEpisode
8
9+ (SapphireEpisode *)episode:(int)ep inSeason:(int)season forShow:(NSString *)show withPath:(NSString *)showPath inContext:(NSManagedObjectContext *)moc
10{
11        SapphireSeason *tvseason = [SapphireSeason season:season forShow:show withPath:showPath inContext:moc];
12        NSEnumerator *epEnum = [tvseason.episodesSet objectEnumerator];
13        SapphireEpisode *tvep;
14        while((tvep = [epEnum nextObject]) != nil)
15        {
16                if([SapphireSubEpisode subEpisode:ep inEpisode:tvep] != nil)
17                        return tvep;
18        }
19       
20        SapphireEpisode *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireEpisodeName inManagedObjectContext:moc];
21        ret.season = tvseason;
22        ret.tvShow = tvseason.tvShow;
23        [ret addSubEpisodesObject:[SapphireSubEpisode createSubEpisode:ep inEpisode:ret]];
24        return ret;
25}
26
27- (void) dealloc
28{
29        [sortedSubEpisodes release];
30        [super dealloc];
31}
32
33- (void)createSortedEpisodes
34{
35        if(sortedSubEpisodes != nil)
36                return;
37       
38        NSArray *unsorted = [self.subEpisodesSet allObjects];
39        sortedSubEpisodes = [[unsorted sortedArrayUsingSelector:@selector(compare:)] retain];
40}
41
42- (void)insertDictionary:(NSDictionary *)dict
43{
44        [[self.subEpisodesSet anyObject] insertDictionary:dict epIndex:0];
45        int secondEp = [[dict objectForKey:@"Episode Number 2"] intValue];
46        if(secondEp != 0)
47        {
48                SapphireSubEpisode *sub = [SapphireSubEpisode createSubEpisode:secondEp inEpisode:self];
49                [sub insertDictionary:dict epIndex:1];
50                [self addSubEpisodesObject:sub];
51        }
52}
53
54+ (SapphireEpisode *)episodeWithDictionary:(NSDictionary *)dict inContext:(NSManagedObjectContext *)moc
55{
56        NSString *show = [dict objectForKey:@"Show Name"];
57        int season = [[dict objectForKey:@"Season"] intValue];
58        int ep = [[dict objectForKey:@"Episode"] intValue];
59       
60        if(show == nil || season == 0)
61                return nil;
62       
63        NSString *showPath = [dict objectForKey:@"Show ID"];
64        SapphireEpisode *ret = [SapphireEpisode episode:ep inSeason:season forShow:show withPath:showPath inContext:moc];
65        [ret insertDictionary:dict];
66        return ret;
67}
68
69- (int)episodeNumberValue
70{
71        [self createSortedEpisodes];
72        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:0]).episodeNumberValue;
73}
74
75- (int)lastEpisodeNumberValue
76{
77        [self createSortedEpisodes];
78        return ((SapphireSubEpisode *)[sortedSubEpisodes objectAtIndex:sortedSubEpisodes.count - 1]).episodeNumberValue;
79}
80
81- (NSString *)episodeTitle
82{
83        [self createSortedEpisodes];
84        return [[sortedSubEpisodes valueForKey:@"episodeTitle"] componentsJoinedByString:@" / "];
85}
86
87- (NSComparisonResult)compare:(SapphireEpisode *)other
88{
89        NSComparisonResult result = [self.season compare:other.season];
90        if(result != NSOrderedSame)
91                return result;
92       
93        [self createSortedEpisodes];
94        return [[sortedSubEpisodes objectAtIndex:0] compare:[other->sortedSubEpisodes objectAtIndex:0]];
95}
96
97- (NSString *)coverArtPath
98{
99        NSString *base = [[self season] coverArtPath];
100        if([self episodeNumberValue] > 0)
101                return [base stringByAppendingFormat:@"/Episode %d", [self episodeNumberValue]];
102        return [base stringByAppendingPathComponent:[self episodeTitle]];
103}
104
105- (void)insertDisplayMetaData:(NSMutableDictionary *)dict
106{
107        id value = [self episodeTitle];
108        if(value != nil)
109                [dict setObject:value forKey:META_TITLE_KEY];
110        value = [[sortedSubEpisodes objectAtIndex:0] airDate];
111        if(value != nil)
112                [dict setObject:value forKey:META_SHOW_AIR_DATE];
113        int season = [[self season] seasonNumberValue];
114        int ep = [self episodeNumberValue];
115        if(season != 0)
116                [dict setObject:[[self season] seasonNumber] forKey:BRLocalizedString(META_SEASON_NUMBER_KEY, @"Season in metadata display")];
117        if(ep != 0)
118                [dict setObject:[NSNumber numberWithInt:ep] forKey:BRLocalizedString(META_EPISODE_NUMBER_KEY, @"Epsiode in metadata display")];
119        if(ep != 0 && season != 0)
120        {
121                int lastEp = [self lastEpisodeNumberValue];
122                NSString *key = BRLocalizedString(META_EPISODE_AND_SEASON_KEY, @"Season / Epsiode in metadata display");
123                if(lastEp != nil)
124                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d-%d", [[self tvShow] name], season, ep, lastEp] forKey:key];
125                else
126                        [dict setObject:[NSString stringWithFormat:@"%@ - %d / %d", [[self tvShow] name], season, lastEp] forKey:key];         
127        }
128}
129
130- (NSString *)path
131{
132        [self createSortedEpisodes];
133        return [[sortedSubEpisodes objectAtIndex:0] path];
134}
135
136
137/*Overrides*/
138- (void)addSubEpisodes:(NSSet*)value_
139{
140        [sortedSubEpisodes release];
141        sortedSubEpisodes = nil;
142        [super addSubEpisodes:value_];
143}
144
145- (void)removeSubEpisodes:(NSSet*)value_
146{
147        [sortedSubEpisodes release];
148        sortedSubEpisodes = nil;
149        [super removeSubEpisodes:value_];
150}
151
152- (void)addSubEpisodesObject:(SapphireSubEpisode*)value_
153{
154        [sortedSubEpisodes release];
155        sortedSubEpisodes = nil;
156        [super addSubEpisodesObject:value_];
157}
158
159- (void)removeSubEpisodesObject:(SapphireSubEpisode*)value_
160{
161        [sortedSubEpisodes release];
162        sortedSubEpisodes = nil;
163        [super removeSubEpisodesObject:value_]; 
164}
165
166@end
Note: See TracBrowser for help on using the repository browser.