source: trunk/SapphireFrappliance/MetaData/SapphireMObjects/SapphireSeason.m @ 1291

Revision 1291, 3.2 KB checked in by gbooker, 4 years ago (diff)

Added sort by duration and sorting by filesize.

Line 
1#import "SapphireSeason.h"
2#import "SapphireTVShow.h"
3#import "SapphireFileSorter.h"
4#import "SapphireMetaDataSupport.h"
5
6@implementation SapphireSeason
7
8static NSArray *allowedSorts = nil;
9
10+ (void)load
11{
12        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDurationSorter sharedInstance], [SapphireFileSizeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil];
13}
14
15+ (SapphireSeason *)season:(int)season forShow:(NSString *)show inContext:(NSManagedObjectContext *)moc
16{
17        SapphireTVShow *tvshow = [SapphireTVShow show:show 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 *)upgradeSeasonVersion:(int)version from:(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- (NSString *)autoSortPath
65{
66        NSString *showPath = [self.tvShow autoSortPath];
67        if(showPath == nil)
68                return nil;
69       
70        return [showPath stringByAppendingPathComponent:[self seasonName]];
71}
72
73- (NSPredicate *)metaFileFetchPredicate
74{
75        return [NSPredicate predicateWithFormat:@"tvEpisode.season == %@", self];
76}
77
78- (id <SapphireDirectory>)parentDirectory
79{
80        return self.tvShow;
81}
82
83- (NSArray *)fileSorters
84{
85        return allowedSorts;
86}
87
88- (NSString *)path
89{
90        NSString *myName = [NSString stringWithFormat:@"Season %d", self.seasonNumberValue];
91        return [self.tvShow.path stringByAppendingPathComponent:myName];
92}
93
94- (NSString *)classDefaultCoverPath
95{
96        return [[NSBundle bundleForClass:[self class]] pathForResource:@"TV" ofType:@"png"];
97}
98
99- (BOOL)shouldDelete
100{
101        return [self.episodesSet count] == 0;
102}
103
104-(void)removeEpisodes:(NSSet*)removedEpisodes
105{
106        [super removeEpisodes:removedEpisodes];
107        if([self.episodesSet count] == 0)
108                [SapphireMetaDataSupport setObjectForPendingDelete:self];
109}
110
111- (void)removeEpisodesObject:(SapphireEpisode*)removedEpisode
112{
113        [super removeEpisodesObject:removedEpisode];
114        if([self.episodesSet count] == 0)
115                [SapphireMetaDataSupport setObjectForPendingDelete:self];
116}
117
118@end
Note: See TracBrowser for help on using the repository browser.