Fix a bug when updating categories in library

This commit is contained in:
inorichi 2015-12-29 22:57:58 +01:00
parent b0ad72afad
commit f9c13e0ee6
4 changed files with 37 additions and 51 deletions

View File

@ -29,4 +29,10 @@ public class Category implements Serializable {
c.name = name;
return c;
}
public static Category createDefault() {
Category c = create("Default");
c.id = 0;
return c;
}
}

View File

@ -8,6 +8,7 @@ import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.view.ActionMode;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -19,9 +20,11 @@ import com.afollestad.materialdialogs.MaterialDialog;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
@ -32,7 +35,6 @@ import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.library.category.CategoryFragment;
import eu.kanade.mangafeed.ui.main.MainActivity;
import eu.kanade.mangafeed.util.EventBusHook;
import nucleus.factory.RequiresPresenter;
@RequiresPresenter(LibraryPresenter.class)
@ -82,18 +84,6 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
super.onDestroyView();
}
@Override
public void onResume() {
super.onResume();
registerForStickyEvents(1);
}
@Override
public void onPause() {
unregisterForEvents();
super.onPause();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.library, menu);
@ -121,31 +111,30 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
((MainActivity) getActivity()).pushFragment(fragment);
}
@EventBusHook
public void onEventMainThread(LibraryMangasEvent event) {
List<Manga> mangasInDefaultCategory = event.getMangas().get(0);
boolean hasDefaultCategory = adapter.hasDefaultCategory();
public void onNextLibraryUpdate(Pair<List<Category>, Map<Integer, List<Manga>>> pair) {
boolean mangasInDefaultCategory = pair.second.get(0) != null;
boolean initialized = adapter.categories != null;
// If there are mangas in the default category and the adapter doesn't have it,
// create the default category
if (mangasInDefaultCategory != null && !hasDefaultCategory) {
setCategoriesWithDefault(getPresenter().categories);
if (mangasInDefaultCategory && (!initialized || !adapter.hasDefaultCategory())) {
setCategoriesWithDefault(pair.first);
}
// If there aren't mangas in the default category and the adapter have it,
// remove the default category
else if (mangasInDefaultCategory == null && hasDefaultCategory) {
setCategories(getPresenter().categories);
else if (!mangasInDefaultCategory && (!initialized || adapter.hasDefaultCategory())) {
setCategories(pair.first);
}
// Send the mangas to child fragments after the adapter is updated
EventBus.getDefault().postSticky(new LibraryMangasEvent(pair.second));
}
public void setCategoriesWithDefault(List<Category> categories) {
List<Category> actualCategories = new ArrayList<>();
private void setCategoriesWithDefault(List<Category> categories) {
List<Category> categoriesWithDefault = new ArrayList<>();
categoriesWithDefault.add(Category.createDefault());
categoriesWithDefault.addAll(categories);
Category defaultCat = Category.create("Default");
defaultCat.id = 0;
actualCategories.add(defaultCat);
actualCategories.addAll(categories);
setCategories(actualCategories);
setCategories(categoriesWithDefault);
}
private void setCategories(List<Category> categories) {

View File

@ -20,7 +20,6 @@ import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.event.LibraryMangasEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@ -33,9 +32,7 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
protected List<Category> categories;
protected List<Manga> selectedMangas;
private Subscription librarySubscription;
private static final int GET_CATEGORIES = 1;
private static final int GET_LIBRARY = 1;
@Override
protected void onCreate(Bundle savedState) {
@ -43,11 +40,11 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
selectedMangas = new ArrayList<>();
restartableLatestCache(GET_CATEGORIES,
this::getCategoriesObservable,
LibraryFragment::setCategoriesWithDefault);
restartableLatestCache(GET_LIBRARY,
this::getLibraryObservable,
LibraryFragment::onNextLibraryUpdate);
start(GET_CATEGORIES);
start(GET_LIBRARY);
}
@Override
@ -56,13 +53,15 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
super.onDestroy();
}
private Observable<Pair<List<Category>, Map<Integer, List<Manga>>>> getLibraryObservable() {
return Observable.combineLatest(getCategoriesObservable(), getLibraryMangasObservable(),
Pair::create)
.observeOn(AndroidSchedulers.mainThread());
}
public Observable<List<Category>> getCategoriesObservable() {
return db.getCategories().createObservable()
.doOnNext(categories -> {
this.categories = categories;
subscribeToLibrary();
})
.observeOn(AndroidSchedulers.mainThread());
.doOnNext(categories -> this.categories = categories);
}
private Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() {
@ -74,14 +73,6 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.toMap(pair -> pair.first, pair -> pair.second));
}
private void subscribeToLibrary() {
if (librarySubscription != null && !librarySubscription.isUnsubscribed())
return;
add(librarySubscription = getLibraryMangasObservable().subscribe(
mangas -> EventBus.getDefault().postSticky(new LibraryMangasEvent(mangas))));
}
public void setSelection(Manga manga, boolean selected) {
if (selected) {
selectedMangas.add(manga);

View File

@ -5,7 +5,7 @@
android:key="acra.enable"
android:title="@string/pref_enable_acra"
android:summary="@string/pref_acra_summary"
android:defaultValue="false"/>
android:defaultValue="true"/>
<Preference
android:key="@string/pref_version"