mirror of
https://github.com/MinimalBible/MinimalBible
synced 2025-07-13 03:35:20 -04:00
Get a first manager to Kotlin
Extension functions are fun...
This commit is contained in:
@ -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<Book> { result -> },
|
||||
Action1<Throwable> { error -> },
|
||||
Action0 { refreshComplete = true })
|
||||
}
|
||||
|
||||
fun getBooks(): Observable<Book> {
|
||||
return installedBooks as Observable
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.bspeice.minimalbible.service.manager
|
||||
|
||||
/**
|
||||
* Created by bspeice on 9/11/14.
|
||||
*/
|
||||
|
||||
class InvalidBookException(message: String?) : Exception(message) {}
|
@ -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<String> {
|
||||
return Observable.from(b.getVersification().getBookNames(b)) as Observable
|
||||
return Observable.from(b.getVersification().getBookNames()) as Observable
|
||||
}
|
||||
|
||||
fun getBooks(b: Book): Observable<BibleBook> {
|
||||
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 <T> Iterator<T>.iterable(): Iterable<T> {
|
||||
val list: MutableList<T> = ArrayList()
|
||||
while (this.hasNext()) {
|
||||
@ -50,13 +52,13 @@ fun <T> Iterator<T>.iterable(): Iterable<T> {
|
||||
return list
|
||||
}
|
||||
|
||||
fun Versification.getBooks(b: Book): List<BibleBook> {
|
||||
fun Versification.getBooks(): List<BibleBook> {
|
||||
return this.getBookIterator()!!.iterable()
|
||||
.filter { VersificationUtil.INTROS.contains(it) }
|
||||
}
|
||||
|
||||
fun Versification.getBookNames(b: Book): List<String> {
|
||||
return this.getBooks(b).map { this.getLongName(it) as String }
|
||||
fun Versification.getBookNames(): List<String> {
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user