Simplify chapter's popup logic

This commit is contained in:
inorichi 2015-12-01 15:47:34 +01:00
parent 04a61edb38
commit 0f5cf6a748
3 changed files with 48 additions and 33 deletions

View File

@ -48,7 +48,7 @@ public class ChaptersAdapter extends FlexibleAdapter<ChaptersHolder, Chapter> {
void onListItemLongClick(int position);
}
public ChaptersFragment getMangaChaptersFragment() {
public ChaptersFragment getChaptersFragment() {
return (ChaptersFragment) fragment;
}
}

View File

@ -6,7 +6,6 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
@ -24,11 +23,11 @@ import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Chapter;
import eu.kanade.mangafeed.data.download.DownloadService;
import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
import eu.kanade.mangafeed.ui.reader.ReaderActivity;
import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
import nucleus.factory.RequiresPresenter;
import rx.Observable;
@ -149,23 +148,15 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_select_all:
adapter.selectAll();
return true;
return onSelectAll();
case R.id.action_mark_as_read:
getPresenter().markChaptersRead(getSelectedChapters(), true);
return true;
return onMarkAsRead(getSelectedChapters());
case R.id.action_mark_as_unread:
getPresenter().markChaptersRead(getSelectedChapters(), false);
return true;
return onMarkAsUnread(getSelectedChapters());
case R.id.action_download:
DownloadService.start(getActivity());
getPresenter().downloadChapters(getSelectedChapters());
closeActionMode();
return true;
return onDownload(getSelectedChapters());
case R.id.action_delete:
getPresenter().deleteChapters(getSelectedChapters());
closeActionMode();
return true;
return onDelete(getSelectedChapters());
}
return false;
}
@ -187,6 +178,36 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
actionMode.finish();
}
protected boolean onSelectAll() {
adapter.selectAll();
setContextTitle(adapter.getSelectedItemCount());
actionMode.invalidate();
return true;
}
protected boolean onMarkAsRead(Observable<Chapter> chapters) {
getPresenter().markChaptersRead(chapters, true);
return true;
}
protected boolean onMarkAsUnread(Observable<Chapter> chapters) {
getPresenter().markChaptersRead(chapters, false);
return true;
}
protected boolean onDownload(Observable<Chapter> chapters) {
DownloadService.start(getActivity());
getPresenter().downloadChapters(chapters);
closeActionMode();
return true;
}
protected boolean onDelete(Observable<Chapter> chapters) {
getPresenter().deleteChapters(chapters);
closeActionMode();
return true;
}
@Override
public boolean onListItemClick(int position) {
if (actionMode != null && adapter.getMode() == ChaptersAdapter.MODE_MULTI) {

View File

@ -2,11 +2,9 @@ package eu.kanade.mangafeed.ui.manga.chapter;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
@ -17,7 +15,6 @@ import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Chapter;
import eu.kanade.mangafeed.data.download.DownloadService;
import rx.Observable;
public class ChaptersHolder extends RecyclerView.ViewHolder implements
@ -58,14 +55,14 @@ public class ChaptersHolder extends RecyclerView.ViewHolder implements
title.setTextColor(ContextCompat.getColor(context, R.color.primary_text));
}
if (chapter.last_page_read > 0 && !chapter.read) {
if (!chapter.read && chapter.last_page_read > 0) {
pages.setText(context.getString(R.string.chapter_progress, chapter.last_page_read + 1));
} else {
pages.setText("");
}
if (chapter.downloaded == Chapter.UNKNOWN) {
adapter.getMangaChaptersFragment().getPresenter().checkIsChapterDownloaded(chapter);
adapter.getChaptersFragment().getPresenter().checkIsChapterDownloaded(chapter);
}
if (chapter.downloaded == Chapter.DOWNLOADED) {
downloadText.setVisibility(View.VISIBLE);
@ -97,27 +94,24 @@ public class ChaptersHolder extends RecyclerView.ViewHolder implements
private void showPopupMenu(View view) {
// Create a PopupMenu, giving it the clicked view for an anchor
PopupMenu popup = new PopupMenu(adapter.getMangaChaptersFragment().getActivity(), view);
PopupMenu popup = new PopupMenu(adapter.getChaptersFragment().getActivity(), view);
// Inflate our menu resource into the PopupMenu's Menu
popup.getMenuInflater().inflate(R.menu.chapter_single, popup.getMenu());
// Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener(menuItem -> {
Observable<Chapter> chapter = Observable.just(item);
switch (menuItem.getItemId()) {
case R.id.action_mark_as_read:
adapter.getMangaChaptersFragment().getPresenter().markChaptersRead(Observable.just(item), true);
return true;
return adapter.getChaptersFragment().onMarkAsRead(chapter);
case R.id.action_mark_as_unread:
adapter.getMangaChaptersFragment().getPresenter().markChaptersRead(Observable.just(item), false);
return true;
return adapter.getChaptersFragment().onMarkAsUnread(chapter);
case R.id.action_download:
DownloadService.start(adapter.getMangaChaptersFragment().getActivity());
adapter.getMangaChaptersFragment().getPresenter().downloadChapters(Observable.just(item));
return true;
return adapter.getChaptersFragment().onDownload(chapter);
case R.id.action_delete:
adapter.getMangaChaptersFragment().getPresenter().deleteChapters(Observable.just(item));
return true;
return adapter.getChaptersFragment().onDelete(chapter);
}
return false;
});