From cb13dd64aaa1af03c6c44f272fc0fabccc8504c1 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 11 Sep 2014 00:41:11 -0400 Subject: [PATCH] Get a first manager to Kotlin Extension functions are fun... --- .../activity/viewer/BibleViewer.java | 1 + .../activity/viewer/BibleViewerModules.java | 1 + .../activity/viewer/BookManager.java | 66 ------------------- .../service/manager/BookManager.kt | 33 ++++++++++ .../service/manager/InvalidBookException.kt | 7 ++ .../jsword/versification/VersificationUtil.kt | 25 ++++--- .../org/bspeice/minimalbible/TestModules.java | 6 +- 7 files changed, 62 insertions(+), 77 deletions(-) delete mode 100644 app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookManager.java create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/service/manager/InvalidBookException.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java index a2bc84a..cc1b853 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java @@ -18,6 +18,7 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment; +import org.bspeice.minimalbible.service.manager.BookManager; import org.crosswire.jsword.book.Book; import javax.inject.Inject; 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 76eb24c..e7b860b 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 @@ -4,6 +4,7 @@ import android.util.Log; import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter; import org.bspeice.minimalbible.service.book.VerseLookupModules; +import org.bspeice.minimalbible.service.manager.BookManager; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.versification.VersificationUtil; diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookManager.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookManager.java deleted file mode 100644 index 28a17bc..0000000 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookManager.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.bspeice.minimalbible.activity.viewer; - -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.Books; - -import java.util.ArrayList; -import java.util.List; - -import javax.inject.Inject; -import javax.inject.Singleton; - -import rx.Observable; -import rx.functions.Action0; -import rx.functions.Action1; -import rx.functions.Func1; -import rx.schedulers.Schedulers; - -/** - * Created by bspeice on 6/18/14. - */ -@Singleton -public class BookManager { - - private Observable installedBooks; - private Boolean refreshComplete; - - // Some of these books seem to think they're installed... - private List excludeBooks = new ArrayList() {{ - add("ERen_no"); - add("ot1nt2"); - }}; - - @Inject - BookManager() { - // TODO: Any way this can be sped up goes straight to the initialization time. - installedBooks = Observable.from(Books.installed().getBooks()) - .filter(new Func1() { - @Override - public Boolean call(Book book) { - return !excludeBooks.contains(book.getInitials()); - } - }) - .cache(); - installedBooks.subscribeOn(Schedulers.io()) - .subscribe(new Action1() { - @Override - public void call(Book book) {} - }, new Action1() { - @Override - public void call(Throwable throwable) {} - }, new Action0() { - @Override - public void call() { - BookManager.this.refreshComplete = true; - } - }); - } - - public Observable getInstalledBooks() { - return installedBooks; - } - - public Boolean isRefreshComplete() { - return refreshComplete; - } -} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt new file mode 100644 index 0000000..7f984e8 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt @@ -0,0 +1,33 @@ +package org.bspeice.minimalbible.service.manager + +import rx.Observable +import org.crosswire.jsword.book.Books +import rx.functions.Action1 +import org.crosswire.jsword.book.Book +import rx.functions.Action0 + +/** + * Created by bspeice on 9/10/14. + */ + +//@Singleton +class BookManager() { + // Some extra books like to think they're installed, but trigger NPE all over the place... + val ignore = array("ERen_no", "ot1nt2"); + + val installedBooks = Observable.from(Books.installed()!!.getBooks()) + ?.filter { !ignore.contains(it!!.getInitials()) } + ?.cache() + var refreshComplete = false; + + { + // TODO: Cleaner way of expressing this? + installedBooks?.subscribe(Action1 { result -> }, + Action1 { error -> }, + Action0 { refreshComplete = true }) + } + + fun getBooks(): Observable { + return installedBooks as Observable + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/InvalidBookException.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/InvalidBookException.kt new file mode 100644 index 0000000..926bfc9 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/InvalidBookException.kt @@ -0,0 +1,7 @@ +package org.bspeice.minimalbible.service.manager + +/** + * Created by bspeice on 9/11/14. + */ + +class InvalidBookException(message: String?) : Exception(message) {} \ No newline at end of file diff --git a/app/src/main/kotlin/org/crosswire/jsword/versification/VersificationUtil.kt b/app/src/main/kotlin/org/crosswire/jsword/versification/VersificationUtil.kt index 6964b56..4b99dc9 100644 --- a/app/src/main/kotlin/org/crosswire/jsword/versification/VersificationUtil.kt +++ b/app/src/main/kotlin/org/crosswire/jsword/versification/VersificationUtil.kt @@ -5,6 +5,8 @@ import java.util.ArrayList import org.crosswire.jsword.versification.system.Versifications import org.crosswire.jsword.book.BookMetaData import rx.Observable +import android.util.Log +import org.bspeice.minimalbible.service.manager.InvalidBookException /** * Created by bspeice on 9/10/14. @@ -20,11 +22,11 @@ class VersificationUtil() { } fun getBookNames(b: Book): Observable { - return Observable.from(b.getVersification().getBookNames(b)) as Observable + return Observable.from(b.getVersification().getBookNames()) as Observable } fun getBooks(b: Book): Observable { - return Observable.from(b.getVersification().getBooks(b)) as Observable + return Observable.from(b.getVersification().getBooks()) as Observable } fun getChapterCount(b: Book, bibleBook: BibleBook): Int { @@ -40,7 +42,7 @@ class VersificationUtil() { } } -// There's probably a better way to do this +// TODO: Refactor (is there a better way to accomplish this?) and move fun Iterator.iterable(): Iterable { val list: MutableList = ArrayList() while (this.hasNext()) { @@ -50,13 +52,13 @@ fun Iterator.iterable(): Iterable { return list } -fun Versification.getBooks(b: Book): List { +fun Versification.getBooks(): List { return this.getBookIterator()!!.iterable() .filter { VersificationUtil.INTROS.contains(it) } } -fun Versification.getBookNames(b: Book): List { - return this.getBooks(b).map { this.getLongName(it) as String } +fun Versification.getBookNames(): List { + return this.getBooks().map { this.getLongName(it) as String } } fun Versification.getChapterCount(b: BibleBook): Int { @@ -64,7 +66,12 @@ fun Versification.getChapterCount(b: BibleBook): Int { } fun Book.getVersification(): Versification { - return Versifications.instance()!!.getVersification( - this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION) as String - ) as Versification + val v = Versifications.instance()!!.getVersification( + this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION).toString() + ) + if (v == null) { + Log.e(getClass()!!.getSimpleName(), "Invalid book: " + this.getInitials()) + throw InvalidBookException(this.getInitials()) + } else + return v } \ No newline at end of file diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java index a0162f7..ed03d46 100644 --- a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java +++ b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java @@ -2,7 +2,7 @@ package org.bspeice.minimalbible; import org.bspeice.minimalbible.activity.downloader.DownloadActivity; -import org.bspeice.minimalbible.activity.viewer.BookManager; +import org.bspeice.minimalbible.service.manager.BookManager; import org.crosswire.jsword.book.BookCategory; import java.util.ArrayList; @@ -30,7 +30,8 @@ public class TestModules { return testActivityTitle; } - @Provides @Singleton + @Provides + @Singleton @Named("ValidCategories") List provideValidCategories() { return new ArrayList() {{ @@ -46,6 +47,7 @@ public class TestModules { } @Provides + @Singleton BookManager provideBookManager() { return bookManager; }