From f9c13e0ee622bf4579a59e39024760f573724e8a Mon Sep 17 00:00:00 2001 From: inorichi Date: Tue, 29 Dec 2015 22:57:58 +0100 Subject: [PATCH] Fix a bug when updating categories in library --- .../data/database/models/Category.java | 6 +++ .../mangafeed/ui/library/LibraryFragment.java | 47 +++++++------------ .../ui/library/LibraryPresenter.java | 33 +++++-------- app/src/main/res/xml/pref_about.xml | 2 +- 4 files changed, 37 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Category.java b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Category.java index 282c30cc2..44dcd4177 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/models/Category.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/models/Category.java @@ -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; + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java index 0aa125c4b..c64f7d386 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryFragment.java @@ -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 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 ((MainActivity) getActivity()).pushFragment(fragment); } - @EventBusHook - public void onEventMainThread(LibraryMangasEvent event) { - List mangasInDefaultCategory = event.getMangas().get(0); - boolean hasDefaultCategory = adapter.hasDefaultCategory(); + public void onNextLibraryUpdate(Pair, Map>> 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 categories) { - List actualCategories = new ArrayList<>(); + private void setCategoriesWithDefault(List categories) { + List 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 categories) { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java index cc13a8a4c..cb67d8cda 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java @@ -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 { @@ -33,9 +32,7 @@ public class LibraryPresenter extends BasePresenter { protected List categories; protected List 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 { 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 { super.onDestroy(); } + private Observable, Map>>> getLibraryObservable() { + return Observable.combineLatest(getCategoriesObservable(), getLibraryMangasObservable(), + Pair::create) + .observeOn(AndroidSchedulers.mainThread()); + } + public Observable> getCategoriesObservable() { return db.getCategories().createObservable() - .doOnNext(categories -> { - this.categories = categories; - subscribeToLibrary(); - }) - .observeOn(AndroidSchedulers.mainThread()); + .doOnNext(categories -> this.categories = categories); } private Observable>> getLibraryMangasObservable() { @@ -74,14 +73,6 @@ public class LibraryPresenter extends BasePresenter { .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); diff --git a/app/src/main/res/xml/pref_about.xml b/app/src/main/res/xml/pref_about.xml index baa203085..a5a4574f6 100644 --- a/app/src/main/res/xml/pref_about.xml +++ b/app/src/main/res/xml/pref_about.xml @@ -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"/>