| 1 | #import "SapphireSubEpisode.h" |
|---|
| 2 | #import "SapphireEpisode.h" |
|---|
| 3 | #import "SapphireSeason.h" |
|---|
| 4 | #import "SapphireFileMetaData.h" |
|---|
| 5 | |
|---|
| 6 | @implementation SapphireSubEpisode |
|---|
| 7 | |
|---|
| 8 | + (SapphireSubEpisode *)subEpisode:(int)subNum inEpisode:(SapphireEpisode *)ep; |
|---|
| 9 | { |
|---|
| 10 | NSEnumerator *subEpEnum = [ep.subEpisodesSet objectEnumerator]; |
|---|
| 11 | SapphireSubEpisode *subep; |
|---|
| 12 | while((subep = [subEpEnum nextObject]) != nil) |
|---|
| 13 | { |
|---|
| 14 | if([subep.episodeNumber intValue] == subNum) |
|---|
| 15 | return subep; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | return nil; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | + (SapphireSubEpisode *)createSubEpisode:(int)subNum inEpisode:(SapphireEpisode *)ep |
|---|
| 22 | { |
|---|
| 23 | SapphireSubEpisode *ret = [SapphireSubEpisode subEpisode:subNum inEpisode:ep]; |
|---|
| 24 | if(ret != nil) |
|---|
| 25 | return ret; |
|---|
| 26 | |
|---|
| 27 | ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireSubEpisodeName inManagedObjectContext:[ep managedObjectContext]]; |
|---|
| 28 | ret.episode = ep; |
|---|
| 29 | ret.episodeNumber = [NSNumber numberWithInt:subNum]; |
|---|
| 30 | return ret; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | - (void)insertDictionary:(NSDictionary *)dict epIndex:(int)index |
|---|
| 34 | { |
|---|
| 35 | NSString *desc = [dict objectForKey:META_DESCRIPTION_KEY]; |
|---|
| 36 | int descSplitIndex = [desc rangeOfString:@" / "].location; |
|---|
| 37 | if(descSplitIndex == NSNotFound) |
|---|
| 38 | descSplitIndex = [desc length]; |
|---|
| 39 | |
|---|
| 40 | NSString *title = [dict objectForKey:META_TITLE_KEY]; |
|---|
| 41 | int titleSplitIndex = [title rangeOfString:@" / "].location; |
|---|
| 42 | if(titleSplitIndex == NSNotFound) |
|---|
| 43 | titleSplitIndex = [title length]; |
|---|
| 44 | |
|---|
| 45 | if(index == -1) |
|---|
| 46 | { |
|---|
| 47 | self.absoluteEpisodeNumber = [dict objectForKey:META_ABSOLUTE_EP_NUMBER_KEY]; |
|---|
| 48 | self.episodeDescription = desc; |
|---|
| 49 | self.episodeTitle = title; |
|---|
| 50 | } |
|---|
| 51 | else if(index == 0) |
|---|
| 52 | { |
|---|
| 53 | self.absoluteEpisodeNumber = [dict objectForKey:META_ABSOLUTE_EP_NUMBER_KEY]; |
|---|
| 54 | self.episodeDescription = [desc substringToIndex:descSplitIndex]; |
|---|
| 55 | self.episodeTitle = [title substringToIndex:titleSplitIndex]; |
|---|
| 56 | } |
|---|
| 57 | else |
|---|
| 58 | { |
|---|
| 59 | self.absoluteEpisodeNumber = [dict objectForKey:META_ABSOLUTE_EP_2_NUMBER_KEY]; |
|---|
| 60 | self.episodeDescription = [desc substringFromIndex:descSplitIndex]; |
|---|
| 61 | self.episodeTitle = [title substringFromIndex:titleSplitIndex]; |
|---|
| 62 | } |
|---|
| 63 | self.airDate = [dict objectForKey:META_SHOW_AIR_DATE]; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | - (NSComparisonResult)compare:(SapphireSubEpisode *)other |
|---|
| 67 | { |
|---|
| 68 | NSComparisonResult result = [self.episode.season compare:other.episode.season]; |
|---|
| 69 | if(result != NSOrderedSame) |
|---|
| 70 | return result; |
|---|
| 71 | |
|---|
| 72 | int myNum = self.episodeNumberValue; |
|---|
| 73 | int theirNum = other.episodeNumberValue; |
|---|
| 74 | if(myNum == 0 || theirNum == 0) |
|---|
| 75 | { |
|---|
| 76 | NSDate *myDate = self.airDate; |
|---|
| 77 | NSDate *theirDate = other.airDate; |
|---|
| 78 | return [myDate compare:theirDate]; |
|---|
| 79 | } |
|---|
| 80 | if(myNum > theirNum) |
|---|
| 81 | return NSOrderedDescending; |
|---|
| 82 | if(theirNum > myNum) |
|---|
| 83 | return NSOrderedAscending; |
|---|
| 84 | |
|---|
| 85 | return NSOrderedSame; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | - (NSString *)path |
|---|
| 89 | { |
|---|
| 90 | NSString *myName = self.episodeTitle; |
|---|
| 91 | if(self.episodeNumberValue != 0) |
|---|
| 92 | myName = [NSString stringWithFormat:@"Episode %d", self.episodeNumberValue]; |
|---|
| 93 | return [self.episode.season.path stringByAppendingPathComponent:myName]; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | @end |
|---|