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