Commit 994d192c authored by Felix Paul Kühne's avatar Felix Paul Kühne

refactor the code path adding tv show episodes to shows

parent c890bcdc
......@@ -42,6 +42,7 @@
@property (weak, nonatomic, readonly) NSArray *sortedEpisodes;
@property (nonatomic, strong, readonly) NSSet *unreadEpisodes;
- (void)addEpisode:(MLShowEpisode*)episode;
- (void)removeEpisode:(MLShowEpisode*)episode;
- (void)removeEpisodeWithSeasonNumber:(NSNumber *)seasonNumber andEpisodeNumber:(NSNumber *)episodeNumber;
......
......@@ -32,7 +32,7 @@
+ (NSArray *)allEpisodes;
// This will eventually create the show
+ (MLShowEpisode *)episodeWithShow:(id)show episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber createIfNeeded:(BOOL)createIfNeeded;
+ (MLShowEpisode *)episodeWithShow:(MLShow *)show episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber createIfNeeded:(BOOL)createIfNeeded;
+ (MLShowEpisode *)episodeWithShowName:(NSString *)showName episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber
createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated;
......
......@@ -474,8 +474,11 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
BOOL wasInserted = NO;
MLShow *show = nil;
MLShowEpisode *episode = [MLShowEpisode episodeWithShowName:tvShowName episodeNumber:episodeNumber seasonNumber:seasonNumber createIfNeeded:YES wasCreated:&wasInserted];
if (episode)
if (episode) {
show = episode.show;
[show addEpisode:episode];
}
if (wasInserted && !hasNoTvShow) {
show.name = tvShowName;
[self fetchMetaDataForShow:show];
......
......@@ -101,6 +101,19 @@
return [episodes sortedArrayUsingDescriptors:descriptors];
}
- (void)addEpisode:(MLShowEpisode*)episode
{
if (!episode)
return;
NSMutableSet *episodes = [self mutableSetValueForKey:@"episodes"];
[episodes addObject:episode];
[self willChangeValueForKey:@"episodes"];
[self setValue:episodes forKey:@"episodes"];
[self didChangeValueForKey:@"episodes"];
}
- (void)removeEpisode:(MLShowEpisode*)episode
{
if (!episode)
......
......@@ -51,9 +51,13 @@
return episodes;
}
+ (MLShowEpisode *)episodeWithShow:(id)show episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber createIfNeeded:(BOOL)createIfNeeded
+ (MLShowEpisode *)episodeWithShow:(MLShow *)show episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber createIfNeeded:(BOOL)createIfNeeded
{
NSMutableSet *episodes = [show mutableSetValueForKey:@"episodes"];
if (!show)
return NULL;
NSSet *episodes = [show valueForKey:@"episodes"];
MLShowEpisode *episode = nil;
if (seasonNumber && episodeNumber) {
for (MLShowEpisode *episodeIter in episodes) {
......@@ -64,17 +68,21 @@
}
}
}
if (!episode && createIfNeeded) {
episode = [[MLMediaLibrary sharedMediaLibrary] createObjectForEntity:@"ShowEpisode"];
episode.episodeNumber = episodeNumber;
episode.seasonNumber = seasonNumber;
[episodes addObject:episode];
episode.show = show;
}
return episode;
}
+ (MLShowEpisode *)episodeWithShowName:(NSString *)showName episodeNumber:(NSNumber *)episodeNumber seasonNumber:(NSNumber *)seasonNumber
createIfNeeded:(BOOL)createIfNeeded wasCreated:(BOOL *)wasCreated
+ (MLShowEpisode *)episodeWithShowName:(NSString *)showName
episodeNumber:(NSNumber *)episodeNumber
seasonNumber:(NSNumber *)seasonNumber
createIfNeeded:(BOOL)createIfNeeded
wasCreated:(BOOL *)wasCreated
{
MLShow *show = [MLShow showWithName:showName];
*wasCreated = NO;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment