minor changes to category adapter

This commit is contained in:
dkanada 2020-09-08 12:25:06 +09:00
commit e06b5bc94e
3 changed files with 39 additions and 38 deletions

View file

@ -18,35 +18,35 @@ import com.dkanada.gramophone.helper.SwipeAndDragHelper;
import java.util.List; import java.util.List;
public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract { public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
private List<CategoryInfo> categoryInfos; private List<CategoryInfo> categories;
private ItemTouchHelper touchHelper; private ItemTouchHelper touchHelper;
public CategoryInfoAdapter(List<CategoryInfo> categoryInfos) { public CategoryAdapter(List<CategoryInfo> categories) {
this.categoryInfos = categoryInfos; this.categories = categories;
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(this); SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(this);
touchHelper = new ItemTouchHelper(swipeAndDragHelper); touchHelper = new ItemTouchHelper(swipeAndDragHelper);
} }
@Override @Override
@NonNull @NonNull
public CategoryInfoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public CategoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.preference_dialog_category_item, parent, false); View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.preference_dialog_category_item, parent, false);
return new ViewHolder(view); return new ViewHolder(view);
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@Override @Override
public void onBindViewHolder(@NonNull CategoryInfoAdapter.ViewHolder holder, int position) { public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
CategoryInfo categoryInfo = categoryInfos.get(position); CategoryInfo category = categories.get(position);
holder.checkBox.setChecked(categoryInfo.visible); holder.checkBox.setChecked(category.visible);
holder.title.setText(holder.title.getResources().getString(categoryInfo.category.stringRes)); holder.title.setText(holder.title.getResources().getString(category.category.stringRes));
holder.itemView.setOnClickListener(v -> { holder.itemView.setOnClickListener(v -> {
if (!(categoryInfo.visible && isLastCheckedCategory(categoryInfo))) { if (!(category.visible && isLastCheckedCategory(category))) {
categoryInfo.visible = !categoryInfo.visible; category.visible = !category.visible;
holder.checkBox.setChecked(categoryInfo.visible); holder.checkBox.setChecked(category.visible);
} else { } else {
Toast.makeText(holder.itemView.getContext(), R.string.you_have_to_select_at_least_one_category, Toast.LENGTH_SHORT).show(); Toast.makeText(holder.itemView.getContext(), R.string.you_have_to_select_at_least_one_category, Toast.LENGTH_SHORT).show();
} }
@ -56,6 +56,7 @@ public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapte
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
touchHelper.startDrag(holder); touchHelper.startDrag(holder);
} }
return false; return false;
} }
); );
@ -63,19 +64,19 @@ public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapte
@Override @Override
public int getItemCount() { public int getItemCount() {
return categoryInfos.size(); return categories.size();
} }
public void setCategoryInfos(List<CategoryInfo> categoryInfos) { public void setCategories(List<CategoryInfo> categories) {
this.categoryInfos = categoryInfos; this.categories = categories;
notifyDataSetChanged(); notifyDataSetChanged();
} }
@Override @Override
public void onViewMoved(int oldPosition, int newPosition) { public void onViewMoved(int oldPosition, int newPosition) {
CategoryInfo categoryInfo = categoryInfos.get(oldPosition); CategoryInfo categoryInfo = categories.get(oldPosition);
categoryInfos.remove(oldPosition); categories.remove(oldPosition);
categoryInfos.add(newPosition, categoryInfo); categories.add(newPosition, categoryInfo);
notifyItemMoved(oldPosition, newPosition); notifyItemMoved(oldPosition, newPosition);
} }
@ -83,16 +84,17 @@ public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapte
touchHelper.attachToRecyclerView(recyclerView); touchHelper.attachToRecyclerView(recyclerView);
} }
public List<CategoryInfo> getCategoryInfos() { public List<CategoryInfo> getCategories() {
return categoryInfos; return categories;
} }
private boolean isLastCheckedCategory(CategoryInfo categoryInfo) { private boolean isLastCheckedCategory(CategoryInfo category) {
if (categoryInfo.visible) { if (category.visible) {
for (CategoryInfo c : categoryInfos) { for (CategoryInfo c : categories) {
if (c != categoryInfo && c.visible) return false; if (c != category && c.visible) return false;
} }
} }
return true; return true;
} }

View file

@ -10,7 +10,7 @@ import android.view.View;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
import com.dkanada.gramophone.R; import com.dkanada.gramophone.R;
import com.dkanada.gramophone.adapter.CategoryInfoAdapter; import com.dkanada.gramophone.adapter.CategoryAdapter;
import com.dkanada.gramophone.model.CategoryInfo; import com.dkanada.gramophone.model.CategoryInfo;
import com.dkanada.gramophone.util.PreferenceUtil; import com.dkanada.gramophone.util.PreferenceUtil;
@ -22,21 +22,21 @@ public class CategoryPreferenceDialog extends DialogFragment {
return new CategoryPreferenceDialog(); return new CategoryPreferenceDialog();
} }
private CategoryInfoAdapter adapter; private CategoryAdapter adapter;
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.preference_dialog_category, null); View view = getActivity().getLayoutInflater().inflate(R.layout.preference_dialog_category, null);
List<CategoryInfo> categoryInfos; List<CategoryInfo> categories;
if (savedInstanceState != null) { if (savedInstanceState != null) {
categoryInfos = savedInstanceState.getParcelableArrayList(PreferenceUtil.CATEGORIES); categories = savedInstanceState.getParcelableArrayList(PreferenceUtil.CATEGORIES);
} else { } else {
categoryInfos = PreferenceUtil.getInstance(getContext()).getCategories(); categories = PreferenceUtil.getInstance(getContext()).getCategories();
} }
adapter = new CategoryInfoAdapter(categoryInfos); adapter = new CategoryAdapter(categories);
RecyclerView recyclerView = view.findViewById(R.id.recycler_view); RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
@ -51,19 +51,19 @@ public class CategoryPreferenceDialog extends DialogFragment {
.negativeText(android.R.string.cancel) .negativeText(android.R.string.cancel)
.neutralText(R.string.reset_action) .neutralText(R.string.reset_action)
.autoDismiss(false) .autoDismiss(false)
.onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultCategories())) .onNeutral((dialog, action) -> adapter.setCategories(PreferenceUtil.getInstance(getContext()).getDefaultCategories()))
.onNegative((dialog, action) -> dismiss()) .onNegative((dialog, action) -> dismiss())
.onPositive((dialog, action) -> { .onPositive((dialog, action) -> {
updateCategories(adapter.getCategoryInfos()); updateCategories(adapter.getCategories());
dismiss(); dismiss();
}) })
.build(); .build();
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelableArrayList(PreferenceUtil.CATEGORIES, new ArrayList<>(adapter.getCategoryInfos())); outState.putParcelableArrayList(PreferenceUtil.CATEGORIES, new ArrayList<>(adapter.getCategories()));
} }
private void updateCategories(List<CategoryInfo> categories) { private void updateCategories(List<CategoryInfo> categories) {
@ -74,9 +74,8 @@ public class CategoryPreferenceDialog extends DialogFragment {
private int getSelected(List<CategoryInfo> categories) { private int getSelected(List<CategoryInfo> categories) {
int selected = 0; int selected = 0;
for (CategoryInfo categoryInfo : categories) { for (CategoryInfo category : categories) {
if (categoryInfo.visible) if (category.visible) selected++;
selected++;
} }
return selected; return selected;

View file

@ -5,7 +5,7 @@
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference <com.kabouzeid.appthemehelper.common.prefs.supportv7.ATESwitchPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="false" android:defaultValue="true"
android:key="classic_notification" android:key="classic_notification"
android:summary="@string/pref_summary_classic_notification" android:summary="@string/pref_summary_classic_notification"
android:title="@string/pref_title_classic_notification" /> android:title="@string/pref_title_classic_notification" />