Some refactoring and Async fixes.

This commit is contained in:
Bradlee Speice 2014-06-14 14:08:50 -04:00
parent 3869cf0b9b
commit ba3c6ebe6c
3 changed files with 31 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.os.Looper;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -30,6 +31,7 @@ import javax.inject.Inject;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
/** /**
@ -125,36 +127,29 @@ public class BookListFragment extends BaseFragment {
} }
// Listen for the books! // Listen for the books!
refreshManager.getAvailableModules().subscribeOn(AndroidSchedulers.mainThread()) refreshManager.getAvailableModules()
.reduce(new ArrayList<Book>(), (books, installerListMap) -> { // First flatten the Map to its lists
for (List<Book> l : installerListMap.values()) { .flatMap((books) -> Observable.from(books.values()))
books.addAll(l); // Then flatten the lists
} .flatMap((books) -> Observable.from(books))
return books; .filter((book) -> book.getBookCategory() ==
}).take(1) BookCategory.fromString(getArguments().getString(ARG_BOOK_CATEGORY)))
.subscribe((books) -> displayBooks(books)); // Repack all the books
} .toSortedList((book1, book2) ->
BookComparators.getInitialComparator().compare(book1, book2))
/** .observeOn(AndroidSchedulers.mainThread())
* Do the hard work of creating the Adapter and displaying books. .subscribe((books) -> {
* @param bookList The (unfiltered) list of {link org.crosswire.jsword.Book}s to display downloadsAvailable.setAdapter(new BookListAdapter(inflater, books));
*/ if (getActivity() != null) {
public void displayBooks(List<Book> bookList) { // On a screen rotate, getActivity() will be null. But, the activity will
try { // already have been set up correctly, so we don't need to worry about it.
// TODO: Should the filter be applied earlier in the process? // If not null, we need to set it up now.
List<Book> displayList;
BookCategory c = BookCategory.fromString(getArguments().getString(ARG_BOOK_CATEGORY));
BookFilter f = FilterUtil.filterFromCategory(c);
displayList = FilterUtil.applyFilter(bookList, f);
Collections.sort(displayList, BookComparators.getInitialComparator());
downloadsAvailable.setAdapter(new BookListAdapter(inflater, displayList));
setInsets(getActivity(), downloadsAvailable); setInsets(getActivity(), downloadsAvailable);
} catch (FilterUtil.InvalidFilterCategoryMappingException e) {
// To be honest, there should be no reason you end up here.
Log.e(TAG, e.getMessage());
} }
if (refreshDialog != null) {
refreshDialog.cancel();
}
});
} }
private class DownloadDialogListener implements private class DownloadDialogListener implements

View File

@ -6,6 +6,7 @@ import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;

View File

@ -1,6 +1,7 @@
package org.bspeice.minimalbible.activities.downloader.manager; package org.bspeice.minimalbible.activities.downloader.manager;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
@ -17,6 +18,8 @@ import javax.inject.Singleton;
import rx.Observable; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.android.schedulers.HandlerThreadScheduler;
import rx.schedulers.Schedulers;
/** /**
* Handle refreshing the list of books available as needed * Handle refreshing the list of books available as needed
@ -40,20 +43,20 @@ public class RefreshManager {
/** /**
* Do the work of kicking off the AsyncTask to refresh books, and make sure we know * Do the work of kicking off the AsyncTask to refresh books, and make sure we know
* when it's done. * when it's done.
* TODO: Should I have a better way of scheduling than Schedulers.io()?
*/ */
private void refreshModules() { private void refreshModules() {
if (availableModules == null) { if (availableModules == null) {
Handler backgroundHandler = new Handler();
availableModules = Observable.from(downloadManager.getInstallers().values()) availableModules = Observable.from(downloadManager.getInstallers().values())
.map(installer -> { .map(installer -> {
Map<Installer, List<Book>> map = new HashMap<Installer, List<Book>>(); Map<Installer, List<Book>> map = new HashMap<Installer, List<Book>>();
map.put(installer, installer.getBooks()); map.put(installer, installer.getBooks());
return map; return map;
}).observeOn(AndroidSchedulers.handlerThread(backgroundHandler)) }).subscribeOn(Schedulers.io())
.cache(); .cache();
// Set refresh complete when it is. // Set refresh complete when it is.
availableModules.subscribeOn(AndroidSchedulers.handlerThread(backgroundHandler)) availableModules.observeOn(Schedulers.io())
.subscribe((onNext) -> {}, (onError) -> {}, () -> refreshComplete.set(true)); .subscribe((onNext) -> {}, (onError) -> {}, () -> refreshComplete.set(true));
} }
} }