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;
public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
private List<CategoryInfo> categoryInfos;
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
private List<CategoryInfo> categories;
private ItemTouchHelper touchHelper;
public CategoryInfoAdapter(List<CategoryInfo> categoryInfos) {
this.categoryInfos = categoryInfos;
public CategoryAdapter(List<CategoryInfo> categories) {
this.categories = categories;
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(this);
touchHelper = new ItemTouchHelper(swipeAndDragHelper);
}
@Override
@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);
return new ViewHolder(view);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(@NonNull CategoryInfoAdapter.ViewHolder holder, int position) {
CategoryInfo categoryInfo = categoryInfos.get(position);
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
CategoryInfo category = categories.get(position);
holder.checkBox.setChecked(categoryInfo.visible);
holder.title.setText(holder.title.getResources().getString(categoryInfo.category.stringRes));
holder.checkBox.setChecked(category.visible);
holder.title.setText(holder.title.getResources().getString(category.category.stringRes));
holder.itemView.setOnClickListener(v -> {
if (!(categoryInfo.visible && isLastCheckedCategory(categoryInfo))) {
categoryInfo.visible = !categoryInfo.visible;
holder.checkBox.setChecked(categoryInfo.visible);
if (!(category.visible && isLastCheckedCategory(category))) {
category.visible = !category.visible;
holder.checkBox.setChecked(category.visible);
} else {
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) {
touchHelper.startDrag(holder);
}
return false;
}
);
@ -63,19 +64,19 @@ public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapte
@Override
public int getItemCount() {
return categoryInfos.size();
return categories.size();
}
public void setCategoryInfos(List<CategoryInfo> categoryInfos) {
this.categoryInfos = categoryInfos;
public void setCategories(List<CategoryInfo> categories) {
this.categories = categories;
notifyDataSetChanged();
}
@Override
public void onViewMoved(int oldPosition, int newPosition) {
CategoryInfo categoryInfo = categoryInfos.get(oldPosition);
categoryInfos.remove(oldPosition);
categoryInfos.add(newPosition, categoryInfo);
CategoryInfo categoryInfo = categories.get(oldPosition);
categories.remove(oldPosition);
categories.add(newPosition, categoryInfo);
notifyItemMoved(oldPosition, newPosition);
}
@ -83,16 +84,17 @@ public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapte
touchHelper.attachToRecyclerView(recyclerView);
}
public List<CategoryInfo> getCategoryInfos() {
return categoryInfos;
public List<CategoryInfo> getCategories() {
return categories;
}
private boolean isLastCheckedCategory(CategoryInfo categoryInfo) {
if (categoryInfo.visible) {
for (CategoryInfo c : categoryInfos) {
if (c != categoryInfo && c.visible) return false;
private boolean isLastCheckedCategory(CategoryInfo category) {
if (category.visible) {
for (CategoryInfo c : categories) {
if (c != category && c.visible) return false;
}
}
return true;
}

View file

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

View file

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