Switch to the VerseLookup over parser

Allows me to use the LRUCache frontent
This commit is contained in:
Bradlee Speice 2014-11-26 23:22:18 -05:00
parent 96c5c895e9
commit 66076c759a
5 changed files with 21 additions and 77 deletions

View File

@ -12,6 +12,7 @@ import android.view.ViewGroup;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseFragment; import org.bspeice.minimalbible.activity.BaseFragment;
import org.bspeice.minimalbible.service.lookup.VerseLookup;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import javax.inject.Inject; import javax.inject.Inject;
@ -34,6 +35,8 @@ public class BookFragment extends BaseFragment {
Book mBook; Book mBook;
@Inject @Inject
PublishSubject<BookScrollEvent> scrollEventProvider; PublishSubject<BookScrollEvent> scrollEventProvider;
@Inject
VerseLookup lookup;
@InjectView(R.id.book_content) @InjectView(R.id.book_content)
RecyclerView bookContent; RecyclerView bookContent;
@ -91,7 +94,7 @@ public class BookFragment extends BaseFragment {
((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials());
final RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity()); final RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity());
BookAdapter adapter = new BookAdapter(b); BookAdapter adapter = new BookAdapter(b, lookup);
bookContent.setLayoutManager(manager); bookContent.setLayoutManager(manager);
bookContent.setAdapter(adapter); bookContent.setAdapter(adapter);

View File

@ -1,50 +0,0 @@
package org.bspeice.minimalbible.activity.viewer
import org.crosswire.jsword.passage.Verse
import android.webkit.WebViewClient
import android.webkit.JavascriptInterface
import org.crosswire.jsword.book.Book
import android.util.Log
import rx.subjects.PublishSubject
import org.crosswire.jsword.book.getVersification
import org.bspeice.minimalbible.service.lookup.VerseLookup
/**
* Created by bspeice on 9/14/14.
*/
class BibleViewClient(val b: Book, val lookup: VerseLookup,
val subject: PublishSubject<String>?) : WebViewClient() {
// We can receive and return only primitives and Strings. Still means we can use JSON :)
JavascriptInterface fun getVerse(ordinal: Int): String {
val v = Verse(b.getVersification(), ordinal)
// TODO: WebView should notify us what verse it's on
subject?.onNext("${v.getBook()} ${v.getChapter()}:${v.getVerse()}")
return lookup getJson v
}
JavascriptInterface fun getVerses(first: Int, count: Int): String {
Log.e("getVerses", "First: $first count: $count")
val verses: MutableList<String> = linkedListOf()
val trueCount: Int
val trueFirst: Int
when {
first < 0 - count -> return ""
first < 0 -> {
trueCount = count + first // Equivalent to count - abs(first)
trueFirst = 0
}
else -> {
trueCount = count
trueFirst = first
}
}
for (i in trueFirst..trueFirst + trueCount - 1) {
verses.add(getVerse(i))
}
Log.e("getVerses", "return verses size: ${verses.size}")
return verses.toString()
}
}

View File

@ -6,19 +6,19 @@ import org.crosswire.jsword.book.Book
import android.view.LayoutInflater import android.view.LayoutInflater
import org.bspeice.minimalbible.R import org.bspeice.minimalbible.R
import android.widget.TextView import android.widget.TextView
import org.bspeice.minimalbible.service.format.osisparser.OsisParser
import org.crosswire.jsword.book.getVersification import org.crosswire.jsword.book.getVersification
import org.crosswire.jsword.versification.getBooks import org.crosswire.jsword.versification.getBooks
import org.crosswire.jsword.versification.BibleBook import org.crosswire.jsword.versification.BibleBook
import org.bspeice.minimalbible.activity.viewer.BookAdapter.ChapterInfo import org.bspeice.minimalbible.activity.viewer.BookAdapter.ChapterInfo
import rx.subjects.PublishSubject import rx.subjects.PublishSubject
import org.bspeice.minimalbible.service.lookup.VerseLookup
/** /**
* Adapter used for displaying a book * Adapter used for displaying a book
* Displays one chapter at a time, * Displays one chapter at a time,
* as each TextView widget is it's own line break * as each TextView widget is it's own line break
*/ */
class BookAdapter(val b: Book) class BookAdapter(val b: Book, val lookup: VerseLookup)
: RecyclerView.Adapter<PassageView>() { : RecyclerView.Adapter<PassageView>() {
val versification = b.getVersification() val versification = b.getVersification()
@ -58,7 +58,7 @@ class BookAdapter(val b: Book)
val emptyView = LayoutInflater.from(parent?.getContext()) val emptyView = LayoutInflater.from(parent?.getContext())
.inflate(R.layout.viewer_passage_view, parent, false) as TextView .inflate(R.layout.viewer_passage_view, parent, false) as TextView
val passage = PassageView(emptyView) val passage = PassageView(emptyView, b, lookup)
return passage return passage
} }
@ -87,16 +87,16 @@ class BookAdapter(val b: Book)
} }
} }
class PassageView(val v: TextView) : RecyclerView.ViewHolder(v) { class PassageView(val v: TextView, val b: Book, val lookup: VerseLookup)
val parser = OsisParser() : RecyclerView.ViewHolder(v) {
fun getVerseText(b: Book, verseRange: Progression<Int>) = fun getVerseText(verseRange: Progression<Int>) =
verseRange.map { parser.getVerse(b, it).content } verseRange.map { lookup.getText(b.getVersification().decodeOrdinal(it)) }
fun reduceText(verses: List<String>) = verses.join(" ") fun reduceText(verses: List<String>) = verses.join(" ")
// Uses functional style, but those parentheses man... you'd think I was writing LISP // Uses functional style, but those parentheses man... you'd think I was writing LISP
fun bind(info: ChapterInfo) { fun bind(info: ChapterInfo) {
v.setText(reduceText(getVerseText(info.book, info.vStart..info.vEnd))) v.setText(reduceText(getVerseText(info.vStart..info.vEnd)))
} }
} }

View File

@ -5,10 +5,8 @@ import org.crosswire.jsword.passage.Verse
import java.util.ArrayDeque import java.util.ArrayDeque
import org.xml.sax.Attributes import org.xml.sax.Attributes
import org.crosswire.jsword.book.OSISUtil import org.crosswire.jsword.book.OSISUtil
import org.bspeice.minimalbible.SafeValDelegate
import org.crosswire.jsword.book.BookData import org.crosswire.jsword.book.BookData
import org.crosswire.jsword.book.Book import org.crosswire.jsword.book.Book
import org.crosswire.jsword.book.getVersification
import kotlin.properties.Delegates import kotlin.properties.Delegates
/** /**
@ -24,17 +22,9 @@ class OsisParser() : DefaultHandler() {
// TODO: Implement a stack to keep min API 8 // TODO: Implement a stack to keep min API 8
val doWrite = ArrayDeque<Boolean>() val doWrite = ArrayDeque<Boolean>()
fun getJson(b: Book, v: Verse): String { fun getVerse(b: Book, v: Verse): VerseContent {
// I don't always set up my constructors to have faces, but when I do... verseContent = VerseContent(v)
verseContent = VerseContent(v = v)
BookData(b, v).getSAXEventProvider() provideSAXEvents this BookData(b, v).getSAXEventProvider() provideSAXEvents this
return verseContent.json
}
fun getVerse(b: Book, v: Int): VerseContent {
val verse = b.getVersification().decodeOrdinal(v)
verseContent = VerseContent(verse)
BookData(b, verse).getSAXEventProvider() provideSAXEvents this
return verseContent return verseContent
} }

View File

@ -8,6 +8,7 @@ import rx.subjects.PublishSubject
import rx.schedulers.Schedulers import rx.schedulers.Schedulers
import org.bspeice.minimalbible.service.format.osisparser.OsisParser import org.bspeice.minimalbible.service.format.osisparser.OsisParser
import org.crosswire.jsword.book.getVersification import org.crosswire.jsword.book.getVersification
import org.bspeice.minimalbible.service.format.osisparser.VerseContent
/** /**
* Do the low-level work of getting a verse's content * Do the low-level work of getting a verse's content
@ -28,11 +29,11 @@ open class VerseLookup(val b: Book) : Action1<Verse> {
fun getVerseId(v: Verse) = v.getOrdinal() fun getVerseId(v: Verse) = v.getOrdinal()
fun getJson(v: Verse): String = fun getText(v: Verse): String =
if (cache contains v) if (cache contains v)
cache[getVerseId(v)] cache[getVerseId(v)]
else { else {
val content = doLookup(v) val content = doLookup(v).content
notify(v) notify(v)
content content
} }
@ -43,11 +44,11 @@ open class VerseLookup(val b: Book) : Action1<Verse> {
* it is displayed. * it is displayed.
* *
* @param v The verse to look up * @param v The verse to look up
* @return The JSON content of this verse * @return The string content of this verse
*/ */
fun doLookup(v: Verse): String = OsisParser().getJson(b, v) fun doLookup(v: Verse): VerseContent = OsisParser().getVerse(b, v)
fun doLookup(ordinal: Int): String = OsisParser() fun doLookup(ordinal: Int): VerseContent = OsisParser()
.getJson(b, Verse(b.getVersification(), ordinal)) .getVerse(b, b.getVersification() decodeOrdinal ordinal)
/** /**
* Not necessary, but helpful if you let us know ahead of time we should pre-cache a verse. * Not necessary, but helpful if you let us know ahead of time we should pre-cache a verse.