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.book.Books;
 | 
				
			||||||
import org.crosswire.jsword.index.IndexManager;
 | 
					import org.crosswire.jsword.index.IndexManager;
 | 
				
			||||||
import org.crosswire.jsword.index.IndexManagerFactory;
 | 
					import org.crosswire.jsword.index.IndexManagerFactory;
 | 
				
			||||||
import org.crosswire.jsword.index.IndexStatus;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
@ -30,6 +29,7 @@ import rx.functions.Func1;
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Entry point for the default modules used by MinimalBible
 | 
					 * Entry point for the default modules used by MinimalBible
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					@SuppressWarnings("unused")
 | 
				
			||||||
@Module(library = true)
 | 
					@Module(library = true)
 | 
				
			||||||
public class MinimalBibleModules {
 | 
					public class MinimalBibleModules {
 | 
				
			||||||
    MinimalBible app;
 | 
					    MinimalBible app;
 | 
				
			||||||
@ -67,7 +67,7 @@ public class MinimalBibleModules {
 | 
				
			|||||||
     * Provide a raw reference to the books installed. Please don't use this, chances are
 | 
					     * 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.
 | 
					     * you should go through List<Book> since it excludes the invalid books.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @return
 | 
					     * @return The raw reference to JSword Books class
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @Provides
 | 
					    @Provides
 | 
				
			||||||
    @Singleton
 | 
					    @Singleton
 | 
				
			||||||
@ -102,10 +102,10 @@ public class MinimalBibleModules {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Provides
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
    @Named("MainBook")
 | 
					    @Named("MainBook")
 | 
				
			||||||
    Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs,
 | 
					    Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs) {
 | 
				
			||||||
                         MBIndexManager indexManager) {
 | 
					        final AtomicReference<Book> mBook = new AtomicReference<>(null);
 | 
				
			||||||
        final AtomicReference<Book> mBook = new AtomicReference<Book>(null);
 | 
					 | 
				
			||||||
        bookManager.getInstalledBooks()
 | 
					        bookManager.getInstalledBooks()
 | 
				
			||||||
                .first(new Func1<Book, Boolean>() {
 | 
					                .first(new Func1<Book, Boolean>() {
 | 
				
			||||||
                    @Override
 | 
					                    @Override
 | 
				
			||||||
@ -147,12 +147,7 @@ public class MinimalBibleModules {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Book b = mBook.get();
 | 
					        return mBook.get();
 | 
				
			||||||
        if (b.getIndexStatus() != IndexStatus.DONE) {
 | 
					 | 
				
			||||||
            indexManager.buildIndex(b);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return b;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Provides
 | 
					    @Provides
 | 
				
			||||||
 | 
				
			|||||||
@ -20,8 +20,10 @@ import org.bspeice.minimalbible.R;
 | 
				
			|||||||
import org.bspeice.minimalbible.activity.BaseActivity;
 | 
					import org.bspeice.minimalbible.activity.BaseActivity;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
					import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.search.BasicSearch;
 | 
					import org.bspeice.minimalbible.activity.search.BasicSearch;
 | 
				
			||||||
 | 
					import org.bspeice.minimalbible.activity.search.MBIndexManager;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.settings.MinimalBibleSettings;
 | 
					import org.bspeice.minimalbible.activity.settings.MinimalBibleSettings;
 | 
				
			||||||
import org.crosswire.jsword.book.Book;
 | 
					import org.crosswire.jsword.book.Book;
 | 
				
			||||||
 | 
					import org.crosswire.jsword.index.IndexStatus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.inject.Inject;
 | 
					import javax.inject.Inject;
 | 
				
			||||||
import javax.inject.Named;
 | 
					import javax.inject.Named;
 | 
				
			||||||
@ -29,6 +31,7 @@ import javax.inject.Named;
 | 
				
			|||||||
import butterknife.ButterKnife;
 | 
					import butterknife.ButterKnife;
 | 
				
			||||||
import butterknife.InjectView;
 | 
					import butterknife.InjectView;
 | 
				
			||||||
import dagger.ObjectGraph;
 | 
					import dagger.ObjectGraph;
 | 
				
			||||||
 | 
					import rx.android.schedulers.AndroidSchedulers;
 | 
				
			||||||
import rx.functions.Action1;
 | 
					import rx.functions.Action1;
 | 
				
			||||||
import rx.subjects.PublishSubject;
 | 
					import rx.subjects.PublishSubject;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -44,6 +47,9 @@ public class BibleViewer extends BaseActivity implements Injector {
 | 
				
			|||||||
    @Inject
 | 
					    @Inject
 | 
				
			||||||
    PublishSubject<BookScrollEvent> scrollEventPublisher;
 | 
					    PublishSubject<BookScrollEvent> scrollEventPublisher;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Inject
 | 
				
			||||||
 | 
					    MBIndexManager indexManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @InjectView(R.id.navigation_drawer)
 | 
					    @InjectView(R.id.navigation_drawer)
 | 
				
			||||||
    BibleMenu bibleMenu;
 | 
					    BibleMenu bibleMenu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -145,7 +151,7 @@ public class BibleViewer extends BaseActivity implements Injector {
 | 
				
			|||||||
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
 | 
					                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // And we can't call getActionView() directly, because it needs API 11+
 | 
					        // 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);
 | 
					        SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // The Android docs instruct you to set up search in the current activity.
 | 
					        // 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);
 | 
					        ComponentName cN = new ComponentName(this, BasicSearch.class);
 | 
				
			||||||
        searchView.setSearchableInfo(searchManager.getSearchableInfo(cN));
 | 
					        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);
 | 
					        return super.onCreateOptionsMenu(menu);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -169,4 +179,36 @@ public class BibleViewer extends BaseActivity implements Injector {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return super.onOptionsItemSelected(item);
 | 
					        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 android.util.Log
 | 
				
			||||||
import rx.schedulers.Schedulers
 | 
					import rx.schedulers.Schedulers
 | 
				
			||||||
import rx.Observable
 | 
					import rx.Observable
 | 
				
			||||||
import rx.subjects.PublishSubject
 | 
					 | 
				
			||||||
import org.crosswire.jsword.index.IndexStatus
 | 
					import org.crosswire.jsword.index.IndexStatus
 | 
				
			||||||
 | 
					import rx.subjects.ReplaySubject
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * There's already an IndexManager, that's why the funky name
 | 
					 * There's already an IndexManager, that's why the funky name
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class MBIndexManager(val indexManager: IndexManager) {
 | 
					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.
 | 
					     * Do the hard work of actually building the book index.
 | 
				
			||||||
     * Returns a PublishSubject<> that completes when the
 | 
					     * Returns a PublishSubject<> that completes when the
 | 
				
			||||||
     * index is complete. Also is nice enough to broadcast
 | 
					     * index is complete. Also is nice enough to broadcast
 | 
				
			||||||
     * what work is being done when.
 | 
					     * what work is being done when.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    fun buildIndex(b: Book): PublishSubject<IndexStatus> {
 | 
					    fun buildIndex(b: Book): ReplaySubject<IndexStatus> {
 | 
				
			||||||
        val indexStatus: PublishSubject<IndexStatus> = PublishSubject.create();
 | 
					        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)
 | 
					        Observable.just(b)
 | 
				
			||||||
                .observeOn(Schedulers.computation())
 | 
					                .observeOn(Schedulers.computation())
 | 
				
			||||||
                .subscribe({
 | 
					                .subscribe({
 | 
				
			||||||
                    indexStatus.onNext(b.getIndexStatus())
 | 
					                    indexStatus.onNext(b.getIndexStatus())
 | 
				
			||||||
 | 
					                    Log.e("MBIndexManager", "Building index for ${b.getInitials()}, ${b.getIndexStatus()}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    indexManager scheduleIndexCreation b
 | 
					                    indexManager scheduleIndexCreation b
 | 
				
			||||||
 | 
					                    Log.e("MBIndexManager", "Done building index for ${b.getInitials()}, ${b.getIndexStatus()}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    indexStatus.onNext(b.getIndexStatus())
 | 
					                    indexStatus.onNext(b.getIndexStatus())
 | 
				
			||||||
                    indexStatus.onCompleted()
 | 
					                    indexStatus.onCompleted()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user