From 9424d43ef7b64e040f4eaf512ba8d0c8ec3c27ae Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 12 Oct 2014 23:53:59 -0400 Subject: [PATCH] Not pretty, but displays verse at top of screen --- .../activity/viewer/BookFragment.java | 39 ++++++++++--------- .../activity/viewer/BibleViewClient.kt | 8 ++-- 2 files changed, 26 insertions(+), 21 deletions(-) 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 6fbc50a..a62a32a 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 @@ -14,7 +14,6 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseFragment; import org.bspeice.minimalbible.service.book.VerseLookupService; import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.passage.Verse; import javax.inject.Inject; import javax.inject.Named; @@ -22,6 +21,9 @@ import javax.inject.Named; import butterknife.ButterKnife; import butterknife.InjectView; import dagger.Lazy; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; +import rx.subjects.PublishSubject; /** * A placeholder fragment containing a simple view. @@ -29,16 +31,20 @@ import dagger.Lazy; public class BookFragment extends BaseFragment { private static final String ARG_BOOK_NAME = "book_name"; + + Injector i; @Inject @Named("MainBook") Lazy mBook; - // TODO: Factory? - VerseLookupService lookupService; @InjectView(R.id.book_content) WebView mainContent; + PublishSubject titleReceiver = PublishSubject.create(); + public BookFragment() { + // We can't initialize the lookupService here since the fragment hasn't been tied + // to the parent activity yet. } /** @@ -63,10 +69,8 @@ public class BookFragment extends BaseFragment { Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_viewer_main, container, false); - Injector i = (Injector) getActivity(); + this.i = (Injector) getActivity(); i.inject(this); - // TODO: Defer lookup until after webview created? When exactly is WebView created? - this.lookupService = new VerseLookupService(i, mBook.get()); ButterKnife.inject(this, rootView); mainContent.getSettings().setJavaScriptEnabled(true); @@ -99,7 +103,17 @@ public class BookFragment extends BaseFragment { ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); mainContent.loadUrl(getString(R.string.book_html)); - BibleViewClient client = new BibleViewClient(b, lookupService); + VerseLookupService lookupService = new VerseLookupService(i, mBook.get()); + BibleViewClient client = new BibleViewClient(b, lookupService, titleReceiver); + titleReceiver + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1() { + @Override + public void call(String s) { + ((BibleViewer) getActivity()).setActionBarTitle(s); + Log.d("BibleViewClient", s); + } + }); mainContent.setWebViewClient(client); mainContent.addJavascriptInterface(client, "Android"); @@ -108,15 +122,4 @@ public class BookFragment extends BaseFragment { WebView.setWebContentsDebuggingEnabled(true); } } - - /** - * Do the heavy lifting of getting the actual text for a verse - * - * @param v The verse to display - */ - @SuppressWarnings("unused") - public void displayVerse(Verse v) { - Book b = mBook.get(); - lookupService.getJsonVerse(v); - } } 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 index 46c445b..ebf0a9d 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt @@ -8,18 +8,20 @@ import org.crosswire.jsword.book.Book import org.crosswire.jsword.versification.getVersification import java.util.ArrayList import android.util.Log +import rx.subjects.PublishSubject /** * Created by bspeice on 9/14/14. */ -class BibleViewClient(b: Book, lookup: VerseLookupService) : WebViewClient() { - val b = b - val lookup = lookup +class BibleViewClient(val b: Book, val lookup: VerseLookupService, + 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().toString() + " " + v.getChapter() + ":" + v.getVerse()) return lookup.getJsonVerse(v) as String }