Kotlin WebView/Javascript client!

This commit is contained in:
Bradlee Speice 2014-09-14 21:56:09 -04:00
parent d309d0de19
commit df5700107d
5 changed files with 36 additions and 48 deletions

View File

@ -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();
};

View File

@ -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()

View File

@ -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<Book> 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() + "')");
}
}

View File

@ -30,7 +30,6 @@ import rx.subjects.PublishSubject;
*/
public class VerseLookupService implements Action1<Verse> {
Book book;
@Inject

View File

@ -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
}
}