2020-09-24 18:51:20 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
import android.graphics.Color;
|
2020-09-24 18:51:20 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CheckBox;
|
2020-10-09 17:47:52 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2020-09-24 18:51:20 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.models.Labels;
|
|
|
|
import java.util.ArrayList;
|
2020-10-09 17:47:52 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2020-09-24 18:51:20 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class LabelsListAdapter extends RecyclerView.Adapter<LabelsListAdapter.LabelsViewHolder> {
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
private List<Integer> currentLabelsIds;
|
2020-09-24 18:51:20 +02:00
|
|
|
private List<Labels> labels;
|
2020-10-09 17:47:52 +02:00
|
|
|
private List<String> labelsStrings = new ArrayList<>();
|
|
|
|
private List<Integer> labelsIds = new ArrayList<>();
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
private LabelsListAdapterListener labelsListener;
|
|
|
|
|
|
|
|
public interface LabelsListAdapterListener {
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
void labelsInterface(List<String> data);
|
|
|
|
void labelsIdsInterface(List<Integer> data);
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
public LabelsListAdapter(List<Labels> labelsMain, LabelsListAdapterListener labelsListener, List<Integer> currentLabelsIds) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
this.labels = labelsMain;
|
|
|
|
this.labelsListener = labelsListener;
|
2020-10-09 17:47:52 +02:00
|
|
|
this.currentLabelsIds = currentLabelsIds;
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static class LabelsViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
private CheckBox labelSelection;
|
2020-10-09 17:47:52 +02:00
|
|
|
private TextView labelText;
|
|
|
|
private ImageView labelColor;
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
private LabelsViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
labelSelection = itemView.findViewById(R.id.labelSelection);
|
2020-10-09 17:47:52 +02:00
|
|
|
labelText = itemView.findViewById(R.id.labelText);
|
|
|
|
labelColor = itemView.findViewById(R.id.labelColor);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public LabelsListAdapter.LabelsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_labels_list, parent, false);
|
|
|
|
return new LabelsListAdapter.LabelsViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull LabelsListAdapter.LabelsViewHolder holder, int position) {
|
|
|
|
|
|
|
|
Labels currentItem = labels.get(position);
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
String labelColor = currentItem.getColor();
|
|
|
|
int color = Color.parseColor("#" + labelColor);
|
|
|
|
|
|
|
|
holder.labelText.setText(currentItem.getName());
|
|
|
|
holder.labelColor.setBackgroundColor(color);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < labelsIds.size(); i++) {
|
|
|
|
|
|
|
|
if(labelsStrings.contains(currentItem.getName())) {
|
|
|
|
|
|
|
|
holder.labelSelection.setChecked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
currentLabelsIds = new ArrayList<>(new LinkedHashSet<>(currentLabelsIds));
|
|
|
|
for(int i = 0; i < currentLabelsIds.size(); i++) {
|
|
|
|
|
|
|
|
if(currentLabelsIds.contains(currentItem.getId())) {
|
|
|
|
|
|
|
|
holder.labelSelection.setChecked(true);
|
|
|
|
labelsIds.add(currentLabelsIds.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labelsListener.labelsIdsInterface(labelsIds);
|
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
holder.labelSelection.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
|
|
|
|
|
|
|
if(isChecked) {
|
|
|
|
|
|
|
|
labelsStrings.add(currentItem.getName());
|
|
|
|
labelsIds.add(currentItem.getId());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
labelsStrings.remove(currentItem.getName());
|
|
|
|
labelsIds.remove(Integer.valueOf(currentItem.getId()));
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
labelsListener.labelsInterface(labelsStrings);
|
|
|
|
labelsListener.labelsIdsInterface(labelsIds);
|
2020-09-24 18:51:20 +02:00
|
|
|
});
|
2020-10-09 17:47:52 +02:00
|
|
|
|
|
|
|
labelsIds = new ArrayList<>(new LinkedHashSet<>(labelsIds));
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return labels.size();
|
|
|
|
}
|
2020-10-09 17:47:52 +02:00
|
|
|
|
|
|
|
public void updateList(List<Integer> list) {
|
|
|
|
|
|
|
|
currentLabelsIds = list;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|