Hide default category if it doesn't have any manga

This commit is contained in:
inorichi 2015-12-29 21:19:41 +01:00
parent 1360a90bf9
commit 62535c77ae
5 changed files with 89 additions and 26 deletions

View file

@ -59,11 +59,19 @@ public class BaseActivity extends AppCompatActivity {
}
public void registerForStickyEvents() {
EventBus.getDefault().registerSticky(this);
registerForStickyEvents(0);
}
public void registerForStickyEvents(int priority) {
EventBus.getDefault().registerSticky(this, priority);
}
public void registerForEvents() {
EventBus.getDefault().register(this);
registerForEvents(0);
}
public void registerForEvents(int priority) {
EventBus.getDefault().register(this, priority);
}
public void unregisterForEvents() {

View file

@ -20,11 +20,19 @@ public class BaseFragment extends Fragment {
}
public void registerForStickyEvents() {
EventBus.getDefault().registerSticky(this);
registerForStickyEvents(0);
}
public void registerForStickyEvents(int priority) {
EventBus.getDefault().registerSticky(this, priority);
}
public void registerForEvents() {
EventBus.getDefault().register(this);
registerForEvents(0);
}
public void registerForEvents(int priority) {
EventBus.getDefault().register(this, priority);
}
public void unregisterForEvents() {

View file

@ -42,4 +42,8 @@ public class LibraryAdapter extends SmartFragmentStatePagerAdapter {
}
}
public boolean hasDefaultCategory() {
return categories.get(0).id == 0;
}
}

View file

@ -27,10 +27,12 @@ import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.sync.LibraryUpdateService;
import eu.kanade.mangafeed.event.LibraryMangasEvent;
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)
@ -40,7 +42,7 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
TabLayout tabs;
AppBarLayout appBar;
@Bind(R.id.view_pager) ViewPager categoriesPager;
@Bind(R.id.view_pager) ViewPager viewPager;
protected LibraryAdapter adapter;
private ActionMode actionMode;
@ -69,7 +71,7 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
adapter = new LibraryAdapter(getChildFragmentManager());
categoriesPager.setAdapter(adapter);
viewPager.setAdapter(adapter);
return view;
}
@ -80,6 +82,18 @@ 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);
@ -107,7 +121,23 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
((MainActivity) getActivity()).pushFragment(fragment);
}
public void onNextCategories(List<Category> categories) {
@EventBusHook
public void onEventMainThread(LibraryMangasEvent event) {
List<Manga> mangasInDefaultCategory = event.getMangas().get(0);
boolean hasDefaultCategory = adapter.hasDefaultCategory();
// 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 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);
}
}
public void setCategoriesWithDefault(List<Category> categories) {
List<Category> actualCategories = new ArrayList<>();
Category defaultCat = Category.create("Default");
@ -115,10 +145,13 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
actualCategories.add(defaultCat);
actualCategories.addAll(categories);
adapter.setCategories(actualCategories);
tabs.setupWithViewPager(categoriesPager);
setCategories(actualCategories);
}
tabs.setVisibility(actualCategories.size() == 1 ? View.GONE : View.VISIBLE);
private void setCategories(List<Category> categories) {
adapter.setCategories(categories);
tabs.setupWithViewPager(viewPager);
tabs.setVisibility(categories.size() == 1 ? View.GONE : View.VISIBLE);
}
public void setContextTitle(int count) {
@ -143,6 +176,10 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
case R.id.action_move_to_category:
moveMangasToCategories(getPresenter().selectedMangas);
return true;
case R.id.action_delete:
getPresenter().deleteMangas();
destroyActionModeIfNeeded();
return true;
}
return false;
}
@ -172,7 +209,6 @@ public class LibraryFragment extends BaseRxFragment<LibraryPresenter>
.positiveText(R.string.button_ok)
.negativeText(R.string.button_cancel)
.show();
}
@Nullable

View file

@ -20,8 +20,8 @@ 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;
import rx.schedulers.Schedulers;
public class LibraryPresenter extends BasePresenter<LibraryFragment> {
@ -33,6 +33,8 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
protected List<Category> categories;
protected List<Manga> selectedMangas;
private Subscription librarySubscription;
private static final int GET_CATEGORIES = 1;
@Override
@ -43,14 +45,9 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
restartableLatestCache(GET_CATEGORIES,
this::getCategoriesObservable,
LibraryFragment::onNextCategories);
LibraryFragment::setCategoriesWithDefault);
start(GET_CATEGORIES);
add(getLibraryMangasObservable()
.subscribe(mangas ->
EventBus.getDefault().postSticky(new LibraryMangasEvent(mangas))));
}
@Override
@ -61,7 +58,10 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
public Observable<List<Category>> getCategoriesObservable() {
return db.getCategories().createObservable()
.doOnNext(categories -> this.categories = categories)
.doOnNext(categories -> {
this.categories = categories;
subscribeToLibrary();
})
.observeOn(AndroidSchedulers.mainThread());
}
@ -74,13 +74,12 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
.toMap(pair -> pair.first, pair -> pair.second));
}
public void deleteMangas(Observable<Manga> selectedMangas) {
add(selectedMangas
.subscribeOn(Schedulers.io())
.doOnNext(manga -> manga.favorite = false)
.toList()
.flatMap(mangas -> db.insertMangas(mangas).createObservable())
.subscribe());
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) {
@ -102,6 +101,14 @@ public class LibraryPresenter extends BasePresenter<LibraryFragment> {
return names;
}
public void deleteMangas() {
for (Manga manga : selectedMangas) {
manga.favorite = false;
}
db.insertMangas(selectedMangas).executeAsBlocking();
}
public void moveMangasToCategories(Integer[] positions, List<Manga> mangas) {
List<Category> categoriesToAdd = new ArrayList<>();
for (Integer index : positions) {