86 lines
3.0 KiB
Java
86 lines
3.0 KiB
Java
package com.example.cuida.ui.medication;
|
|
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.CheckBox;
|
|
import android.widget.TextView;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import com.example.cuida.R;
|
|
import com.example.cuida.data.model.Medication;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder> {
|
|
|
|
private List<Medication> medicationList = new ArrayList<>();
|
|
private final OnItemClickListener listener;
|
|
|
|
public interface OnItemClickListener {
|
|
void onCheckClick(Medication medication);
|
|
|
|
void onItemClick(Medication medication);
|
|
}
|
|
|
|
public MedicationAdapter(OnItemClickListener listener) {
|
|
this.listener = listener;
|
|
}
|
|
|
|
public void setMedications(List<Medication> medications) {
|
|
this.medicationList = medications;
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public MedicationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_medication, parent, false);
|
|
return new MedicationViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull MedicationViewHolder holder, int position) {
|
|
Medication medication = medicationList.get(position);
|
|
holder.textName.setText(medication.name);
|
|
holder.textDosage.setText(medication.dosage);
|
|
holder.textTime.setText(medication.time);
|
|
holder.textNotes.setText(medication.notes);
|
|
|
|
// Remove listener temporarily to avoid triggering it during bind
|
|
holder.checkBoxTaken.setOnCheckedChangeListener(null);
|
|
holder.checkBoxTaken.setChecked(medication.isTaken);
|
|
|
|
holder.checkBoxTaken.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
|
medication.isTaken = isChecked;
|
|
listener.onCheckClick(medication);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return medicationList.size();
|
|
}
|
|
|
|
public class MedicationViewHolder extends RecyclerView.ViewHolder {
|
|
TextView textName, textDosage, textTime, textNotes;
|
|
CheckBox checkBoxTaken;
|
|
|
|
public MedicationViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
textName = itemView.findViewById(R.id.text_med_name);
|
|
textDosage = itemView.findViewById(R.id.text_med_dosage);
|
|
textTime = itemView.findViewById(R.id.text_med_time);
|
|
textNotes = itemView.findViewById(R.id.text_med_notes);
|
|
checkBoxTaken = itemView.findViewById(R.id.checkbox_taken);
|
|
|
|
itemView.setOnClickListener(v -> {
|
|
int position = getAdapterPosition();
|
|
if (listener != null && position != RecyclerView.NO_POSITION) {
|
|
listener.onItemClick(medicationList.get(position));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|