tenho de ver se marca as coisas

This commit is contained in:
2026-05-04 14:33:25 +01:00
parent ae6cf5413c
commit a40a75d710
3 changed files with 146 additions and 99 deletions

View File

@@ -4,10 +4,10 @@
<selectionStates>
<SelectionState runConfigName="app">
<option name="selectionMode" value="DROPDOWN" />
<DropdownSelection timestamp="2026-04-30T09:29:05.514328Z">
<DropdownSelection timestamp="2026-04-30T09:38:26.648780Z">
<Target type="DEFAULT_BOOT">
<handle>
<DeviceId pluginId="LocalEmulator" identifier="path=/Users/230405/.android/avd/Medium_Phone.avd" />
<DeviceId pluginId="PhysicalDevice" identifier="serial=b93659d0e5dd" />
</handle>
</Target>
</DropdownSelection>

View File

@@ -1,7 +1,5 @@
package com.example.cuida.ui.home;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@@ -9,12 +7,17 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.cuida.databinding.FragmentHomeBinding;
import com.example.cuida.ui.medication.MedicationViewModel;
import com.example.cuida.ui.medication.MedicationAdapter;
import com.example.cuida.ui.appointments.AppointmentsViewModel;
import com.example.cuida.ui.appointments.AppointmentAdapter;
import com.example.cuida.data.model.Appointment;
import java.util.Calendar;
import java.util.Locale;
import com.example.cuida.data.model.Medication;
import java.util.Collections;
public class HomeFragment extends Fragment {
@@ -22,6 +25,9 @@ public class HomeFragment extends Fragment {
private MedicationViewModel medicationViewModel;
private AppointmentsViewModel appointmentsViewModel;
private AppointmentAdapter appointmentAdapter;
private MedicationAdapter medicationAdapter;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
@@ -34,19 +40,16 @@ public class HomeFragment extends Fragment {
.get()
.addOnSuccessListener(documentSnapshot -> {
if (documentSnapshot.exists() && isAdded()) {
// Tenta 'nome_completo' (novo) ou 'name' (antigo)
String name = documentSnapshot.getString("nome_completo");
if (name == null || name.isEmpty()) name = documentSnapshot.getString("name");
if (name != null && !name.isEmpty()) {
// Extract first name
String firstName = name.split(" ")[0];
binding.textGreeting.setText("Olá, " + firstName + "!");
} else {
binding.textGreeting.setText("Olá, Utilizador!");
}
// Load Profile Picture
String profilePictureUri = documentSnapshot.getString("profilePictureUri");
if (profilePictureUri != null && !profilePictureUri.isEmpty()) {
try {
@@ -65,32 +68,62 @@ public class HomeFragment extends Fragment {
binding.textGreeting.setText("Olá, Utilizador!");
}
// --- Next Medication ---
// --- Setup Adapters ---
// Consultations
appointmentAdapter = new AppointmentAdapter();
binding.recyclerAppointments.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerAppointments.setAdapter(appointmentAdapter);
// Medications
medicationViewModel = new ViewModelProvider(this).get(MedicationViewModel.class);
medicationViewModel.getNextMedication().observe(getViewLifecycleOwner(), medication -> {
if (medication != null) {
binding.nextMedName.setText(medication.name + " (" + medication.dosage + ")");
binding.nextMedTime.setText("Hoje, " + medication.time);
medicationAdapter = new MedicationAdapter(new MedicationAdapter.OnItemClickListener() {
@Override
public void onCheckClick(Medication medication) {
medicationViewModel.update(medication);
}
@Override
public void onItemClick(Medication medication) {
// Do nothing
}
});
binding.recyclerMedications.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerMedications.setAdapter(medicationAdapter);
// --- Load Appointments ---
appointmentsViewModel = new ViewModelProvider(this).get(AppointmentsViewModel.class);
appointmentsViewModel.getFutureAppointments().observe(getViewLifecycleOwner(), appointments -> {
if (appointments != null && !appointments.isEmpty()) {
// Force status "Aceite" as per prompt instructions
for(Appointment app : appointments) {
app.status = "Aceite";
}
binding.recyclerAppointments.setVisibility(View.VISIBLE);
binding.textEmptyAppointments.setVisibility(View.GONE);
appointmentAdapter.setAppointments(appointments);
} else {
binding.nextMedName.setText("Sem medicação");
binding.nextMedTime.setText("--:--");
binding.recyclerAppointments.setVisibility(View.GONE);
binding.textEmptyAppointments.setVisibility(View.VISIBLE);
}
});
// --- Book Appointment ---
appointmentsViewModel = new ViewModelProvider(this).get(AppointmentsViewModel.class);
binding.buttonBookAppointment.setOnClickListener(v -> {
androidx.navigation.Navigation.findNavController(v)
.navigate(com.example.cuida.R.id.action_home_to_schedule_appointment);
// --- Load Medications ---
medicationViewModel.getAllMedications().observe(getViewLifecycleOwner(), medications -> {
if (medications != null && !medications.isEmpty()) {
// To keep the most recent at the top, we reverse the list
Collections.reverse(medications);
binding.recyclerMedications.setVisibility(View.VISIBLE);
binding.textEmptyMedications.setVisibility(View.GONE);
medicationAdapter.setMedications(medications);
} else {
binding.recyclerMedications.setVisibility(View.GONE);
binding.textEmptyMedications.setVisibility(View.VISIBLE);
}
});
return binding.getRoot();
}
@Override
public void onDestroyView() {
super.onDestroyView();

View File

@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<com.google.android.material.imageview.ShapeableImageView
@@ -28,62 +33,71 @@
app:layout_constraintBottom_toBottomOf="@id/image_profile_home"
app:layout_constraintStart_toEndOf="@id/image_profile_home"/>
<com.google.android.material.card.MaterialCardView
android:id="@+id/card_next_medication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:layout_constraintTop_toBottomOf="@id/image_profile_home">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Agenda de Consultas -->
<TextView
android:id="@+id/title_agenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Próxima Medicação"
android:layout_marginTop="24dp"
android:text="Agenda de Consultas"
android:textSize="20sp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="@color/primary_color"/>
android:textColor="@color/primary_color"
app:layout_constraintTop_toBottomOf="@id/image_profile_home"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/next_med_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paracetamol 500mg"
android:textSize="16sp"
android:layout_marginTop="8dp"/>
<TextView
android:id="@+id/next_med_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hoje, 14:00"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
android:layout_marginTop="4dp"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.button.MaterialButton
android:id="@+id/button_book_appointment"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_appointments"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Marcar Consulta"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/title_agenda"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/text_empty_appointments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sem consultas agendadas."
android:textAlignment="center"
android:textColor="@android:color/darker_gray"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/recycler_appointments" />
<!-- Tratamentos Atuais -->
<TextView
android:id="@+id/title_medication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:icon="@android:drawable/ic_menu_my_calendar"
app:iconGravity="textStart"
app:layout_constraintTop_toBottomOf="@id/card_next_medication"/>
android:text="Tratamentos Atuais"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/primary_color"
app:layout_constraintTop_toBottomOf="@id/recycler_appointments"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_medications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="@id/title_medication"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="@+id/text_empty_medications"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nenhum tratamento ativo."
android:textAlignment="center"
android:textColor="@android:color/darker_gray"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/recycler_medications" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>