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 d45b16c..7e93390 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 @@ -3,7 +3,6 @@ package org.bspeice.minimalbible.activity.downloader; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.MinimalBibleModules; import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager; -import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadThread; import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; import org.crosswire.jsword.book.Book; @@ -14,7 +13,6 @@ import java.util.ArrayList; import java.util.List; import javax.inject.Named; -import javax.inject.Qualifier; import javax.inject.Singleton; import dagger.Module; @@ -29,7 +27,6 @@ import de.devland.esperandro.Esperandro; BookListFragment.class, BookItemHolder.class, BookDownloadManager.class, - BookDownloadThread.class, RefreshManager.class, DownloadNavDrawerFragment.class, DownloadActivity.class, diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadManager.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadManager.java index c07a1c8..1857eba 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadManager.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadManager.java @@ -14,6 +14,8 @@ import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.BooksEvent; import org.crosswire.jsword.book.BooksListener; +import org.crosswire.jsword.book.install.InstallException; +import org.crosswire.jsword.book.install.Installer; import java.util.HashMap; import java.util.Map; @@ -22,6 +24,9 @@ import javax.inject.Inject; import javax.inject.Provider; import javax.inject.Singleton; +import rx.Observable; +import rx.functions.Action1; +import rx.schedulers.Schedulers; import rx.subjects.PublishSubject; /** @@ -31,6 +36,7 @@ import rx.subjects.PublishSubject; //TODO: Install indexes for Bibles @Singleton public class BookDownloadManager implements WorkListener, BooksListener { + private String TAG = "BookDownloadManager"; /** * Mapping of Job ID to the EventBus we should trigger progress on @@ -44,8 +50,8 @@ public class BookDownloadManager implements WorkListener, BooksListener { private final PublishSubject downloadEvents = PublishSubject.create(); - @Inject - Provider dlThreadProvider; + @Inject Books installedBooks; + @Inject RefreshManager refreshManager; @Inject public BookDownloadManager(Injector injector) { @@ -53,13 +59,12 @@ public class BookDownloadManager implements WorkListener, BooksListener { inProgressDownloads = new HashMap(); JobManager.addWorkListener(this); injector.inject(this); - Books.installed().addBooksListener(this); + installedBooks.addBooksListener(this); } public void installBook(Book b) { - BookDownloadThread dlThread = dlThreadProvider.get(); - dlThread.downloadBook(b); - addJob(BookDownloadThread.getJobId(b), b); + downloadBook(b); + addJob(getJobId(b), b); downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)); } @@ -67,6 +72,40 @@ public class BookDownloadManager implements WorkListener, BooksListener { bookMappings.put(jobId, b); } + public void downloadBook(final Book b) { + // So, the JobManager can't be injected, but we'll make do + + // First, look up where the Book came from + Observable.from(refreshManager.installerFromBook(b)) + .subscribeOn(Schedulers.io()) + .subscribe(new Action1() { + @Override + public void call(Installer installer) { + try { + installer.install(b); + } catch (InstallException e) { + Log.d(TAG, e.getMessage()); + } + + getDownloadEvents() + .onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)); + } + }); + } + + /** + * Build what the installer creates the job name as. + * Likely prone to be brittle. + * TODO: Make sure to test that this is an accurate job name + * + * @param b The book to predict the download job name of + * @return The name of the job that will/is download/ing this book + */ + + public static String getJobId(Book b) { + return "INSTALL_BOOK-" + b.getInitials(); + } + @Override public void workProgressed(WorkEvent ev) { Progress job = ev.getJob(); diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadThread.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadThread.java deleted file mode 100644 index 86f2614..0000000 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/BookDownloadThread.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.bspeice.minimalbible.activity.downloader.manager; - -import android.content.Context; -import android.util.Log; - -import org.bspeice.minimalbible.Injector; -import org.bspeice.minimalbible.MinimalBible; -import org.bspeice.minimalbible.activity.downloader.DownloadActivity; -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.install.InstallException; -import org.crosswire.jsword.book.install.Installer; - -import javax.inject.Inject; - -import rx.functions.Action1; -import rx.schedulers.Schedulers; - -/** - * Thread that handles downloading a book - */ -//TODO: Refactor to BookDownloadManager, downloadBook() creates its own thread -public class BookDownloadThread { - - private final String TAG = "BookDownloadThread"; - - @Inject - BookDownloadManager bookDownloadManager; - @Inject - RefreshManager refreshManager; - - @Inject - public BookDownloadThread(Injector injector) { - injector.inject(this); - } - - public void downloadBook(final Book b) { - // So, the JobManager can't be injected, but we'll make do - - // First, look up where the Book came from - refreshManager.installerFromBook(b) - .subscribeOn(Schedulers.io()) - .subscribe(new Action1() { - @Override - public void call(Installer installer) { - try { - installer.install(b); - } catch (InstallException e) { - Log.d(TAG, e.getMessage()); - } - - bookDownloadManager.getDownloadEvents() - .onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)); - } - }); - } - - /** - * Build what the installer creates the job name as. - * Likely prone to be brittle. - * TODO: Make sure to test that this is an accurate job name - * - * @param b The book to predict the download job name of - * @return The name of the job that will/is download/ing this book - */ - - public static String getJobId(Book b) { - return "INSTALL_BOOK-" + b.getInitials(); - } -} diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.java index a09a172..e0347df 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.java @@ -27,12 +27,12 @@ import rx.schedulers.Schedulers; /** * Handle refreshing the list of books available as needed + * Note that we don't refactor this class out since we need somewhere + * to track whether the refresh is done. */ @Singleton public class RefreshManager { - @Inject InstalledManager installedManager; - /** * Cached copy of modules that are available so we don't refresh for everyone who requests it. */ @@ -50,7 +50,7 @@ public class RefreshManager { * when it's done. * TODO: Should I have a better way of scheduling than Schedulers.io()? */ - private void refreshModules() { + private Observable>> refreshModules() { if (availableModules == null) { availableModules = Observable.from(new InstallManager().getInstallers().values()) .map(new Func1>>() { @@ -78,9 +78,6 @@ public class RefreshManager { } }); } - } - - public Observable>> getAvailableModules() { return availableModules; } @@ -102,49 +99,28 @@ public class RefreshManager { }); } - /** - * Get the cached book list - * @return The cached book list, or null - */ - public List getBookList() { - List availableList = new ArrayList(); - availableModules.reduce(availableList, - new Func2, Map>, List>() { - @Override - public List call(List books, Map> installerListMap) { - for (List l : installerListMap.values()) { - books.addAll(l); - } - return books; - } - }); - return availableList; - } /** * Find the installer that a Book comes from. * @param b The book to search for * @return The Installer that should be used for this book. */ - public Observable installerFromBook(final Book b) { - return availableModules.filter(new Func1>, Boolean>() { - @Override - public Boolean call(Map> installerListMap) { + public Installer installerFromBook(final Book b) { + Map> element = availableModules + .filter(new Func1>, Boolean>() { + @Override + public Boolean call(Map> installerListMap) { for (List element : installerListMap.values()) { if (element.contains(b)) { return true; } } return false; - } - }) - .first() - .map(new Func1>, Installer>() { - @Override - public Installer call(Map> element) { - return element.entrySet().iterator().next().getKey(); - } - }); + } + }) + .toBlocking() + .first(); + return element.entrySet().iterator().next().getKey(); } public boolean isRefreshComplete() {