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;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Label;
|
2020-09-24 18:51:20 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2022-07-29 14:08:44 +02:00
|
|
|
* @author M M Arif
|
2020-09-24 18:51:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class LabelsListAdapter extends RecyclerView.Adapter<LabelsListAdapter.LabelsViewHolder> {
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
private List<Integer> currentLabelsIds;
|
2022-04-18 09:10:54 +02:00
|
|
|
private final List<Label> labels;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final List<String> labelsStrings = new ArrayList<>();
|
2020-10-09 17:47:52 +02:00
|
|
|
private List<Integer> labelsIds = new ArrayList<>();
|
2020-09-24 18:51:20 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final LabelsListAdapterListener labelsListener;
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public LabelsListAdapter(List<Label> 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 {
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final CheckBox labelSelection;
|
|
|
|
private final TextView labelText;
|
|
|
|
private final ImageView labelColor;
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
private LabelsViewHolder(View itemView) {
|
2020-10-29 13:52:40 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
super(itemView);
|
2020-10-29 13:52:40 +01:00
|
|
|
this.setIsRecyclable(false);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
Label currentItem = labels.get(position);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
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));
|
2020-10-29 13:52:40 +01:00
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
for(int i = 0; i < currentLabelsIds.size(); i++) {
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
if(currentLabelsIds.contains(currentItem.getId().intValue())) {
|
2020-10-09 17:47:52 +02:00
|
|
|
|
|
|
|
holder.labelSelection.setChecked(true);
|
|
|
|
labelsIds.add(currentLabelsIds.get(i));
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 13:52:40 +01:00
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
labelsListener.labelsIdsInterface(labelsIds);
|
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
holder.labelSelection.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
|
|
|
|
|
|
|
if(isChecked) {
|
|
|
|
|
|
|
|
labelsStrings.add(currentItem.getName());
|
2022-04-18 09:10:54 +02:00
|
|
|
labelsIds.add(currentItem.getId().intValue());
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
labelsStrings.remove(currentItem.getName());
|
2022-04-18 09:10:54 +02:00
|
|
|
labelsIds.remove(Integer.valueOf(currentItem.getId().intValue()));
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-29 13:52:40 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
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
|
|
|
}
|