From 540426a0a26254b04ff20a6e688a2cb86c1c8534 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 8 Sep 2014 23:36:33 -0400 Subject: [PATCH] The kotlin migration continues... --- .../activity/viewer/BibleViewerModules.java | 1 + .../activity/viewer/BookFragment.java | 1 + .../viewer/bookutil/VersificationUtil.java | 62 ------------------- .../activity/viewer/VersificationUtil.kt | 53 ++++++++++++++++ 4 files changed, 55 insertions(+), 62 deletions(-) delete mode 100644 app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/VersificationUtil.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java index 62190dd..94d0e0a 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java @@ -98,6 +98,7 @@ public class BibleViewerModules { return new BookManager(); } + // If this is ever injected by more than one person, switch to extension functions in Kotlin @Provides VersificationUtil provideVersificationUtil() { return new VersificationUtil(); 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..92b132c 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 @@ -40,6 +40,7 @@ public class BookFragment extends BaseFragment { @Inject @Named("MainBook") Lazy mBook; + // TODO: Once @Inject VersificationUtil vUtil; // TODO: Factory? diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java deleted file mode 100644 index ae2787e..0000000 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.bspeice.minimalbible.activity.viewer.bookutil; - -import org.bspeice.minimalbible.util.IteratorUtil; -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.BookMetaData; -import org.crosswire.jsword.versification.BibleBook; -import org.crosswire.jsword.versification.Versification; -import org.crosswire.jsword.versification.system.Versifications; - -import java.util.ArrayList; -import java.util.List; - -import rx.Observable; -import rx.functions.Func1; - -/** - * Created by bspeice on 7/11/14. - */ -public class VersificationUtil { - - private static final List INTROS = new ArrayList() {{ - add(BibleBook.INTRO_BIBLE); - add(BibleBook.INTRO_OT); - add(BibleBook.INTRO_NT); - }}; - - // TODO: Cache the versification? - public Versification getVersification(Book b) { - return Versifications.instance().getVersification( - (String) b.getBookMetaData().getProperty(BookMetaData.KEY_VERSIFICATION) - ); - } - - public Observable getBooks(Book b) { - Versification v = getVersification(b); - return Observable.from(IteratorUtil.copyIterator(v.getBookIterator())) - .filter(new Func1() { - @Override - public Boolean call(BibleBook bibleBook) { - return !INTROS.contains(bibleBook); - } - }); - } - - public Observable getBookNames(final Book b) { - return getBooks(b) - .map(new Func1() { - @Override - public String call(BibleBook bibleBook) { - return getBookName(b, bibleBook); - } - }); - } - - public Integer getChapterCount(Book b, BibleBook bibleBook) { - return getVersification(b).getLastChapter(bibleBook); - } - - public String getBookName(Book book, BibleBook bibleBook) { - return getVersification(book).getLongName(bibleBook); - } -} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/VersificationUtil.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/VersificationUtil.kt new file mode 100644 index 0000000..6c7c049 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/VersificationUtil.kt @@ -0,0 +1,53 @@ +/** + * Created by bspeice on 9/8/14. + */ +package org.bspeice.minimalbible.activity.viewer.bookutil + +import org.crosswire.jsword.versification.BibleBook +import java.util.ArrayList +import org.crosswire.jsword.versification.Versification +import org.crosswire.jsword.book.Book +import org.crosswire.jsword.versification.system.Versifications +import org.crosswire.jsword.book.BookMetaData +import rx.Observable + +class VersificationUtil() { + + val INTROS: Array = array( + BibleBook.INTRO_BIBLE, + BibleBook.INTRO_OT, + BibleBook.INTRO_NT) + + public fun getVersification(b: Book): Versification { + return Versifications.instance()!!.getVersification( + b.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION) as String + ) as Versification + } + + public fun getBookName(book: Book, bibleBook: BibleBook): String { + return getVersification(book).getLongName(bibleBook) as String + } + + public fun getChapterCount(book: Book, bibleBook: BibleBook): Int { + return getVersification(book).getLastChapter(bibleBook) + } + + public fun getBooks(b: Book): Observable { + return Observable.from(getVersification(b).getBookIterator()!!.copyIterator()) + ?.filter { INTROS.contains(it) } as Observable + } + + public fun getBookNames(b: Book): Observable { + return getBooks(b).map { getBookName(b, it as BibleBook) } + as Observable + } + +} + +fun Iterator.copyIterator(): List { + var list: MutableList = ArrayList() // Must be mutable + while (this.hasNext()) { + list.add(this.next()) + } + return list +} \ No newline at end of file