mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-04 23:28:19 -05:00
Kotlin WebView/Javascript client!
This commit is contained in:
parent
d309d0de19
commit
df5700107d
7
app/src/main/assets/dist/book-bundle.js
vendored
7
app/src/main/assets/dist/book-bundle.js
vendored
@ -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();
|
||||
};
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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() + "')");
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,6 @@ import rx.subjects.PublishSubject;
|
||||
*/
|
||||
public class VerseLookupService implements Action1<Verse> {
|
||||
|
||||
|
||||
Book book;
|
||||
|
||||
@Inject
|
||||
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user