This commit is contained in:
Karim Abou Zeid 2017-12-26 22:35:07 +01:00
commit 739e565bc4
7 changed files with 107 additions and 159 deletions

View file

@ -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;

View file

@ -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);
}

View file

@ -1,65 +0,0 @@
package com.kabouzeid.gramophone.model;
import com.kabouzeid.gramophone.R;
import com.google.gson.annotations.SerializedName;
public class Category {
@SerializedName("id")
public Id id;
@SerializedName("index")
public int index;
@SerializedName("checkBox")
public boolean visible;
public Category(Category category) {
this.id = category.id;
this.visible = category.visible;
this.index = index;
}
public Category(Id id, boolean visible, int index) {
this.id = id;
this.visible = visible;
this.index = index;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return id == category.id;
}
@Override
public int hashCode() {
int result = 0;
result = 31 * result + id.hashCode();
return result;
}
public String toString()
{
return "{id:"+id + ", pos:"+ index + ", vis=" + visible + "}";
}
public static enum Id
{
SONGS(R.string.songs),
ALBUMS(R.string.albums),
ARTISTS(R.string.artists),
GENRES(R.string.genres),
PLAYLISTS(R.string.playlists);
public final int key;
private Id(int key) {
this.key = key;
}
}
}

View file

@ -0,0 +1,27 @@
package com.kabouzeid.gramophone.model;
import com.kabouzeid.gramophone.R;
public class CategoryInfo {
public Category category;
public boolean visible;
public CategoryInfo(Category category, boolean visible) {
this.category = category;
this.visible = visible;
}
public enum Category {
SONGS(R.string.songs),
ALBUMS(R.string.albums),
ARTISTS(R.string.artists),
GENRES(R.string.genres),
PLAYLISTS(R.string.playlists);
public final int stringRes;
Category(int stringRes) {
this.stringRes = stringRes;
}
}
}

View file

@ -6,16 +6,13 @@ import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import com.afollestad.materialdialogs.MaterialDialog;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.adapter.CategoryAdapter;
import com.kabouzeid.gramophone.model.Category;
import com.kabouzeid.gramophone.adapter.CategoryInfoAdapter;
import com.kabouzeid.gramophone.model.CategoryInfo;
import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.kabouzeid.gramophone.util.SwipeAndDragHelper;
import java.util.ArrayList;
@ -28,18 +25,15 @@ public class LibraryPreferenceDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.preference_dialog_library_categories, null);
View view = getActivity().getLayoutInflater().inflate(R.layout.preference_dialog_library_categories, null);
ArrayList<Category> categories = PreferenceUtil.getInstance(getContext()).getLibraryCategories();
RecyclerView categoriesView = view.findViewById(R.id.recycler_view);
categoriesView.setLayoutManager(new LinearLayoutManager(getActivity()));
CategoryAdapter adapter = new CategoryAdapter(categories);
SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(swipeAndDragHelper);
adapter.setTouchHelper(touchHelper);
categoriesView.setAdapter(adapter);
touchHelper.attachToRecyclerView(categoriesView);
CategoryInfoAdapter adapter = new CategoryInfoAdapter(PreferenceUtil.getInstance(getContext()).getLibraryCategoryInfos());
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
adapter.attachToRecyclerView(recyclerView);
return new MaterialDialog.Builder(getContext())
.title(R.string.library_categories)
@ -48,27 +42,25 @@ public class LibraryPreferenceDialog extends DialogFragment {
.negativeText(android.R.string.cancel)
.neutralText(R.string.reset_action)
.autoDismiss(false)
.onNeutral((dialog, action) -> {
adapter.setCategories(PreferenceUtil.getInstance(getContext()).getDefaultLibraryCategories());
})
.onNeutral((dialog, action) -> adapter.setCategoryInfos(PreferenceUtil.getInstance(getContext()).getDefaultLibraryCategoryInfos()))
.onNegative((dialog, action) -> dismiss())
.onPositive((dialog, action) -> {
updateCategories(adapter.getCategories());
updateCategories(adapter.getCategoryInfos());
dismiss();
})
.build();
}
private void updateCategories(ArrayList<Category> categories) {
private void updateCategories(ArrayList<CategoryInfo> categories) {
if (getSelected(categories) == 0) return;
PreferenceUtil.getInstance(getContext()).setLibraryCategories(categories);
PreferenceUtil.getInstance(getContext()).setLibraryCategoryInfos(categories);
}
private int getSelected(ArrayList<Category> categories) {
private int getSelected(ArrayList<CategoryInfo> categories) {
int selected = 0;
for (Category category : categories) {
if (category.visible)
for (CategoryInfo categoryInfo : categories) {
if (categoryInfo.visible)
selected++;
}
return selected;

View file

@ -98,7 +98,7 @@ public class LibraryFragment extends AbsMainActivityFragment implements CabHolde
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (PreferenceUtil.LIBRARY_CATEGORIES.equals(key)) {
Fragment current = getCurrentFragment();
pagerAdapter.setCategories(PreferenceUtil.getInstance(getActivity()).getLibraryCategories());
pagerAdapter.setCategoryInfos(PreferenceUtil.getInstance(getActivity()).getLibraryCategoryInfos());
pager.setOffscreenPageLimit(pagerAdapter.getCount() - 1);
int position = pagerAdapter.getItemPosition(current);
pager.setCurrentItem(position > -1 ? position : 0);

View file

@ -8,17 +8,17 @@ import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.helper.SortOrder;
import com.kabouzeid.gramophone.model.Category;
import com.kabouzeid.gramophone.model.CategoryInfo;
import com.kabouzeid.gramophone.ui.fragments.mainactivity.folders.FoldersFragment;
import com.kabouzeid.gramophone.ui.fragments.player.NowPlayingScreen;
import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public final class PreferenceUtil {
public static final String GENERAL_THEME = "general_theme";
@ -435,46 +435,40 @@ public final class PreferenceUtil {
return mPreferences.getBoolean(INITIALIZED_BLACKLIST, false);
}
public void setLibraryCategories(ArrayList<Category> categories) {
public void setLibraryCategoryInfos(ArrayList<CategoryInfo> categories) {
Gson gson = new Gson();
Type collectionType = new TypeToken<ArrayList<CategoryInfo>>() {
}.getType();
Set<String> data = new HashSet<>(categories.size());
for (int i = 0, size = categories.size(); i < size; i++) {
Category category = categories.get(i);
category.index = i;
data.add(gson.toJson(category));
}
SharedPreferences.Editor editor = mPreferences.edit();
editor.putStringSet(LIBRARY_CATEGORIES, data);
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(LIBRARY_CATEGORIES, gson.toJson(categories, collectionType));
editor.apply();
}
public ArrayList<Category> getLibraryCategories() {
Set<String> data = mPreferences.getStringSet(LIBRARY_CATEGORIES, null);
public ArrayList<CategoryInfo> getLibraryCategoryInfos() {
String data = mPreferences.getString(LIBRARY_CATEGORIES, null);
if (data != null) {
Gson gson = new Gson();
ArrayList<Category> result = new ArrayList<>(Collections.nCopies(data.size(), (Category) null));
Type collectionType = new TypeToken<ArrayList<CategoryInfo>>() {
}.getType();
for (String json : data) {
Category category = gson.fromJson(json, Category.class);
result.set(category.index, category);
try {
return gson.fromJson(data, collectionType);
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return result;
}
return getDefaultLibraryCategories();
return getDefaultLibraryCategoryInfos();
}
public ArrayList<Category> getDefaultLibraryCategories() {
ArrayList<Category> result = new ArrayList<>();
result.add(new Category(Category.Id.SONGS, true, 0));
result.add(new Category(Category.Id.ALBUMS, true, 1));
result.add(new Category(Category.Id.ARTISTS, true, 2));
result.add(new Category(Category.Id.GENRES, true, 3));
result.add(new Category(Category.Id.PLAYLISTS, true, 4));
return result;
public ArrayList<CategoryInfo> getDefaultLibraryCategoryInfos() {
ArrayList<CategoryInfo> defaultCategoryInfos = new ArrayList<>(5);
defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.SONGS, true));
defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ALBUMS, true));
defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.ARTISTS, true));
defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.GENRES, true));
defaultCategoryInfos.add(new CategoryInfo(CategoryInfo.Category.PLAYLISTS, true));
return defaultCategoryInfos;
}
}