| 1 | #import "SapphireSeason.h" |
|---|
| 2 | #import "SapphireTVShow.h" |
|---|
| 3 | #import "SapphireFileSorter.h" |
|---|
| 4 | |
|---|
| 5 | @implementation SapphireSeason |
|---|
| 6 | |
|---|
| 7 | static NSArray *allowedSorts = nil; |
|---|
| 8 | |
|---|
| 9 | + (void)load |
|---|
| 10 | { |
|---|
| 11 | allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil]; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | + (SapphireSeason *)season:(int)season forShow:(NSString *)show withPath:(NSString *)showPath inContext:(NSManagedObjectContext *)moc |
|---|
| 15 | { |
|---|
| 16 | SapphireTVShow *tvshow = [SapphireTVShow show:show withPath:showPath inContext:moc]; |
|---|
| 17 | NSPredicate *seasonPred = [NSPredicate predicateWithFormat:@"seasonNumber = %d", season]; |
|---|
| 18 | NSArray *results = [[tvshow.seasonsSet allObjects] filteredArrayUsingPredicate:seasonPred]; |
|---|
| 19 | if([results count]) |
|---|
| 20 | return [results objectAtIndex:0]; |
|---|
| 21 | |
|---|
| 22 | SapphireSeason *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireSeasonName inManagedObjectContext:moc]; |
|---|
| 23 | ret.tvShow = tvshow; |
|---|
| 24 | ret.seasonNumber = [NSNumber numberWithInt:season]; |
|---|
| 25 | return ret; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | + (SapphireSeason *)upgradeV1Season:(NSManagedObject *)oldSeason toShow:(SapphireTVShow *)show |
|---|
| 29 | { |
|---|
| 30 | NSManagedObjectContext *newMoc = [show managedObjectContext]; |
|---|
| 31 | |
|---|
| 32 | SapphireSeason *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireSeasonName inManagedObjectContext:newMoc]; |
|---|
| 33 | ret.seasonDescription = [oldSeason valueForKey:@"seasonDescription"]; |
|---|
| 34 | ret.seasonNumber = [oldSeason valueForKey:@"seasonNumber"]; |
|---|
| 35 | ret.tvShow = show; |
|---|
| 36 | return ret; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | - (NSComparisonResult)compare:(SapphireSeason *)other |
|---|
| 40 | { |
|---|
| 41 | NSComparisonResult result = [self.tvShow compare:other.tvShow]; |
|---|
| 42 | if(result != NSOrderedSame) |
|---|
| 43 | return result; |
|---|
| 44 | |
|---|
| 45 | int myNum = self.seasonNumberValue; |
|---|
| 46 | int theirNum = other.seasonNumberValue; |
|---|
| 47 | if(myNum == 0) |
|---|
| 48 | myNum = INT_MAX; |
|---|
| 49 | if(theirNum == 0) |
|---|
| 50 | theirNum = INT_MAX; |
|---|
| 51 | if(myNum > theirNum) |
|---|
| 52 | return NSOrderedDescending; |
|---|
| 53 | if(theirNum > myNum) |
|---|
| 54 | return NSOrderedAscending; |
|---|
| 55 | return NSOrderedSame; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | - (NSString *)seasonName |
|---|
| 59 | { |
|---|
| 60 | return [NSString stringWithFormat:@"Season %d", self.seasonNumberValue]; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | - (NSPredicate *)metaFileFetchPredicate |
|---|
| 64 | { |
|---|
| 65 | return [NSPredicate predicateWithFormat:@"tvEpisode.season == %@", self]; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | - (id <SapphireDirectory>)parentDirectory |
|---|
| 69 | { |
|---|
| 70 | return self.tvShow; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | - (NSArray *)fileSorters |
|---|
| 74 | { |
|---|
| 75 | return allowedSorts; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | - (NSString *)path |
|---|
| 79 | { |
|---|
| 80 | NSString *myName = [NSString stringWithFormat:@"Season %d", self.seasonNumberValue]; |
|---|
| 81 | return [self.tvShow.path stringByAppendingPathComponent:myName]; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | @end |
|---|