Skip to content

The _getChapterBySpineIndex function does not work when a chapter has subchapters #1

@lvxduck

Description

@lvxduck

The current code looks like this, which does not handle the case where a chapter has subchapters.

// Find chapter with matching content file
final chapters = _bookRef.getChapters();
return chapters.firstWhere(
  (chapter) => chapter.contentFileName == manifestItem!.href,
  orElse: () => throw StateError('Chapter not found'),
);

My proposed solution is to add a flatten function.

List<EpubChapterRef> flattenChapter(EpubChapterRef chapter) {
  return [chapter, ...chapter.subChapters.expand(flattenChapter)];
}

/// Gets a chapter reference by spine index.
EpubChapterRef? _getChapterBySpineIndex(int spineIndex) {
  // ....
  // Find chapter with matching content file
  final chapters = bookRef.getChapters().expand(flattenChapter);
  return chapters.firstWhere(
    (chapter) => chapter.contentFileName == manifestItem!.href,
    orElse: () => throw StateError('Chapter not found'),
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions