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