Commits

Peter Hosey  committed b25054a

Automatically remove redundant empty chapters after emptying the last chapter.

  • Participants
  • Parent commits 6a17c62

Comments (0)

Files changed (1)

 }
 
 - (void) implicitlyAddNextChapterIfNecessary {
+	bool madeChanges = false;
+
 	RipChapter *lastChapter = [self.chapters lastObject];
+	NSUInteger idx = [self countOfChapters];
+	while (lastChapter && ([lastChapter countOfSourceTracks] == 0UL)) {
+		[self removeObjectFromChaptersAtIndex:--idx];
+		madeChanges = true;
+		lastChapter = [self.chapters lastObject];
+	}
+	RipChapter *nextChapter = nil;
 	if ((lastChapter == nil) || ([lastChapter countOfSourceTracks] > 0UL)) {
-		RipChapter *nextChapter = [[[RipChapter alloc] init] autorelease];
+		nextChapter = [[[RipChapter alloc] init] autorelease];
 		nextChapter.title = [self titleForNextChapter];
 		[[self mutableArrayValueForKey:KVC_KEY(chapters)] addObject:nextChapter];
+		madeChanges = true;
+	}
+
+	if (madeChanges) {
 		[tracksView reloadItem:nil reloadChildren:YES];
-		[tracksView expandItem:nextChapter];
+		if (nextChapter)
+			[tracksView expandItem:nextChapter];
 	}
 }
 - (NSString *) titleForNextChapter {