From df5700107d13ed4431abafe6060209dd2cc91dda Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 14 Sep 2014 21:56:09 -0400 Subject: [PATCH] Kotlin WebView/Javascript client! --- app/src/main/assets/dist/book-bundle.js | 7 +-- app/src/main/assets/src/book.coffee | 8 ++-- .../activity/viewer/BookFragment.java | 45 ++----------------- .../service/book/VerseLookupService.java | 1 - .../activity/viewer/BibleViewClient.kt | 23 ++++++++++ 5 files changed, 36 insertions(+), 48 deletions(-) create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt diff --git a/app/src/main/assets/dist/book-bundle.js b/app/src/main/assets/dist/book-bundle.js index 2f7fd13..2607edc 100755 --- a/app/src/main/assets/dist/book-bundle.js +++ b/app/src/main/assets/dist/book-bundle.js @@ -13,10 +13,11 @@ app.controller('BookCtrl', [ $scope.order_verses = function() { return $scope.verses = $filter('orderBy')($scope.verses, 'id', false); }; - return $scope.appendVerse = function(verse) { - $scope.verses.push(verse); + $scope.appendVerse = function(jsonVerseString) { + $scope.verses.push(angular.fromJson(jsonVerseString)); return $scope.order_verses(); }; + return $scope.appendVerse(Android.getVerse(5)); } ]); @@ -25,7 +26,7 @@ controller = "#bookController"; window.appendVerse = function(jsonVerseString) { var scope; scope = angular.element($("#bookController")).scope(); - scope.appendVerse(angular.fromJson(jsonVerseString)); + scope.appendVerse(jsonVerseString); return scope.$apply(); }; diff --git a/app/src/main/assets/src/book.coffee b/app/src/main/assets/src/book.coffee index 85baa5e..e5b8480 100644 --- a/app/src/main/assets/src/book.coffee +++ b/app/src/main/assets/src/book.coffee @@ -9,9 +9,11 @@ app.controller 'BookCtrl', ['$scope', '$filter', ($scope, $filter) -> $scope.order_verses = -> $scope.verses = $filter('orderBy')($scope.verses, 'id', false) - $scope.appendVerse = (verse) -> - $scope.verses.push verse + $scope.appendVerse = (jsonVerseString) -> + $scope.verses.push angular.fromJson jsonVerseString $scope.order_verses() + + $scope.appendVerse Android.getVerse(5) ] # Due to page initialization, we can only store the controller string. @@ -21,7 +23,7 @@ controller = "#bookController" window.appendVerse = (jsonVerseString) -> scope = angular.element($("#bookController")).scope() - scope.appendVerse angular.fromJson jsonVerseString + scope.appendVerse jsonVerseString # Since we're calling outside of angular, we need to manually apply scope.$apply() 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 6b18680..6fbc50a 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 @@ -7,9 +7,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.webkit.JavascriptInterface; import android.webkit.WebView; -import android.webkit.WebViewClient; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.R; @@ -17,8 +15,6 @@ 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 org.crosswire.jsword.versification.BibleBook; -import org.crosswire.jsword.versification.VersificationUtil; import javax.inject.Inject; import javax.inject.Named; @@ -36,8 +32,7 @@ public class BookFragment extends BaseFragment { @Inject @Named("MainBook") Lazy mBook; - @Inject - VersificationUtil vUtil; + // TODO: Factory? VerseLookupService lookupService; @InjectView(R.id.book_content) @@ -103,33 +98,10 @@ public class BookFragment extends BaseFragment { Log.d("BookFragment", b.getName()); ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); mainContent.loadUrl(getString(R.string.book_html)); - mainContent.setWebViewClient(new WebViewClient(){ - @Override - public void onPageFinished(WebView view, String url) { - // TODO: Restore this verse from a SharedPref - Verse initial = new Verse(vUtil.getVersification(mBook.get()), - BibleBook.GEN, 1, 1); - super.onPageFinished(view, url); - Log.e(getClass().getSimpleName(), lookupService.getJsonVerse(initial)); - invokeJavascript("appendVerse", lookupService.getJsonVerse(initial)); - } - @Override - public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { - super.onReceivedError(view, errorCode, description, failingUrl); - Log.e(this.getClass().getSimpleName(), "Code: " + errorCode + " " + - description); - } - }); - - // We can receive and return only primitives and Strings. Still means we can use JSON :) - mainContent.addJavascriptInterface(new Object() { - @JavascriptInterface - @SuppressWarnings("unused") - public String testReturn(String echo) { - return echo; - } - }, "Android"); + BibleViewClient client = new BibleViewClient(b, lookupService); + mainContent.setWebViewClient(client); + mainContent.addJavascriptInterface(client, "Android"); // TODO: Remove remote debugging when ready - or should this be removed? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { @@ -147,13 +119,4 @@ public class BookFragment extends BaseFragment { Book b = mBook.get(); lookupService.getJsonVerse(v); } - - /*----------------------------------------- - Here be the methods you wish didn't have to exist. - ----------------------------------------- - */ - - private void invokeJavascript(String function, Object arg) { - mainContent.loadUrl("javascript:" + function + "('" + arg.toString() + "')"); - } } diff --git a/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java b/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java index a244ea6..c638938 100644 --- a/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java +++ b/app/src/main/java/org/bspeice/minimalbible/service/book/VerseLookupService.java @@ -30,7 +30,6 @@ import rx.subjects.PublishSubject; */ public class VerseLookupService implements Action1 { - Book book; @Inject 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 new file mode 100644 index 0000000..1d54e67 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleViewClient.kt @@ -0,0 +1,23 @@ +package org.bspeice.minimalbible.activity.viewer + +import org.crosswire.jsword.passage.Verse +import org.bspeice.minimalbible.service.book.VerseLookupService +import android.webkit.WebViewClient +import android.webkit.JavascriptInterface +import org.crosswire.jsword.book.Book +import org.crosswire.jsword.versification.getVersification + +/** + * Created by bspeice on 9/14/14. + */ + +class BibleViewClient(b: Book, lookup: VerseLookupService) : WebViewClient() { + val b = b + val lookup = lookup + + // 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) + return lookup.getJsonVerse(v) as String + } +} \ No newline at end of file