diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt index 823ab21..2418f6e 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt @@ -29,19 +29,29 @@ class BookAdapter(val b: Book) : RecyclerView.Adapter() { data class ChapterInfo(val book: Book, val chapter: Int, val bibleBook: BibleBook, val vStart: Int, val vEnd: Int) + /** + * A list of all ChapterInfo objects needed for displaying a book + * The for expression probably looks a bit nicer: + * for { + * book <- bookList + * chapter <- 1..versification.getLastChapter(currentBook) + * } yield ChapterInfo(...) + * + * Also note that getLastVerse() returns the number of verses in a chapter, + * so we build the actual last verse by adding getFirstVerse and getLastVerse + */ // TODO: Lazy compute values needed for this list val chapterList: List = bookList.flatMap { val currentBook = it - (1..versification.getLastChapter(currentBook)).map { chapter -> - Log.d("BookAdapter", "Building info for chapter ${chapter}") - ChapterInfo(b, chapter, currentBook, - versification.getFirstVerse(currentBook, chapter), - versification.getLastVerse(currentBook, chapter)) + (1..versification.getLastChapter(currentBook)).map { + val firstVerse = versification.getFirstVerse(currentBook, it) + val verseCount = versification.getLastVerse(currentBook, it) + ChapterInfo(b, it, currentBook, firstVerse, firstVerse + verseCount) } } /** - * I'm not sure what the position argument actually is, + * I'm not sure what the position argument actually represents, * but on initial load it doesn't change */ override fun onCreateViewHolder(parent: ViewGroup?, @@ -66,11 +76,15 @@ class BookAdapter(val b: Book) : RecyclerView.Adapter() { } class PassageView(val v: TextView) : RecyclerView.ViewHolder(v) { + val parser = OsisParser() + fun getVerseText(b: Book, verseRange: Progression) = + verseRange.map { parser.getVerse(b, it).content } + + fun reduceText(verses: List) = verses.join(" ") + + // Uses functional style, but those parentheses man... you'd think I was writing LISP fun bind(info: ChapterInfo) { - val o = OsisParser() - val string = o.getVerse(info.book, info.vEnd).content - Log.d("PassageView", "Book: ${info.bibleBook}, Chapter: ${info.chapter}") - v setText string + v.setText(reduceText(getVerseText(info.book, info.vStart..info.vEnd))) } }