mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-12-25 16:18:17 -05:00
More refactoring work
Able to delete an extraneous class!
This commit is contained in:
parent
b0327dd491
commit
80e38dacb4
@ -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,
|
||||
|
@ -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<DLProgressEvent> downloadEvents = PublishSubject.create();
|
||||
|
||||
@Inject
|
||||
Provider<BookDownloadThread> 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<Book, DLProgressEvent>();
|
||||
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<Installer>() {
|
||||
@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();
|
||||
|
@ -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<Installer>() {
|
||||
@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();
|
||||
}
|
||||
}
|
@ -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<Map<Installer, List<Book>>> refreshModules() {
|
||||
if (availableModules == null) {
|
||||
availableModules = Observable.from(new InstallManager().getInstallers().values())
|
||||
.map(new Func1<Installer, Map<Installer, List<Book>>>() {
|
||||
@ -78,9 +78,6 @@ public class RefreshManager {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public Observable<Map<Installer, List<Book>>> getAvailableModules() {
|
||||
return availableModules;
|
||||
}
|
||||
|
||||
@ -102,49 +99,28 @@ public class RefreshManager {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cached book list
|
||||
* @return The cached book list, or null
|
||||
*/
|
||||
public List<Book> getBookList() {
|
||||
List<Book> availableList = new ArrayList<Book>();
|
||||
availableModules.reduce(availableList,
|
||||
new Func2<List<Book>, Map<Installer, List<Book>>, List<Book>>() {
|
||||
@Override
|
||||
public List<Book> call(List<Book> books, Map<Installer, List<Book>> installerListMap) {
|
||||
for (List<Book> 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<Installer> installerFromBook(final Book b) {
|
||||
return availableModules.filter(new Func1<Map<Installer, List<Book>>, Boolean>() {
|
||||
@Override
|
||||
public Boolean call(Map<Installer, List<Book>> installerListMap) {
|
||||
public Installer installerFromBook(final Book b) {
|
||||
Map<Installer, List<Book>> element = availableModules
|
||||
.filter(new Func1<Map<Installer, List<Book>>, Boolean>() {
|
||||
@Override
|
||||
public Boolean call(Map<Installer, List<Book>> installerListMap) {
|
||||
for (List<Book> element : installerListMap.values()) {
|
||||
if (element.contains(b)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.first()
|
||||
.map(new Func1<Map<Installer, List<Book>>, Installer>() {
|
||||
@Override
|
||||
public Installer call(Map<Installer, List<Book>> element) {
|
||||
return element.entrySet().iterator().next().getKey();
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.toBlocking()
|
||||
.first();
|
||||
return element.entrySet().iterator().next().getKey();
|
||||
}
|
||||
|
||||
public boolean isRefreshComplete() {
|
||||
|
Loading…
Reference in New Issue
Block a user