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; c.name = name;
return c; 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.app.Fragment;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.view.ActionMode; import android.support.v7.view.ActionMode;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -19,9 +20,11 @@ import com.afollestad.materialdialogs.MaterialDialog;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import eu.davidea.flexibleadapter.FlexibleAdapter; import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category; 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.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.library.category.CategoryFragment; import eu.kanade.mangafeed.ui.library.category.CategoryFragment;
import eu.kanade.mangafeed.ui.main.MainActivity; import eu.kanade.mangafeed.ui.main.MainActivity;
import eu.kanade.mangafeed.util.EventBusHook;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@RequiresPresenter(LibraryPresenter.class) @RequiresPresenter(LibraryPresenter.class)
@ -82,18 +84,6 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
super.onDestroyView(); super.onDestroyView();
} }
@Override
public void onResume() {
super.onResume();
registerForStickyEvents(1);
}
@Override
public void onPause() {
unregisterForEvents();
super.onPause();
}
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.library, menu); inflater.inflate(R.menu.library, menu);
@ -121,31 +111,30 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
((MainActivity) getActivity()).pushFragment(fragment); ((MainActivity) getActivity()).pushFragment(fragment);
} }
@EventBusHook public void onNextLibraryUpdate(Pair<List<Category>, Map<Integer, List<Manga>>> pair) {
public void onEventMainThread(LibraryMangasEvent event) { boolean mangasInDefaultCategory = pair.second.get(0) != null;
List<Manga> mangasInDefaultCategory = event.getMangas().get(0); boolean initialized = adapter.categories != null;
boolean hasDefaultCategory = adapter.hasDefaultCategory();
// If there are mangas in the default category and the adapter doesn't have it, // If there are mangas in the default category and the adapter doesn't have it,
// create the default category // create the default category
if (mangasInDefaultCategory != null && !hasDefaultCategory) { if (mangasInDefaultCategory && (!initialized || !adapter.hasDefaultCategory())) {
setCategoriesWithDefault(getPresenter().categories); setCategoriesWithDefault(pair.first);
} }
// If there aren't mangas in the default category and the adapter have it, // If there aren't mangas in the default category and the adapter have it,
// remove the default category // remove the default category
else if (mangasInDefaultCategory == null && hasDefaultCategory) { else if (!mangasInDefaultCategory && (!initialized || adapter.hasDefaultCategory())) {
setCategories(getPresenter().categories); 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) { private void setCategoriesWithDefault(List<Category> categories) {
List<Category> actualCategories = new ArrayList<>(); List<Category> categoriesWithDefault = new ArrayList<>();
categoriesWithDefault.add(Category.createDefault());
categoriesWithDefault.addAll(categories);
Category defaultCat = Category.create("Default"); setCategories(categoriesWithDefault);
defaultCat.id = 0;
actualCategories.add(defaultCat);
actualCategories.addAll(categories);
setCategories(actualCategories);
} }
private void setCategories(List<Category> categories) { 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.event.LibraryMangasEvent;
import eu.kanade.mangafeed.ui.base.presenter.BasePresenter; import eu.kanade.mangafeed.ui.base.presenter.BasePresenter;
import rx.Observable; import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
public class LibraryPresenter extends BasePresenter<LibraryFragment> { public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@ -33,9 +32,7 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
protected List<Category> categories; protected List<Category> categories;
protected List<Manga> selectedMangas; protected List<Manga> selectedMangas;
private Subscription librarySubscription; private static final int GET_LIBRARY = 1;
private static final int GET_CATEGORIES = 1;
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
@ -43,11 +40,11 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
selectedMangas = new ArrayList<>(); selectedMangas = new ArrayList<>();
restartableLatestCache(GET_CATEGORIES, restartableLatestCache(GET_LIBRARY,
this::getCategoriesObservable, this::getLibraryObservable,
LibraryFragment::setCategoriesWithDefault); LibraryFragment::onNextLibraryUpdate);
start(GET_CATEGORIES); start(GET_LIBRARY);
} }
@Override @Override
@ -56,13 +53,15 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
super.onDestroy(); 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() { public Observable<List<Category>> getCategoriesObservable() {
return db.getCategories().createObservable() return db.getCategories().createObservable()
.doOnNext(categories -> { .doOnNext(categories -> this.categories = categories);
this.categories = categories;
subscribeToLibrary();
})
.observeOn(AndroidSchedulers.mainThread());
} }
private Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() { private Observable<Map<Integer, List<Manga>>> getLibraryMangasObservable() {
@ -74,14 +73,6 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.toMap(pair -> pair.first, pair -> pair.second)); .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) { public void setSelection(Manga manga, boolean selected) {
if (selected) { if (selected) {
selectedMangas.add(manga); selectedMangas.add(manga);

View file

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