aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2015-05-26 18:03:47 -0700
committerScott Jackson <daneren2005@gmail.com>2015-05-26 18:03:47 -0700
commit117c246d7e619ab9a3c0fb36fb152b8ad8bf9afe (patch)
treecd3e5f986ce8449b4b0937c5923f61434bbf72f0 /app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java
parent096f97542c1f8b9baa6cc53fb2f6909f969e261f (diff)
downloaddsub-117c246d7e619ab9a3c0fb36fb152b8ad8bf9afe.tar.gz
dsub-117c246d7e619ab9a3c0fb36fb152b8ad8bf9afe.tar.bz2
dsub-117c246d7e619ab9a3c0fb36fb152b8ad8bf9afe.zip
Abstract out common logic into SectionAdapter
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java280
1 files changed, 280 insertions, 0 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java
new file mode 100644
index 00000000..5244c7a6
--- /dev/null
+++ b/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java
@@ -0,0 +1,280 @@
+/*
+ This file is part of Subsonic.
+ Subsonic is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+ Subsonic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+ You should have received a copy of the GNU General Public License
+ along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
+ Copyright 2015 (C) Scott Jackson
+*/
+
+package github.daneren2005.dsub.adapter;
+
+import android.content.Context;
+import android.support.v7.widget.RecyclerView;
+import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import github.daneren2005.dsub.R;
+import github.daneren2005.dsub.view.BasicHeaderView;
+import github.daneren2005.dsub.view.UpdateView;
+import github.daneren2005.dsub.view.UpdateView.UpdateViewHolder;
+
+public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewHolder<T>> {
+ private static String TAG = SectionAdapter.class.getSimpleName();
+ public static int VIEW_TYPE_HEADER = 0;
+
+ protected Context context;
+ protected List<String> headers;
+ protected List<List<T>> sections;
+ protected boolean singleSectionHeader;
+ protected OnItemClickedListener<T> onItemClickedListener;
+ protected UpdateView contextView;
+ protected T contextItem;
+ private List<T> selected = new ArrayList<>();
+
+ public SectionAdapter(Context context, List<T> section) {
+ this(context, section, false);
+ }
+ public SectionAdapter(Context context, List<T> section, boolean singleSectionHeader) {
+ this.context = context;
+ this.headers = Arrays.asList("Section");
+ this.sections = new ArrayList<>();
+ this.sections.add(section);
+ this.singleSectionHeader = singleSectionHeader;
+ }
+ public SectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
+ this(context, headers, sections, true);
+ }
+ public SectionAdapter(Context context, List<String> headers, List<List<T>> sections, boolean singleSectionHeader){
+ this.context = context;
+ this.headers = headers;
+ this.sections = sections;
+ this.singleSectionHeader = singleSectionHeader;
+ }
+
+ @Override
+ public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ if(viewType == VIEW_TYPE_HEADER) {
+ return onCreateHeaderHolder(parent);
+ } else {
+ final UpdateViewHolder<T> holder = onCreateSectionViewHolder(parent, viewType);
+ final UpdateView updateView = holder.getUpdateView();
+
+ if(updateView != null) {
+ updateView.getChildAt(0).setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ T item = holder.getItem();
+ if(updateView.isCheckable()) {
+ if (selected.contains(item)) {
+ selected.remove(item);
+ setChecked(updateView, false);
+ } else {
+ selected.add(item);
+ setChecked(updateView, true);
+ }
+ } else if(onItemClickedListener != null) {
+ onItemClickedListener.onItemClicked(item);
+ }
+ }
+ });
+ updateView.getChildAt(0).setOnLongClickListener(new View.OnLongClickListener() {
+ @Override
+ public boolean onLongClick(View v) {
+ T item = holder.getItem();
+ setContextItem(updateView, item);
+ v.showContextMenu();
+ return false;
+ }
+ });
+
+ View moreButton = updateView.findViewById(R.id.more_button);
+ if(moreButton == null) {
+ moreButton = updateView.findViewById(R.id.item_more);
+ }
+ if (moreButton != null) {
+ moreButton.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ T item = holder.getItem();
+ setContextItem(updateView, item);
+ v.showContextMenu();
+ }
+ });
+ }
+ }
+
+ return holder;
+ }
+ }
+
+ @Override
+ public void onBindViewHolder(UpdateViewHolder holder, int position) {
+ UpdateView updateView = holder.getUpdateView();
+
+ if(sections.size() == 1 && !singleSectionHeader) {
+ T item = sections.get(0).get(position);
+ onBindViewHolder(holder, item, getItemViewType(position));
+ if(updateView.isCheckable()) {
+ setChecked(updateView, selected.contains(item));
+ }
+ holder.setItem(item);
+ return;
+ }
+
+ int subPosition = 0;
+ for(List<T> section: sections) {
+ if(position == subPosition) {
+ int index = sections.indexOf(section);
+ onBindHeaderHolder(holder, headers.get(index));
+ return;
+ }
+
+ if(position <= (subPosition + section.size())) {
+ T item = section.get(position - subPosition - 1);
+ onBindViewHolder(holder, item, getItemViewType(item));
+
+ if(updateView.isCheckable()) {
+ setChecked(updateView, selected.contains(item));
+ }
+ holder.setItem(item);
+ return;
+ }
+
+ subPosition += section.size() + 1;
+ }
+ }
+
+ @Override
+ public int getItemCount() {
+ if(sections.size() == 1 && !singleSectionHeader) {
+ return sections.get(0).size();
+ }
+
+ int count = headers.size();
+ for(List<T> section: sections) {
+ count += section.size();
+ }
+
+ return count;
+ }
+
+ @Override
+ public int getItemViewType(int position) {
+ if(sections.size() == 1 && !singleSectionHeader) {
+ return getItemViewType(sections.get(0).get(position));
+ }
+
+ int subPosition = 0;
+ for(List<T> section: sections) {
+ if(position == subPosition) {
+ return VIEW_TYPE_HEADER;
+ }
+
+ if(position <= (subPosition + section.size())) {
+ return getItemViewType(section.get(position - subPosition - 1));
+ }
+
+ subPosition += section.size() + 1;
+ }
+
+ return -1;
+ }
+
+ public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
+ return new UpdateViewHolder(new BasicHeaderView(context));
+ }
+ public void onBindHeaderHolder(UpdateViewHolder holder, String header) {
+ UpdateView view = holder.getUpdateView();
+ view.setObject(header);
+ }
+
+ public T getItemForPosition(int position) {
+ if(sections.size() == 1 && !singleSectionHeader) {
+ return sections.get(0).get(position);
+ }
+
+ int subPosition = 0;
+ for(List<T> section: sections) {
+ if(position == subPosition) {
+ return null;
+ }
+
+ if(position <= (subPosition + section.size())) {
+ return section.get(position - subPosition - 1);
+ }
+
+ subPosition += section.size() + 1;
+ }
+
+ return null;
+ }
+
+ public void setContextItem(UpdateView updateView, T item) {
+ contextView = updateView;
+ contextItem = item;
+ }
+ public UpdateView getContextView() {
+ return contextView;
+ }
+ public T getContextItem() {
+ return contextItem;
+ }
+
+ public void setOnItemClickedListener(OnItemClickedListener<T> onItemClickedListener) {
+ this.onItemClickedListener = onItemClickedListener;
+ }
+
+ public List<T> getSelected() {
+ List<T> selected = new ArrayList<>();
+ selected.addAll(this.selected);
+ return selected;
+ }
+
+ public void clearSelected() {
+ // TODO: This needs to work with multiple sections
+ for(T item: selected) {
+ int index = sections.get(0).indexOf(item);
+ this.notifyItemChanged(index);
+ }
+ selected.clear();
+ }
+
+ public void removeItem(T item) {
+ int subPosition = 0;
+ for(List<T> section: sections) {
+ if(sections.size() > 1 || singleSectionHeader) {
+ subPosition++;
+ }
+
+ int index = section.indexOf(item);
+ if (index != -1) {
+ section.remove(item);
+ notifyItemRemoved(subPosition + index);
+ break;
+ }
+
+ subPosition += section.size();
+ }
+ }
+
+ public abstract UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);
+ public abstract void onBindViewHolder(UpdateViewHolder holder, T item, int viewType);
+ public abstract int getItemViewType(T item);
+ public void setChecked(UpdateView updateView, boolean checked) {}
+
+ public interface OnItemClickedListener<T> {
+ void onItemClicked(T item);
+ }
+}