Only show search item if search is ready

This commit is contained in:
Bradlee Speice
2015-02-16 12:07:06 -05:00
parent 7a35ad69ea
commit 6d534cfa9b
3 changed files with 62 additions and 15 deletions

View File

@ -5,28 +5,38 @@ import org.crosswire.jsword.book.Book
import android.util.Log
import rx.schedulers.Schedulers
import rx.Observable
import rx.subjects.PublishSubject
import org.crosswire.jsword.index.IndexStatus
import rx.subjects.ReplaySubject
/**
* There's already an IndexManager, that's why the funky name
*/
class MBIndexManager(val indexManager: IndexManager) {
fun shouldIndex(b: Book) = b.getIndexStatus() == IndexStatus.UNDONE
fun indexReady(b: Book) = b.getIndexStatus() == IndexStatus.DONE
/**
* Do the hard work of actually building the book index.
* Returns a PublishSubject<> that completes when the
* index is complete. Also is nice enough to broadcast
* what work is being done when.
*/
fun buildIndex(b: Book): PublishSubject<IndexStatus> {
val indexStatus: PublishSubject<IndexStatus> = PublishSubject.create();
fun buildIndex(b: Book): ReplaySubject<IndexStatus> {
if (!shouldIndex(b)) {
Log.e("MBIndexManager", "Current status is ${b.getIndexStatus()}, not creating index")
throw IllegalStateException("Don't try and index a book that should not get it.")
}
val indexStatus: ReplaySubject<IndexStatus> = ReplaySubject.create();
Observable.just(b)
.observeOn(Schedulers.computation())
.subscribe({
indexStatus.onNext(b.getIndexStatus())
Log.e("MBIndexManager", "Building index for ${b.getInitials()}, ${b.getIndexStatus()}")
indexManager scheduleIndexCreation b
Log.e("MBIndexManager", "Done building index for ${b.getInitials()}, ${b.getIndexStatus()}")
indexStatus.onNext(b.getIndexStatus())
indexStatus.onCompleted()