|
@@ -6,6 +6,7 @@ import android.support.v4.content.res.ResourcesCompat;
|
|
|
import android.support.v7.view.ActionMode;
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
+import android.support.v7.widget.helper.ItemTouchHelper;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.Menu;
|
|
|
import android.view.MenuItem;
|
|
@@ -22,6 +23,7 @@ import eu.kanade.mangafeed.R;
|
|
|
import eu.kanade.mangafeed.data.database.models.Category;
|
|
|
import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
|
|
|
import eu.kanade.mangafeed.ui.base.adapter.FlexibleViewHolder;
|
|
|
+import eu.kanade.mangafeed.ui.base.adapter.OnStartDragListener;
|
|
|
import eu.kanade.mangafeed.ui.base.fragment.BaseRxFragment;
|
|
|
import eu.kanade.mangafeed.ui.decoration.DividerItemDecoration;
|
|
|
import eu.kanade.mangafeed.ui.library.LibraryCategoryAdapter;
|
|
@@ -29,14 +31,15 @@ import nucleus.factory.RequiresPresenter;
|
|
|
import rx.Observable;
|
|
|
|
|
|
@RequiresPresenter(CategoryPresenter.class)
|
|
|
-public class CategoryFragment extends BaseRxFragment<CategoryPresenter>
|
|
|
- implements ActionMode.Callback, FlexibleViewHolder.OnListItemClickListener {
|
|
|
+public class CategoryFragment extends BaseRxFragment<CategoryPresenter> implements
|
|
|
+ ActionMode.Callback, FlexibleViewHolder.OnListItemClickListener, OnStartDragListener {
|
|
|
|
|
|
@Bind(R.id.categories_list) RecyclerView recycler;
|
|
|
@Bind(R.id.fab) FloatingActionButton fab;
|
|
|
|
|
|
private CategoryAdapter adapter;
|
|
|
private ActionMode actionMode;
|
|
|
+ private ItemTouchHelper touchHelper;
|
|
|
|
|
|
public static CategoryFragment newInstance() {
|
|
|
return new CategoryFragment();
|
|
@@ -56,12 +59,17 @@ public class CategoryFragment extends BaseRxFragment<CategoryPresenter>
|
|
|
recycler.addItemDecoration(new DividerItemDecoration(
|
|
|
ResourcesCompat.getDrawable(getResources(), R.drawable.line_divider, null)));
|
|
|
|
|
|
+ // Touch helper to drag and reorder categories
|
|
|
+ touchHelper = new ItemTouchHelper(new CategoryItemTouchHelper(adapter));
|
|
|
+ touchHelper.attachToRecyclerView(recycler);
|
|
|
+
|
|
|
fab.setOnClickListener(v -> {
|
|
|
new MaterialDialog.Builder(getActivity())
|
|
|
.title(R.string.action_add_category)
|
|
|
.input(R.string.name, 0, false, (dialog, input) -> {
|
|
|
getPresenter().createCategory(input.toString());
|
|
|
- }).show();
|
|
|
+ })
|
|
|
+ .show();
|
|
|
});
|
|
|
|
|
|
return view;
|
|
@@ -105,6 +113,8 @@ public class CategoryFragment extends BaseRxFragment<CategoryPresenter>
|
|
|
} else {
|
|
|
setContextTitle(count);
|
|
|
actionMode.invalidate();
|
|
|
+ MenuItem editItem = actionMode.getMenu().findItem(R.id.action_edit);
|
|
|
+ editItem.setVisible(count == 1);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -128,7 +138,10 @@ public class CategoryFragment extends BaseRxFragment<CategoryPresenter>
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
switch (item.getItemId()) {
|
|
|
case R.id.action_delete:
|
|
|
- getPresenter().deleteCategories(getSelectedCategories());
|
|
|
+ deleteCategories(getSelectedCategories());
|
|
|
+ return true;
|
|
|
+ case R.id.action_edit:
|
|
|
+ editCategory(getSelectedCategories().get(0));
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
@@ -147,4 +160,22 @@ public class CategoryFragment extends BaseRxFragment<CategoryPresenter>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void deleteCategories(List<Category> categories) {
|
|
|
+ getPresenter().deleteCategories(categories);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void editCategory(Category category) {
|
|
|
+ new MaterialDialog.Builder(getActivity())
|
|
|
+ .title(R.string.action_rename_category)
|
|
|
+ .input(getString(R.string.name), category.name, false, (dialog, input) -> {
|
|
|
+ getPresenter().renameCategory(category, input.toString());
|
|
|
+ })
|
|
|
+ .show();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
|
|
|
+ touchHelper.startDrag(viewHolder);
|
|
|
+ }
|
|
|
+
|
|
|
}
|