mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-25 09:28:21 -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.Injector;
|
||||||
import org.bspeice.minimalbible.MinimalBibleModules;
|
import org.bspeice.minimalbible.MinimalBibleModules;
|
||||||
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
|
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.InstalledManager;
|
||||||
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
|
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
|
||||||
import org.crosswire.jsword.book.Book;
|
import org.crosswire.jsword.book.Book;
|
||||||
@ -14,7 +13,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
import javax.inject.Qualifier;
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
@ -29,7 +27,6 @@ import de.devland.esperandro.Esperandro;
|
|||||||
BookListFragment.class,
|
BookListFragment.class,
|
||||||
BookItemHolder.class,
|
BookItemHolder.class,
|
||||||
BookDownloadManager.class,
|
BookDownloadManager.class,
|
||||||
BookDownloadThread.class,
|
|
||||||
RefreshManager.class,
|
RefreshManager.class,
|
||||||
DownloadNavDrawerFragment.class,
|
DownloadNavDrawerFragment.class,
|
||||||
DownloadActivity.class,
|
DownloadActivity.class,
|
||||||
|
@ -14,6 +14,8 @@ import org.crosswire.jsword.book.Book;
|
|||||||
import org.crosswire.jsword.book.Books;
|
import org.crosswire.jsword.book.Books;
|
||||||
import org.crosswire.jsword.book.BooksEvent;
|
import org.crosswire.jsword.book.BooksEvent;
|
||||||
import org.crosswire.jsword.book.BooksListener;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -22,6 +24,9 @@ import javax.inject.Inject;
|
|||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import rx.Observable;
|
||||||
|
import rx.functions.Action1;
|
||||||
|
import rx.schedulers.Schedulers;
|
||||||
import rx.subjects.PublishSubject;
|
import rx.subjects.PublishSubject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,6 +36,7 @@ import rx.subjects.PublishSubject;
|
|||||||
//TODO: Install indexes for Bibles
|
//TODO: Install indexes for Bibles
|
||||||
@Singleton
|
@Singleton
|
||||||
public class BookDownloadManager implements WorkListener, BooksListener {
|
public class BookDownloadManager implements WorkListener, BooksListener {
|
||||||
|
private String TAG = "BookDownloadManager";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mapping of Job ID to the EventBus we should trigger progress on
|
* 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();
|
private final PublishSubject<DLProgressEvent> downloadEvents = PublishSubject.create();
|
||||||
|
|
||||||
@Inject
|
@Inject Books installedBooks;
|
||||||
Provider<BookDownloadThread> dlThreadProvider;
|
@Inject RefreshManager refreshManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public BookDownloadManager(Injector injector) {
|
public BookDownloadManager(Injector injector) {
|
||||||
@ -53,13 +59,12 @@ public class BookDownloadManager implements WorkListener, BooksListener {
|
|||||||
inProgressDownloads = new HashMap<Book, DLProgressEvent>();
|
inProgressDownloads = new HashMap<Book, DLProgressEvent>();
|
||||||
JobManager.addWorkListener(this);
|
JobManager.addWorkListener(this);
|
||||||
injector.inject(this);
|
injector.inject(this);
|
||||||
Books.installed().addBooksListener(this);
|
installedBooks.addBooksListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void installBook(Book b) {
|
public void installBook(Book b) {
|
||||||
BookDownloadThread dlThread = dlThreadProvider.get();
|
downloadBook(b);
|
||||||
dlThread.downloadBook(b);
|
addJob(getJobId(b), b);
|
||||||
addJob(BookDownloadThread.getJobId(b), b);
|
|
||||||
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b));
|
downloadEvents.onNext(new DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +72,40 @@ public class BookDownloadManager implements WorkListener, BooksListener {
|
|||||||
bookMappings.put(jobId, b);
|
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
|
@Override
|
||||||
public void workProgressed(WorkEvent ev) {
|
public void workProgressed(WorkEvent ev) {
|
||||||
Progress job = ev.getJob();
|
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
|
* 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
|
@Singleton
|
||||||
public class RefreshManager {
|
public class RefreshManager {
|
||||||
|
|
||||||
@Inject InstalledManager installedManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cached copy of modules that are available so we don't refresh for everyone who requests it.
|
* 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.
|
* when it's done.
|
||||||
* TODO: Should I have a better way of scheduling than Schedulers.io()?
|
* 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) {
|
if (availableModules == null) {
|
||||||
availableModules = Observable.from(new InstallManager().getInstallers().values())
|
availableModules = Observable.from(new InstallManager().getInstallers().values())
|
||||||
.map(new Func1<Installer, Map<Installer, List<Book>>>() {
|
.map(new Func1<Installer, Map<Installer, List<Book>>>() {
|
||||||
@ -78,9 +78,6 @@ public class RefreshManager {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Observable<Map<Installer, List<Book>>> getAvailableModules() {
|
|
||||||
return availableModules;
|
return availableModules;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,32 +99,15 @@ 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.
|
* Find the installer that a Book comes from.
|
||||||
* @param b The book to search for
|
* @param b The book to search for
|
||||||
* @return The Installer that should be used for this book.
|
* @return The Installer that should be used for this book.
|
||||||
*/
|
*/
|
||||||
public Observable<Installer> installerFromBook(final Book b) {
|
public Installer installerFromBook(final Book b) {
|
||||||
return availableModules.filter(new Func1<Map<Installer, List<Book>>, Boolean>() {
|
Map<Installer, List<Book>> element = availableModules
|
||||||
|
.filter(new Func1<Map<Installer, List<Book>>, Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public Boolean call(Map<Installer, List<Book>> installerListMap) {
|
public Boolean call(Map<Installer, List<Book>> installerListMap) {
|
||||||
for (List<Book> element : installerListMap.values()) {
|
for (List<Book> element : installerListMap.values()) {
|
||||||
@ -138,14 +118,10 @@ public class RefreshManager {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.first()
|
.toBlocking()
|
||||||
.map(new Func1<Map<Installer, List<Book>>, Installer>() {
|
.first();
|
||||||
@Override
|
|
||||||
public Installer call(Map<Installer, List<Book>> element) {
|
|
||||||
return element.entrySet().iterator().next().getKey();
|
return element.entrySet().iterator().next().getKey();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isRefreshComplete() {
|
public boolean isRefreshComplete() {
|
||||||
return refreshComplete.get();
|
return refreshComplete.get();
|
||||||
|
Loading…
Reference in New Issue
Block a user