From 4d0a8618a85718d908ccde2893ce7a58f5010321 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 9 Sep 2014 23:05:58 -0400 Subject: [PATCH] Displaying chapter and verse headers now --- app/src/main/assets/book.html | 6 +- app/src/main/assets/dist/book-bundle.js | 16 ++--- app/src/main/assets/src/book.coffee | 14 +++-- .../activity/viewer/BookFragment.java | 5 +- .../service/book/VerseLookupService.java | 14 ++--- .../service/format/osisparser/OsisParser.java | 18 +++--- .../format/osisparser/VerseContent.java | 58 +++++++++++++++++-- 7 files changed, 92 insertions(+), 39 deletions(-) diff --git a/app/src/main/assets/book.html b/app/src/main/assets/book.html index 7dce02a..8098317 100644 --- a/app/src/main/assets/book.html +++ b/app/src/main/assets/book.html @@ -4,7 +4,11 @@
- {{ verse.text }} +

+ {{ verse.chapter }} +

+ {{ verse.verseNum }} + {{ verse.content }}
diff --git a/app/src/main/assets/dist/book-bundle.js b/app/src/main/assets/dist/book-bundle.js index 4589aa9..2f7fd13 100755 --- a/app/src/main/assets/dist/book-bundle.js +++ b/app/src/main/assets/dist/book-bundle.js @@ -8,22 +8,24 @@ require('angular'); app = angular.module('bookApp', []); app.controller('BookCtrl', [ - '$scope', function($scope) { + '$scope', '$filter', function($scope, $filter) { $scope.verses = []; - return $scope.appendVerse = function(text) { - return $scope.verses.push({ - 'text': text - }); + $scope.order_verses = function() { + return $scope.verses = $filter('orderBy')($scope.verses, 'id', false); + }; + return $scope.appendVerse = function(verse) { + $scope.verses.push(verse); + return $scope.order_verses(); }; } ]); controller = "#bookController"; -window.appendVerse = function(text) { +window.appendVerse = function(jsonVerseString) { var scope; scope = angular.element($("#bookController")).scope(); - scope.appendVerse(text); + scope.appendVerse(angular.fromJson(jsonVerseString)); return scope.$apply(); }; diff --git a/app/src/main/assets/src/book.coffee b/app/src/main/assets/src/book.coffee index 455b254..85baa5e 100644 --- a/app/src/main/assets/src/book.coffee +++ b/app/src/main/assets/src/book.coffee @@ -3,11 +3,15 @@ require 'angular' app = angular.module('bookApp', []) -app.controller 'BookCtrl', ['$scope', ($scope) -> +app.controller 'BookCtrl', ['$scope', '$filter', ($scope, $filter) -> $scope.verses = [] - $scope.appendVerse = (text) -> - $scope.verses.push {'text': text} + $scope.order_verses = -> + $scope.verses = $filter('orderBy')($scope.verses, 'id', false) + + $scope.appendVerse = (verse) -> + $scope.verses.push verse + $scope.order_verses() ] # Due to page initialization, we can only store the controller string. @@ -15,9 +19,9 @@ app.controller 'BookCtrl', ['$scope', ($scope) -> # etc. to grab the scope ahead of time and re-use it. controller = "#bookController" -window.appendVerse = (text) -> +window.appendVerse = (jsonVerseString) -> scope = angular.element($("#bookController")).scope() - scope.appendVerse text + scope.appendVerse angular.fromJson 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 e868a55..2f4908b 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 @@ -114,7 +114,8 @@ public class BookFragment extends BaseFragment { Verse initial = new Verse(vUtil.getVersification(mBook.get()), BibleBook.GEN, 1, 1); super.onPageFinished(view, url); - invokeJavascript("appendVerse", lookupService.getHTMLVerse(initial)); + Log.e(getClass().getSimpleName(), lookupService.getJsonVerse(initial)); + invokeJavascript("appendVerse", lookupService.getJsonVerse(initial)); } @Override @@ -148,7 +149,7 @@ public class BookFragment extends BaseFragment { @SuppressWarnings("unused") public void displayVerse(Verse v) { Book b = mBook.get(); - lookupService.getHTMLVerse(v); + lookupService.getJsonVerse(v); } /*----------------------------------------- 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 c7f2f0f..ad61eb4 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 @@ -1,7 +1,6 @@ package org.bspeice.minimalbible.service.book; import android.support.v4.util.LruCache; -import android.util.Log; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.service.format.osisparser.OsisParser; @@ -56,9 +55,9 @@ public class VerseLookupService implements Action1 { * In all cases, notify that we're looking up a verse so we can get the surrounding ones. * * @param v The verse to look up - * @return The HTML text for this verse (\) + * @return The JSON object for this verse (\) */ - public String getHTMLVerse(Verse v) { + public String getJsonVerse(Verse v) { if (contains(v)) { return cache.get(getEntryName(v)); } else { @@ -72,20 +71,19 @@ public class VerseLookupService implements Action1 { /** * Perform the ugly work of getting the actual data for a verse - * TODO: Return a verse object, JS should be left to templating. + * Note that we build the verse object, JS should be left to determine how + * it is displayed. * * @param v The verse to look up - * @return The string content of this verse + * @return The JSON content of this verse */ public String doVerseLookup(Verse v) { BookData bookData = new BookData(book, v); try { SAXEventProvider provider = bookData.getSAXEventProvider(); -// OsisToHtmlSaxHandler handler = new OsisToHtmlSaxHandler(new OsisToHtmlParameters()); OsisParser handler = new OsisParser(v); provider.provideSAXEvents(handler); - Log.e(this.getClass().getName(), handler.toString()); - return handler.getContent().getContent(); + return handler.getContent().toJson(); } catch (BookException e) { e.printStackTrace(); return "Unable to locate " + v.toString() + "!"; diff --git a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java b/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java index 8dd4b7f..7690bf5 100644 --- a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java +++ b/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/OsisParser.java @@ -1,5 +1,7 @@ package org.bspeice.minimalbible.service.format.osisparser; +import android.util.Log; + import org.crosswire.jsword.book.OSISUtil; import org.crosswire.jsword.passage.Verse; import org.xml.sax.Attributes; @@ -9,17 +11,18 @@ import org.xml.sax.helpers.DefaultHandler; import java.util.Stack; /** - * Created by bspeice on 9/8/14. + * Parse a single verse into a VerseContent object. + * Need to benchmark if I should process ranges at a time... */ public class OsisParser extends DefaultHandler { VerseContent verseContent; // ArrayDeque requires API 9 Stack doWrite; - Verse verse; public OsisParser(Verse v) { - this.verse = v; + verseContent = new VerseContent(v); + doWrite = new Stack(); } @Override @@ -29,7 +32,6 @@ public class OsisParser extends DefaultHandler { if (name.equals(OSISUtil.OSIS_ELEMENT_VERSE)) { doWrite.push(true); - verseContent.setId(getId(attributes)); } else { doWrite.push(false); } @@ -43,15 +45,11 @@ public class OsisParser extends DefaultHandler { @Override public void characters(char[] ch, int start, int length) throws SAXException { if (ch != null && doWrite.peek()) { - verseContent.appendContent(new String(ch)); + Log.e(getClass().getName(), new String(ch, start, length)); + verseContent.appendContent(new String(ch, start, length)); } } - @Override - public void startDocument() throws SAXException { - super.startDocument(); - } - @Override public void endDocument() throws SAXException { super.endDocument(); diff --git a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/VerseContent.java b/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/VerseContent.java index e5ed2d2..109e2de 100644 --- a/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/VerseContent.java +++ b/app/src/main/java/org/bspeice/minimalbible/service/format/osisparser/VerseContent.java @@ -1,7 +1,10 @@ package org.bspeice.minimalbible.service.format.osisparser; -import org.apache.commons.lang3.NotImplementedException; +import com.google.gson.Gson; +import org.crosswire.jsword.passage.Verse; + +import java.util.ArrayList; import java.util.List; /** @@ -9,10 +12,23 @@ import java.util.List; */ public class VerseContent { private int id; - private String content; - private String chapterTitle; - private String paraTitle; - private List references; + private String bookName = ""; + private int chapter; + private int verseNum; + private String content = ""; + private String chapterTitle = ""; + private String paraTitle = ""; + private List references = new ArrayList(); + + public VerseContent() { + } + + public VerseContent(Verse v) { + this.id = v.getOrdinal(); + this.bookName = v.getBook().toString(); + this.chapter = v.getChapter(); + this.verseNum = v.getVerse(); + } public int getId() { return id; @@ -58,7 +74,37 @@ public class VerseContent { this.content += content; } + public void appendReference(VerseReference reference) { + this.references.add(reference); + } + + public String getBookName() { + return bookName; + } + + public void setBookName(String bookName) { + this.bookName = bookName; + } + + public int getChapter() { + return chapter; + } + + public void setChapter(int chapter) { + this.chapter = chapter; + } + + public int getVerseNum() { + return verseNum; + } + + public void setVerseNum(int verseNum) { + this.verseNum = verseNum; + } + public String toJson() { - throw new NotImplementedException("JSON conversion not implemented yet!"); + // Lazy load Gson - not likely that we'll call this method multiple times, so + // don't have to worry about a penalty there. + return new Gson().toJson(this); } }