diff --git a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java index e57456d..f6b865b 100644 --- a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java @@ -2,11 +2,18 @@ package org.bspeice.minimalbible; import android.app.Application; -import javax.inject.Named; +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.Books; + +import java.util.ArrayList; +import java.util.List; + import javax.inject.Singleton; import dagger.Module; import dagger.Provides; +import rx.Observable; +import rx.functions.Func1; /** * Entry point for the default modules used by MinimalBible @@ -25,15 +32,52 @@ public class MinimalBibleModules { } /** - * This field allows us to set application-wide whether we are in a test or not - * Allows components on down the line to know whether they should set some things up or not. - * Additionally, not a Singleton so we can enable/disable testing mode as needed. However, - * for production, it's always false. - * @return Whether we are in a test - false + * Provide a list of book names that are known bad. This can be because they trigger NPE, + * or are just missing lots of content, etc. + * @return the list of books (by name) to ignore */ @Provides - @Named("Testing") - boolean isTest() { - return false; + @Singleton + List invalidBooks() { + List list = new ArrayList<>(); + list.add("ABU"); // Missing content + list.add("ERen_no"); // Thinks its installed, when it isn't. Triggers NPE + list.add("ot1nt2"); // Thinks its installed, when it isn't. Triggers NPE + + return list; + } + + //TODO: Move this to a true async + + /** + * 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 + */ + @Provides + @Singleton + Books provideInstalledBooks() { + return Books.installed(); + } + + /** + * Use this to get the list of books installed, as filtered by what should be excluded + * + * @param b The raw Books instance to get the installed list from + * @param invalidBooks The books to exclude from usage + * @return The books available for using + */ + @Provides + List provideInstalledBooks(Books b, final List invalidBooks) { + List rawBooks = b.getBooks(); + return Observable.from(rawBooks) + .filter(new Func1() { + @Override + public Boolean call(Book book) { + return invalidBooks.contains(book.getInitials()); + } + }) + .toList().toBlocking().first(); } } diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java index a07ac05..55b8c1e 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java @@ -8,7 +8,6 @@ import org.bspeice.minimalbible.MinimalBibleModules; import org.bspeice.minimalbible.activity.downloader.manager.BookManager; import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; -import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.install.InstallManager; @@ -96,18 +95,6 @@ public class DownloadActivityModules { }}; } - //TODO: Move this to a true async - @Provides - @Singleton - Books provideInstalledBooks() { - return Books.installed(); - } - - @Provides - List provideInstalledBooks(Books b) { - return b.getBooks(); - } - @Provides @Singleton Collection provideInstallers() { @@ -116,9 +103,10 @@ public class DownloadActivityModules { @Provides @Singleton - RefreshManager provideRefreshManager(Collection installers, DownloadPrefs prefs, + RefreshManager provideRefreshManager(Collection installers, List exclude, + DownloadPrefs prefs, @Named("DownloadActivityContext") Context context) { - return new RefreshManager(installers, prefs, + return new RefreshManager(installers, exclude, prefs, (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); } diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/settings/AvailableBookPreference.java b/app/src/main/java/org/bspeice/minimalbible/activity/settings/AvailableBookPreference.java index ab1e98b..2a956bf 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/settings/AvailableBookPreference.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/settings/AvailableBookPreference.java @@ -6,32 +6,37 @@ import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; +import org.bspeice.minimalbible.Injector; import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.Books; import java.util.List; +import javax.inject.Inject; + /** * Set the active "main book" * Can not be implemented in Kotlin due to array needs */ public class AvailableBookPreference extends ListPreference { + @Inject + List books; + public AvailableBookPreference(Context ctx, AttributeSet attrs) { super(ctx, attrs); } @Override protected View onCreateView(ViewGroup parent) { - // TODO: Refactor out the static reference - List books = Books.installed().getBooks(); + ((Injector) getContext()).inject(this); + CharSequence[] entries = new CharSequence[books.size()]; CharSequence[] entryValues = new CharSequence[books.size()]; for (int i = 0; i < books.size(); i++) { Book b = books.get(i); entries[i] = b.getName(); - entryValues[i] = b.getInitials(); + entryValues[i] = b.getName(); } setEntries(entries); diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.java b/app/src/main/java/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.java new file mode 100644 index 0000000..5c70827 --- /dev/null +++ b/app/src/main/java/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.java @@ -0,0 +1,64 @@ +package org.bspeice.minimalbible.activity.settings; + +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.v7.widget.Toolbar; +import android.widget.LinearLayout; + +import org.bspeice.minimalbible.Injector; +import org.bspeice.minimalbible.MinimalBible; +import org.bspeice.minimalbible.MinimalBibleModules; +import org.bspeice.minimalbible.R; +import org.bspeice.minimalbible.activity.BaseActivity; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import dagger.Module; +import dagger.ObjectGraph; + +/** + * Created by bspeice on 12/29/14. + */ +public class MinimalBibleSettings extends PreferenceActivity + implements Injector { + + ObjectGraph settingsGraph; + @InjectView(R.id.toolbar) + Toolbar toolbar; + @InjectView(R.id.container) + LinearLayout root; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.preferences); + setContentView(R.layout.activity_settings); + BaseActivity.setupStatusBar(this); + + ButterKnife.inject(this); + + toolbar.setTitle(R.string.action_settings); + BaseActivity.setupInsets(this, root); + } + + private ObjectGraph buildObjGraph() { + MinimalBible app = (MinimalBible) getApplicationContext(); + + return app.plus(new SettingsModule()); + } + + @Override + public void inject(Object o) { + if (settingsGraph == null) { + settingsGraph = buildObjGraph(); + } + settingsGraph.inject(o); + } + + @Module(injects = AvailableBookPreference.class, + addsTo = MinimalBibleModules.class) + class SettingsModule { + + } +} 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 21eeac0..c55b585 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 @@ -54,6 +54,8 @@ public class BibleViewer extends BaseActivity implements Injector { /** * Build a scoped object graph for anything used by the BibleViewer + * and inject ourselves + * TODO: Refactor so buildObjGraph doesn't have side effects */ private void buildObjGraph() { if (bvObjectGraph == null) { diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java index 7dd605a..7712b44 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java @@ -2,9 +2,11 @@ package org.bspeice.minimalbible.activity.viewer; import android.util.Log; +import org.bspeice.minimalbible.MinimalBibleModules; import org.bspeice.minimalbible.service.manager.BookManager; import org.crosswire.jsword.book.Book; +import java.util.List; import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicReference; @@ -24,7 +26,8 @@ import rx.subjects.PublishSubject; @Module( injects = { BibleViewer.class, - } + }, + addsTo = MinimalBibleModules.class ) @SuppressWarnings("unused") public class BibleViewerModules { @@ -90,8 +93,8 @@ public class BibleViewerModules { @Provides @Singleton - BookManager bookManager() { - return new BookManager(); + BookManager bookManager(List exclude) { + return new BookManager(exclude); } @Provides diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt index 96e7b89..b382c23 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt @@ -16,6 +16,7 @@ import java.util.Date */ class RefreshManager(val installers: Collection, + val exclude: List, val prefs: DownloadPrefs, val connManager: ConnectivityManager?) { val refreshComplete = AtomicBoolean() @@ -26,7 +27,9 @@ class RefreshManager(val installers: Collection, it.reloadBookList() // TODO: Handle InstallException prefs.downloadRefreshedOn(Date().getTime()) } - mapOf(Pair(it, it.getBooks())) + val validBooks = it.getBooks() + .filterNot { exclude contains it.getInitials() } + mapOf(Pair(it, validBooks)) } .subscribeOn(Schedulers.io()) .cache(); diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt deleted file mode 100644 index 0e08847..0000000 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.bspeice.minimalbible.activity.settings - -import android.preference.PreferenceActivity -import android.os.Bundle -import org.bspeice.minimalbible.R -import android.support.v7.widget.Toolbar -import org.bspeice.minimalbible.activity.BaseActivity -import android.widget.LinearLayout - -/** - * Created by bspeice on 12/1/14. - */ -class MinimalBibleSettings() : PreferenceActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - addPreferencesFromResource(R.xml.preferences) - setContentView(R.layout.activity_settings) - BaseActivity.setupStatusBar(this) - - val toolbar = findViewById(R.id.toolbar) as Toolbar - toolbar.setTitle(R.string.action_settings) - - val root = findViewById(R.id.container) as LinearLayout - BaseActivity.setupInsets(this, root) - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt index ee275d5..8ab5a6f 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/service/manager/BookManager.kt @@ -14,11 +14,9 @@ import javax.inject.Singleton */ Singleton -open class BookManager() { - // Some extra books like to think they're installed, but trigger NPE all over the place... - val ignore = array("ERen_no", "ot1nt2"); - +open class BookManager(val ignore: List) { + // TODO: Remove static reference to Books.installed() open val installedBooks = Observable.from(Books.installed()!!.getBooks()) ?.filter { !ignore.contains(it!!.getInitials()) } ?.cache() ?: throw NoBooksInstalledException()