This commit is contained in:
2026-04-28 17:11:57 +01:00
parent 418cbf9479
commit 097dbad215
3 changed files with 59 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ public class Medication {
public String notes; public String notes;
public boolean isTaken; public boolean isTaken;
public long lastTakenTimestamp; // Momento exato em milissegundos
public String userId; public String userId;
public Medication() { public Medication() {

View File

@@ -9,8 +9,11 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.example.cuida.R; import com.example.cuida.R;
import com.example.cuida.data.model.Medication; import com.example.cuida.data.model.Medication;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder> { 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.textTime.setText(medication.time);
holder.textNotes.setText(medication.notes); 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 // Remove listener temporarily to avoid triggering it during bind
holder.checkBoxTaken.setOnCheckedChangeListener(null); holder.checkBoxTaken.setOnCheckedChangeListener(null);
holder.checkBoxTaken.setChecked(medication.isTaken); holder.checkBoxTaken.setChecked(wasTakenForDose);
holder.checkBoxTaken.setOnCheckedChangeListener((buttonView, isChecked) -> { holder.checkBoxTaken.setOnCheckedChangeListener((buttonView, isChecked) -> {
medication.isTaken = isChecked; medication.isTaken = isChecked;
if (isChecked) {
medication.lastTakenTimestamp = System.currentTimeMillis();
} else {
medication.lastTakenTimestamp = 0;
}
listener.onCheckClick(medication); 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 @Override
public int getItemCount() { public int getItemCount() {
return medicationList.size(); return medicationList.size();

View File

@@ -53,10 +53,10 @@
android:id="@+id/input_symptoms" android:id="@+id/input_symptoms"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textCapSentences" android:inputType="textMultiLine|textCapSentences"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:minLines="3" android:minLines="6"
android:maxLines="5" android:maxLines="15"
android:gravity="top|start"/> android:gravity="top|start"/>
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>