From 66076c759a8ff979cf5b6e39451c26dbb410a2a8 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 26 Nov 2014 23:22:18 -0500 Subject: [PATCH] Switch to the VerseLookup over parser Allows me to use the LRUCache frontent --- .../activity/viewer/BookFragment.java | 5 +- .../activity/viewer/BibleViewClient.kt | 50 ------------------- .../activity/viewer/BookAdapter.kt | 16 +++--- .../service/format/osisparser/OsisParser.kt | 14 +----- .../service/lookup/VerseLookup.kt | 13 ++--- 5 files changed, 21 insertions(+), 77 deletions(-) delete mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java index 8728910..7d31f27 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java @@ -12,6 +12,7 @@ import android.view.ViewGroup; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseFragment; +import org.bspeice.minimalbible.service.lookup.VerseLookup; import org.crosswire.jsword.book.Book; import javax.inject.Inject; @@ -34,6 +35,8 @@ public class BookFragment extends BaseFragment { Book mBook; @Inject PublishSubject scrollEventProvider; + @Inject + VerseLookup lookup; @InjectView(R.id.book_content) RecyclerView bookContent; @@ -91,7 +94,7 @@ public class BookFragment extends BaseFragment { ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); final RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity()); - BookAdapter adapter = new BookAdapter(b); + BookAdapter adapter = new BookAdapter(b, lookup); bookContent.setLayoutManager(manager); bookContent.setAdapter(adapter); diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt deleted file mode 100644 index b06df2c..0000000 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt +++ /dev/null @@ -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?) : 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 = 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() - } -} \ No newline at end of file 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 f6a00f6..a1dae49 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 @@ -6,19 +6,19 @@ import org.crosswire.jsword.book.Book import android.view.LayoutInflater import org.bspeice.minimalbible.R import android.widget.TextView -import org.bspeice.minimalbible.service.format.osisparser.OsisParser import org.crosswire.jsword.book.getVersification import org.crosswire.jsword.versification.getBooks import org.crosswire.jsword.versification.BibleBook import org.bspeice.minimalbible.activity.viewer.BookAdapter.ChapterInfo import rx.subjects.PublishSubject +import org.bspeice.minimalbible.service.lookup.VerseLookup /** * Adapter used for displaying a book * Displays one chapter at a time, * 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() { val versification = b.getVersification() @@ -58,7 +58,7 @@ class BookAdapter(val b: Book) val emptyView = LayoutInflater.from(parent?.getContext()) .inflate(R.layout.viewer_passage_view, parent, false) as TextView - val passage = PassageView(emptyView) + val passage = PassageView(emptyView, b, lookup) return passage } @@ -87,16 +87,16 @@ class BookAdapter(val b: Book) } } -class PassageView(val v: TextView) : RecyclerView.ViewHolder(v) { - val parser = OsisParser() +class PassageView(val v: TextView, val b: Book, val lookup: VerseLookup) +: RecyclerView.ViewHolder(v) { - fun getVerseText(b: Book, verseRange: Progression) = - verseRange.map { parser.getVerse(b, it).content } + fun getVerseText(verseRange: Progression) = + verseRange.map { lookup.getText(b.getVersification().decodeOrdinal(it)) } fun reduceText(verses: List) = verses.join(" ") // Uses functional style, but those parentheses man... you'd think I was writing LISP fun bind(info: ChapterInfo) { - v.setText(reduceText(getVerseText(info.book, info.vStart..info.vEnd))) + v.setText(reduceText(getVerseText(info.vStart..info.vEnd))) } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt index bb31258..2197369 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/format/osisparser/OsisParser.kt @@ -5,10 +5,8 @@ import org.crosswire.jsword.passage.Verse import java.util.ArrayDeque import org.xml.sax.Attributes import org.crosswire.jsword.book.OSISUtil -import org.bspeice.minimalbible.SafeValDelegate import org.crosswire.jsword.book.BookData import org.crosswire.jsword.book.Book -import org.crosswire.jsword.book.getVersification import kotlin.properties.Delegates /** @@ -24,17 +22,9 @@ class OsisParser() : DefaultHandler() { // TODO: Implement a stack to keep min API 8 val doWrite = ArrayDeque() - fun getJson(b: Book, v: Verse): String { - // I don't always set up my constructors to have faces, but when I do... - verseContent = VerseContent(v = v) + fun getVerse(b: Book, v: Verse): VerseContent { + verseContent = VerseContent(v) 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 } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/lookup/VerseLookup.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/lookup/VerseLookup.kt index faf1145..c746c42 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/lookup/VerseLookup.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/lookup/VerseLookup.kt @@ -8,6 +8,7 @@ import rx.subjects.PublishSubject import rx.schedulers.Schedulers import org.bspeice.minimalbible.service.format.osisparser.OsisParser 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 @@ -28,11 +29,11 @@ open class VerseLookup(val b: Book) : Action1 { fun getVerseId(v: Verse) = v.getOrdinal() - fun getJson(v: Verse): String = + fun getText(v: Verse): String = if (cache contains v) cache[getVerseId(v)] else { - val content = doLookup(v) + val content = doLookup(v).content notify(v) content } @@ -43,11 +44,11 @@ open class VerseLookup(val b: Book) : Action1 { * it is displayed. * * @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(ordinal: Int): String = OsisParser() - .getJson(b, Verse(b.getVersification(), ordinal)) + fun doLookup(v: Verse): VerseContent = OsisParser().getVerse(b, v) + fun doLookup(ordinal: Int): VerseContent = OsisParser() + .getVerse(b, b.getVersification() decodeOrdinal ordinal) /** * Not necessary, but helpful if you let us know ahead of time we should pre-cache a verse.