.
This commit is contained in:
@@ -18,6 +18,7 @@ public class Medication {
|
||||
public String notes;
|
||||
|
||||
public boolean isTaken;
|
||||
public long lastTakenTimestamp; // Momento exato em milissegundos
|
||||
public String userId;
|
||||
|
||||
public Medication() {
|
||||
|
||||
@@ -9,8 +9,11 @@ import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import com.example.cuida.R;
|
||||
import com.example.cuida.data.model.Medication;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder> {
|
||||
|
||||
@@ -47,16 +50,67 @@ public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.Me
|
||||
holder.textTime.setText(medication.time);
|
||||
holder.textNotes.setText(medication.notes);
|
||||
|
||||
// Lógica para resetar o "certo" a cada novo horário
|
||||
boolean wasTakenForDose = wasTakenForCurrentDose(medication);
|
||||
|
||||
// Remove listener temporarily to avoid triggering it during bind
|
||||
holder.checkBoxTaken.setOnCheckedChangeListener(null);
|
||||
holder.checkBoxTaken.setChecked(medication.isTaken);
|
||||
holder.checkBoxTaken.setChecked(wasTakenForDose);
|
||||
|
||||
holder.checkBoxTaken.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
medication.isTaken = isChecked;
|
||||
if (isChecked) {
|
||||
medication.lastTakenTimestamp = System.currentTimeMillis();
|
||||
} else {
|
||||
medication.lastTakenTimestamp = 0;
|
||||
}
|
||||
listener.onCheckClick(medication);
|
||||
});
|
||||
}
|
||||
|
||||
private boolean wasTakenForCurrentDose(Medication med) {
|
||||
if (!med.isTaken || med.lastTakenTimestamp == 0 || med.time == null) return false;
|
||||
|
||||
try {
|
||||
String[] scheduleTimes = med.time.split(",\\s*");
|
||||
java.util.Calendar now = java.util.Calendar.getInstance();
|
||||
long mostRecentScheduleToday = 0;
|
||||
|
||||
for (String t : scheduleTimes) {
|
||||
String[] parts = t.split(":");
|
||||
if (parts.length != 2) continue;
|
||||
|
||||
java.util.Calendar sched = java.util.Calendar.getInstance();
|
||||
sched.set(java.util.Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
|
||||
sched.set(java.util.Calendar.MINUTE, Integer.parseInt(parts[1]));
|
||||
sched.set(java.util.Calendar.SECOND, 0);
|
||||
sched.set(java.util.Calendar.MILLISECOND, 0);
|
||||
|
||||
// Se este horário já passou hoje
|
||||
if (sched.before(now)) {
|
||||
if (sched.getTimeInMillis() > mostRecentScheduleToday) {
|
||||
mostRecentScheduleToday = sched.getTimeInMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Se ainda nenhum horário passou hoje, verificamos se tomou ontem/antes
|
||||
if (mostRecentScheduleToday == 0) {
|
||||
// Opcional: resetar ao início do dia
|
||||
java.util.Calendar startOfDay = java.util.Calendar.getInstance();
|
||||
startOfDay.set(java.util.Calendar.HOUR_OF_DAY, 0);
|
||||
startOfDay.set(java.util.Calendar.MINUTE, 0);
|
||||
return med.lastTakenTimestamp > startOfDay.getTimeInMillis();
|
||||
}
|
||||
|
||||
// O "certo" só fica se tomou DEPOIS do horário agendado que passou
|
||||
return med.lastTakenTimestamp >= mostRecentScheduleToday;
|
||||
|
||||
} catch (Exception e) {
|
||||
return med.isTaken;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return medicationList.size();
|
||||
|
||||
@@ -53,10 +53,10 @@
|
||||
android:id="@+id/input_symptoms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textCapSentences"
|
||||
android:inputType="textMultiLine|textCapSentences"
|
||||
android:imeOptions="actionDone"
|
||||
android:minLines="3"
|
||||
android:maxLines="5"
|
||||
android:minLines="6"
|
||||
android:maxLines="15"
|
||||
android:gravity="top|start"/>
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user