Clean up
This commit is contained in:
parent
30dd66e788
commit
739e565bc4
7 changed files with 107 additions and 159 deletions
|
|
@ -11,37 +11,39 @@ import android.widget.CheckBox;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.model.Category;
|
||||
import com.kabouzeid.gramophone.model.CategoryInfo;
|
||||
import com.kabouzeid.gramophone.util.SwipeAndDragHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
|
||||
private ArrayList<Category> categories;
|
||||
public class CategoryInfoAdapter extends RecyclerView.Adapter<CategoryInfoAdapter.ViewHolder> implements SwipeAndDragHelper.ActionCompletionContract {
|
||||
private ArrayList<CategoryInfo> categoryInfos;
|
||||
private ItemTouchHelper touchHelper;
|
||||
|
||||
public CategoryAdapter(ArrayList<Category> categories) {
|
||||
this.categories = categories;
|
||||
public CategoryInfoAdapter(ArrayList<CategoryInfo> categoryInfos) {
|
||||
this.categoryInfos = categoryInfos;
|
||||
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(this);
|
||||
touchHelper = new ItemTouchHelper(swipeAndDragHelper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
public CategoryInfoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.preference_dialog_library_categories_listitem, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
public void onBindViewHolder(CategoryAdapter.ViewHolder holder, int position) {
|
||||
Category category = categories.get(position);
|
||||
public void onBindViewHolder(CategoryInfoAdapter.ViewHolder holder, int position) {
|
||||
CategoryInfo categoryInfo = categoryInfos.get(position);
|
||||
|
||||
holder.checkBox.setChecked(category.visible);
|
||||
holder.title.setText(holder.title.getResources().getString(category.id.key));
|
||||
holder.checkBox.setChecked(categoryInfo.visible);
|
||||
holder.title.setText(holder.title.getResources().getString(categoryInfo.category.stringRes));
|
||||
|
||||
holder.itemView.setOnClickListener(v -> {
|
||||
if (!(category.visible && isLastCheckedCategory(category))) {
|
||||
category.visible = !category.visible;
|
||||
holder.checkBox.setChecked(category.visible);
|
||||
if (!(categoryInfo.visible && isLastCheckedCategory(categoryInfo))) {
|
||||
categoryInfo.visible = !categoryInfo.visible;
|
||||
holder.checkBox.setChecked(categoryInfo.visible);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -56,35 +58,35 @@ public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHo
|
|||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return categories.size();
|
||||
return categoryInfos.size();
|
||||
}
|
||||
|
||||
|
||||
public void setCategories(ArrayList<Category> categories) {
|
||||
this.categories = categories;
|
||||
public void setCategoryInfos(ArrayList<CategoryInfo> categoryInfos) {
|
||||
this.categoryInfos = categoryInfos;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewMoved(int oldPosition, int newPosition) {
|
||||
Category category = categories.get(oldPosition);
|
||||
categories.remove(oldPosition);
|
||||
categories.add(newPosition, category);
|
||||
CategoryInfo categoryInfo = categoryInfos.get(oldPosition);
|
||||
categoryInfos.remove(oldPosition);
|
||||
categoryInfos.add(newPosition, categoryInfo);
|
||||
notifyItemMoved(oldPosition, newPosition);
|
||||
}
|
||||
|
||||
public void setTouchHelper(ItemTouchHelper touchHelper) {
|
||||
this.touchHelper = touchHelper;
|
||||
public void attachToRecyclerView(RecyclerView recyclerView) {
|
||||
touchHelper.attachToRecyclerView(recyclerView);
|
||||
}
|
||||
|
||||
public ArrayList<Category> getCategories() {
|
||||
return categories;
|
||||
public ArrayList<CategoryInfo> getCategoryInfos() {
|
||||
return categoryInfos;
|
||||
}
|
||||
|
||||
private boolean isLastCheckedCategory(Category category) {
|
||||
if (category.visible) {
|
||||
for (Category c : categories) {
|
||||
if (c != category && c.visible) return false;
|
||||
private boolean isLastCheckedCategory(CategoryInfo categoryInfo) {
|
||||
if (categoryInfo.visible) {
|
||||
for (CategoryInfo c : categoryInfos) {
|
||||
if (c != categoryInfo && c.visible) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
@ -9,8 +9,7 @@ import android.support.v4.app.FragmentPagerAdapter;
|
|||
import android.util.SparseArray;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.model.Category;
|
||||
import com.kabouzeid.gramophone.model.CategoryInfo;
|
||||
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.pager.AlbumsFragment;
|
||||
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.pager.ArtistsFragment;
|
||||
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.pager.GenresFragment;
|
||||
|
|
@ -36,20 +35,19 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
|
|||
public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) {
|
||||
super(fragmentManager);
|
||||
mContext = context;
|
||||
setCategories(PreferenceUtil.getInstance(context).getLibraryCategories());
|
||||
setCategoryInfos(PreferenceUtil.getInstance(context).getLibraryCategoryInfos());
|
||||
}
|
||||
|
||||
public void setCategories(@NonNull ArrayList<Category> categories) {
|
||||
public void setCategoryInfos(@NonNull ArrayList<CategoryInfo> categoryInfos) {
|
||||
mHolderList.clear();
|
||||
|
||||
for (int i = 0, size = categories.size(); i < size; i++) {
|
||||
Category category = categories.get(i);
|
||||
if (category.visible) {
|
||||
MusicFragments fragment = MusicFragments.valueOf(category.id.toString());
|
||||
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(category.id.key)
|
||||
.getString(categoryInfo.category.stringRes)
|
||||
.toUpperCase(Locale.getDefault());
|
||||
mHolderList.add(holder);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue