Merge branch 'library-categories' of https://github.com/doompadee/Phonograph into doompadee-library-categories

This commit is contained in:
Karim Abou Zeid 2017-12-25 23:42:32 +01:00
commit ffa33fced7
14 changed files with 567 additions and 29 deletions

View file

@ -0,0 +1,95 @@
package com.kabouzeid.gramophone.adapter;
import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.model.Category;
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;
private ItemTouchHelper touchHelper;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
@Override
public CategoryAdapter.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);
holder.checkBox.setChecked(category.visible);
holder.title.setText(holder.title.getResources().getString(category.id.key));
holder.itemView.setOnClickListener(v -> {
category.visible = !category.visible;
holder.checkBox.setChecked(category.visible);
});
holder.dragView.setOnTouchListener((view, event) -> {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
touchHelper.startDrag(holder);
}
return false;
}
);
}
@Override
public int getItemCount() {
return categories.size();
}
public void setCategories(ArrayList<Category> categories) {
this.categories = categories;
notifyDataSetChanged();
}
@Override
public void onViewMoved(int oldPosition, int newPosition) {
Category category = categories.get(oldPosition);
categories.remove(oldPosition);
categories.add(newPosition, category);
notifyItemMoved(oldPosition, newPosition);
}
public void setTouchHelper(ItemTouchHelper touchHelper) {
this.touchHelper = touchHelper;
}
public ArrayList<Category> getCategories() {
return categories;
}
static class ViewHolder extends RecyclerView.ViewHolder {
public CheckBox checkBox;
public TextView title;
public View dragView;
public ViewHolder(View view) {
super(view);
checkBox = view.findViewById(R.id.checkbox);
title = view.findViewById(R.id.title);
dragView = view.findViewById(R.id.drag_view);
}
}
}

View file

@ -10,14 +10,17 @@ import android.util.SparseArray;
import android.view.ViewGroup;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.model.Category;
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;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.pager.PlaylistsFragment;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.library.pager.SongsFragment;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@ -30,33 +33,29 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
@NonNull
private final Context mContext;
@NonNull
private final String[] titles;
public MusicLibraryPagerAdapter(@NonNull final Context context, final FragmentManager fragmentManager) {
super(fragmentManager);
mContext = context;
titles = new String[]{
context.getResources().getString(R.string.songs),
context.getResources().getString(R.string.albums),
context.getResources().getString(R.string.artists),
context.getResources().getString(R.string.genres),
context.getResources().getString(R.string.playlists)
};
final MusicFragments[] fragments = MusicFragments.values();
for (final MusicLibraryPagerAdapter.MusicFragments fragment : fragments) {
add(fragment.getFragmentClass(), null);
}
setCategories(PreferenceUtil.getInstance(context).getLibraryCategories());
}
@SuppressWarnings("synthetic-access")
public void add(@NonNull final Class<? extends Fragment> className, final Bundle params) {
final Holder mHolder = new Holder();
mHolder.mClassName = className.getName();
mHolder.mParams = params;
public void setCategories(@NonNull ArrayList<Category> categories) {
mHolderList.clear();
final int mPosition = mHolderList.size();
mHolderList.add(mPosition, mHolder);
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());
Holder holder = new Holder();
holder.mClassName = fragment.getFragmentClass().getName();
holder.title = mContext.getResources()
.getString(category.id.key)
.toUpperCase(Locale.getDefault());
mHolderList.add(holder);
}
}
alignCache();
notifyDataSetChanged();
}
@ -68,6 +67,23 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
return getItem(position);
}
@Override
public int getItemPosition(@NonNull Object fragment) {
for (int i = 0, size = mHolderList.size(); i < size; i++) {
Holder holder = mHolderList.get(i);
if (holder.mClassName.equals(fragment.getClass().getName())) {
return i;
}
}
return POSITION_NONE;
}
@Override
public long getItemId(int position) {
// as fragment position is not fixed, we can't use position as id
return MusicFragments.of(getFragment(position).getClass()).ordinal();
}
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
@ -104,16 +120,40 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
@NonNull
@Override
public CharSequence getPageTitle(final int position) {
return titles[position]
.toUpperCase(Locale.getDefault());
return mHolderList.get(position).title;
}
/**
* Aligns the fragment cache with the current category layout.
*/
private void alignCache() {
if (mFragmentArray.size() == 0) return;
HashMap<String, WeakReference<Fragment>> mappings = new HashMap<>(mFragmentArray.size());
for (int i = 0, size = mFragmentArray.size(); i < size; i++) {
WeakReference<Fragment> ref = mFragmentArray.valueAt(i);
Fragment fragment = ref.get();
if (fragment != null) {
mappings.put(fragment.getClass().getName(), ref);
}
}
for (int i = 0, size = mHolderList.size(); i < size; i++) {
WeakReference<Fragment> ref = mappings.get(mHolderList.get(i).mClassName);
if (ref != null) {
mFragmentArray.put(i, ref);
} else {
mFragmentArray.remove(i);
}
}
}
public enum MusicFragments {
SONG(SongsFragment.class),
ALBUM(AlbumsFragment.class),
ARTIST(ArtistsFragment.class),
SONGS(SongsFragment.class),
ALBUMS(AlbumsFragment.class),
ARTISTS(ArtistsFragment.class),
GENRES(GenresFragment.class),
PLAYLIST(PlaylistsFragment.class);
PLAYLISTS(PlaylistsFragment.class);
private final Class<? extends Fragment> mFragmentClass;
@ -124,10 +164,25 @@ public class MusicLibraryPagerAdapter extends FragmentPagerAdapter {
public Class<? extends Fragment> getFragmentClass() {
return mFragmentClass;
}
public static MusicFragments of(Class<?> cl) {
MusicFragments[] fragments = All.FRAGMENTS;
for (int i = 0; i < fragments.length; i++) {
if (cl.equals(fragments[i].mFragmentClass))
return fragments[i];
}
throw new IllegalArgumentException("Unknown music fragment " + cl);
}
private static class All {
public static final MusicFragments[] FRAGMENTS = values();
}
}
private final static class Holder {
String mClassName;
Bundle mParams;
String title;
}
}