convert category information to an enum
This commit is contained in:
parent
0acbba651c
commit
fbffb4ef8a
7 changed files with 73 additions and 154 deletions
|
|
@ -14,20 +14,20 @@ import androidx.recyclerview.widget.RecyclerView;
|
|||
import androidx.recyclerview.widget.ItemTouchHelper;
|
||||
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.model.CategoryInfo;
|
||||
import com.dkanada.gramophone.model.Category;
|
||||
import com.dkanada.gramophone.helper.SwipeAndDragHelper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
|
||||
private List<CategoryInfo> categories;
|
||||
private List<Category> categories;
|
||||
private ItemTouchHelper touchHelper;
|
||||
|
||||
public CategoryAdapter(List<CategoryInfo> categories) {
|
||||
public CategoryAdapter(List<Category> categories) {
|
||||
this.categories = categories;
|
||||
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(this);
|
||||
touchHelper = new ItemTouchHelper(swipeAndDragHelper);
|
||||
this.touchHelper = new ItemTouchHelper(swipeAndDragHelper);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
@ -40,17 +40,17 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHo
|
|||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull CategoryAdapter.ViewHolder holder, int position) {
|
||||
CategoryInfo category = categories.get(position);
|
||||
Category category = categories.get(position);
|
||||
|
||||
holder.checkBox.setChecked(category.visible);
|
||||
holder.title.setText(holder.title.getResources().getString(category.category.stringRes));
|
||||
holder.checkBox.setChecked(category.select);
|
||||
holder.title.setText(holder.title.getResources().getString(category.title));
|
||||
|
||||
holder.itemView.setOnClickListener(v -> {
|
||||
if (!(category.visible && isLastCheckedCategory(category))) {
|
||||
category.visible = !category.visible;
|
||||
holder.checkBox.setChecked(category.visible);
|
||||
} else {
|
||||
if (category.select && categories.stream().filter(c -> c.select).count() == 1) {
|
||||
Toast.makeText(holder.itemView.getContext(), R.string.you_have_to_select_at_least_one_category, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
category.select = !category.select;
|
||||
holder.checkBox.setChecked(category.select);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -68,16 +68,16 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHo
|
|||
return categories.size();
|
||||
}
|
||||
|
||||
public void setCategories(List<CategoryInfo> categories) {
|
||||
public void setCategories(List<Category> categories) {
|
||||
this.categories = categories;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewMoved(int oldPosition, int newPosition) {
|
||||
CategoryInfo categoryInfo = categories.get(oldPosition);
|
||||
Category category = categories.get(oldPosition);
|
||||
categories.remove(oldPosition);
|
||||
categories.add(newPosition, categoryInfo);
|
||||
categories.add(newPosition, category);
|
||||
notifyItemMoved(oldPosition, newPosition);
|
||||
}
|
||||
|
||||
|
|
@ -85,20 +85,10 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHo
|
|||
touchHelper.attachToRecyclerView(recyclerView);
|
||||
}
|
||||
|
||||
public List<CategoryInfo> getCategories() {
|
||||
public List<Category> getCategories() {
|
||||
return categories;
|
||||
}
|
||||
|
||||
private boolean isLastCheckedCategory(CategoryInfo category) {
|
||||
if (category.visible) {
|
||||
for (CategoryInfo c : categories) {
|
||||
if (c != category && c.visible) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
public CheckBox checkBox;
|
||||
public TextView title;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import androidx.fragment.app.FragmentManager;
|
|||
import androidx.fragment.app.FragmentPagerAdapter;
|
||||
|
||||
import com.dkanada.gramophone.fragments.mainactivity.library.pager.FavoritesFragment;
|
||||
import com.dkanada.gramophone.model.CategoryInfo;
|
||||
import com.dkanada.gramophone.model.Category;
|
||||
import com.dkanada.gramophone.fragments.mainactivity.library.pager.AlbumsFragment;
|
||||
import com.dkanada.gramophone.fragments.mainactivity.library.pager.ArtistsFragment;
|
||||
import com.dkanada.gramophone.fragments.mainactivity.library.pager.GenresFragment;
|
||||
|
|
@ -23,7 +23,7 @@ import java.lang.ref.WeakReference;
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
|
||||
|
||||
|
|
@ -36,23 +36,25 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
|
|||
|
||||
public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) {
|
||||
super(fragmentManager);
|
||||
|
||||
mContext = context;
|
||||
setCategoryInfos(PreferenceUtil.getInstance(context).getCategories());
|
||||
setCategories(PreferenceUtil.getInstance(context).getCategories()
|
||||
.stream()
|
||||
.filter(category -> category.select)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public void setCategoryInfos(@NonNull List<CategoryInfo> categoryInfos) {
|
||||
public void setCategories(@NonNull List<Category> categories) {
|
||||
mHolderList.clear();
|
||||
|
||||
for (CategoryInfo categoryInfo : categoryInfos) {
|
||||
if (categoryInfo.visible) {
|
||||
MusicFragments fragment = MusicFragments.valueOf(categoryInfo.category.toString());
|
||||
Holder holder = new Holder();
|
||||
holder.mClassName = fragment.getFragmentClass().getName();
|
||||
holder.title = mContext.getResources()
|
||||
.getString(categoryInfo.category.stringRes)
|
||||
.toUpperCase(Locale.getDefault());
|
||||
mHolderList.add(holder);
|
||||
}
|
||||
for (Category category : categories) {
|
||||
MusicFragments fragment = MusicFragments.valueOf(category.toString());
|
||||
Holder holder = new Holder();
|
||||
|
||||
holder.mClassName = fragment.getFragmentClass().getName();
|
||||
holder.title = mContext.getResources().getString(category.title).toUpperCase();
|
||||
|
||||
mHolderList.add(holder);
|
||||
}
|
||||
|
||||
alignCache();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue