mirror of
				https://github.com/MinimalBible/MinimalBible
				synced 2025-11-03 18:10:27 -05:00 
			
		
		
		
	Only show search item if search is ready
This commit is contained in:
		@ -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<Book> 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<Book> mBook = new AtomicReference<Book>(null);
 | 
			
		||||
    Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs) {
 | 
			
		||||
        final AtomicReference<Book> mBook = new AtomicReference<>(null);
 | 
			
		||||
        bookManager.getInstalledBooks()
 | 
			
		||||
                .first(new Func1<Book, Boolean>() {
 | 
			
		||||
                    @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
 | 
			
		||||
 | 
			
		||||
@ -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<BookScrollEvent> 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<IndexStatus>() {
 | 
			
		||||
                        @Override
 | 
			
		||||
                        public void call(IndexStatus indexStatus) {
 | 
			
		||||
                            item.setVisible(indexManager.indexReady(b));
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        item.setVisible(indexManager.indexReady(b));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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()
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user