[broken] Convert RefreshManager to Rx

This commit is contained in:
Bradlee Speice 2014-06-10 22:04:53 -04:00
parent 5770e8dd74
commit ca1ccd9942

View File

@ -1,5 +1,7 @@
package org.bspeice.minimalbible.activities.downloader.manager; package org.bspeice.minimalbible.activities.downloader.manager;
import android.os.Handler;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
@ -12,7 +14,8 @@ import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import de.greenrobot.event.EventBus; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
/** /**
* Handle refreshing the list of books available as needed * Handle refreshing the list of books available as needed
@ -25,11 +28,11 @@ public class RefreshManager {
/** /**
* 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.
*/ */
private Map<Installer, List<Book>> availableModules; private Observable<Map<Installer, List<Book>>> availableModules;
private boolean refreshComplete;
public RefreshManager() { public RefreshManager() {
MinimalBible.getApplication().inject(this); MinimalBible.getApplication().inject(this);
availableModules = new HashMap<Installer, List<Book>>();
refreshModules(); refreshModules();
} }
@ -38,18 +41,20 @@ public class RefreshManager {
* when it's done. * when it's done.
*/ */
private void refreshModules() { private void refreshModules() {
EventBus refreshBus = downloadManager.getDownloadBus(); if (availableModules == null) {
refreshBus.register(this); Handler backgroundHandler = new Handler();
new BookRefreshTask().execute(downloadManager.getInstallersArray()); availableModules = Observable.from(downloadManager.getInstallers().values())
} .map(installer -> {
Map<Installer, List<Book>> map = new HashMap<Installer, List<Book>>();
map.put(installer, installer.getBooks());
return map;
}).observeOn(AndroidSchedulers.handlerThread(backgroundHandler))
.cache();
/** // Set refresh complete when it is.
* When book refresh is done, cache the list so we can give that to someone else availableModules.subscribeOn(AndroidSchedulers.handlerThread(backgroundHandler))
* @param event A POJO wrapper around the Book list .subscribe(null, null, () -> refreshComplete = true);
*/ }
@SuppressWarnings("unused")
public void onEvent(EventBookList event) {
this.availableModules = event.getInstallerMapping();
} }
/** /**
@ -57,15 +62,14 @@ public class RefreshManager {
* @return The cached book list, or null * @return The cached book list, or null
*/ */
public List<Book> getBookList() { public List<Book> getBookList() {
if (availableModules.values().size() == 0) { List<Book> availableList = new ArrayList<>();
return null; availableModules.reduce(availableList, (books, installerListMap) -> {
} else { for (List<Book> l : installerListMap.values()) {
List<Book> bookList = new ArrayList<Book>(); books.addAll(l);
for (List<Book> l : availableModules.values()) {
bookList.addAll(l);
} }
return bookList; return books;
} });
return availableList;
} }
/** /**
@ -73,12 +77,16 @@ public class RefreshManager {
* @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 Installer installerFromBook(Book b) { public Observable<Installer> installerFromBook(Book b) {
for (Map.Entry<Installer, List<Book>> entry : availableModules.entrySet()) { return availableModules.filter(installerListMap -> {
if (entry.getValue().contains(b)) { for (List<Book> element : installerListMap.values()) {
return entry.getKey(); if (element.contains(b)) {
return true;
}
} }
} return false;
return null; })
.take(1)
.map(element -> element.entrySet().iterator().next().getKey());
} }
} }