From 6d534cfa9b25050c39dc4fcc2cd0a519563c2a19 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 16 Feb 2015 12:07:06 -0500 Subject: [PATCH] Only show search item if search is ready --- .../minimalbible/MinimalBibleModules.java | 17 +++---- .../activity/viewer/BibleViewer.java | 44 ++++++++++++++++++- .../activity/search/MBIndexManager.kt | 16 +++++-- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java index 59e86cd..f697d8c 100644 --- a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java @@ -10,7 +10,6 @@ import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.index.IndexManager; import org.crosswire.jsword.index.IndexManagerFactory; -import org.crosswire.jsword.index.IndexStatus; import java.util.ArrayList; import java.util.List; @@ -30,6 +29,7 @@ import rx.functions.Func1; /** * Entry point for the default modules used by MinimalBible */ +@SuppressWarnings("unused") @Module(library = true) public class MinimalBibleModules { MinimalBible app; @@ -67,7 +67,7 @@ public class MinimalBibleModules { * Provide a raw reference to the books installed. Please don't use this, chances are * you should go through List since it excludes the invalid books. * - * @return + * @return The raw reference to JSword Books class */ @Provides @Singleton @@ -102,10 +102,10 @@ public class MinimalBibleModules { } @Provides + @Singleton @Named("MainBook") - Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs, - MBIndexManager indexManager) { - final AtomicReference mBook = new AtomicReference(null); + Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs) { + final AtomicReference mBook = new AtomicReference<>(null); bookManager.getInstalledBooks() .first(new Func1() { @Override @@ -147,12 +147,7 @@ public class MinimalBibleModules { } } - Book b = mBook.get(); - if (b.getIndexStatus() != IndexStatus.DONE) { - indexManager.buildIndex(b); - } - - return b; + return mBook.get(); } @Provides 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 d959256..d63ca48 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 @@ -20,8 +20,10 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.search.BasicSearch; +import org.bspeice.minimalbible.activity.search.MBIndexManager; import org.bspeice.minimalbible.activity.settings.MinimalBibleSettings; import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.index.IndexStatus; import javax.inject.Inject; import javax.inject.Named; @@ -29,6 +31,7 @@ import javax.inject.Named; import butterknife.ButterKnife; import butterknife.InjectView; import dagger.ObjectGraph; +import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; import rx.subjects.PublishSubject; @@ -44,6 +47,9 @@ public class BibleViewer extends BaseActivity implements Injector { @Inject PublishSubject scrollEventPublisher; + @Inject + MBIndexManager indexManager; + @InjectView(R.id.navigation_drawer) BibleMenu bibleMenu; @@ -145,7 +151,7 @@ public class BibleViewer extends BaseActivity implements Injector { (SearchManager) getSystemService(Context.SEARCH_SERVICE); // And we can't call getActionView() directly, because it needs API 11+ - MenuItem item = menu.findItem(R.id.action_search); + final MenuItem item = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); // The Android docs instruct you to set up search in the current activity. @@ -153,6 +159,10 @@ public class BibleViewer extends BaseActivity implements Injector { ComponentName cN = new ComponentName(this, BasicSearch.class); searchView.setSearchableInfo(searchManager.getSearchableInfo(cN)); + // Finally, search menu should be hidden by default - show it once we can guarantee + // than an index is created for the current book + displaySearchMenu(item, mainBook, indexManager); + return super.onCreateOptionsMenu(menu); } @@ -169,4 +179,36 @@ public class BibleViewer extends BaseActivity implements Injector { return super.onOptionsItemSelected(item); } + + /** + * Display the search menu as needed - + * Specifically, make the menu hidden by default, and show it once the book + * has actually been indexed. + * + * @param item The menu item to switch visibility of + * @param b The book controlling whether the menu is visible + * @param indexManager Manager to generate the index if it doesn't yet exist. + */ + public void displaySearchMenu(final MenuItem item, final Book b, + final MBIndexManager indexManager) { + if (b.getIndexStatus() == IndexStatus.DONE) { + item.setVisible(true); + return; + } + + item.setVisible(false); + + if (indexManager.shouldIndex(b)) { + indexManager.buildIndex(b) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1() { + @Override + public void call(IndexStatus indexStatus) { + item.setVisible(indexManager.indexReady(b)); + } + }); + } + + item.setVisible(indexManager.indexReady(b)); + } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt index a2a3316..4008025 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt @@ -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 { - val indexStatus: PublishSubject = PublishSubject.create(); + fun buildIndex(b: Book): ReplaySubject { + 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 = 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()