tenho de ver se marca as coisas

This commit is contained in:
2026-05-27 08:41:01 +01:00
parent b43829e11c
commit b0887862fa
2 changed files with 55 additions and 59 deletions

View File

@@ -1,47 +1,57 @@
package com.example.cuida.ui.home; package com.example.cuida.ui.home;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.cuida.R;
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 com.example.cuida.data.model.Medication;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.util.Collections; import com.example.cuida.R;
import com.example.cuida.data.model.Appointment;
import com.example.cuida.databinding.FragmentHomeBinding;
import com.example.cuida.ui.appointments.AppointmentAdapter;
import com.example.cuida.ui.appointments.AppointmentsViewModel;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.List;
public class HomeFragment extends Fragment { public class HomeFragment extends Fragment {
private FragmentHomeBinding binding; private FragmentHomeBinding binding;
private AppointmentsViewModel appointmentsViewModel;
private AppointmentAdapter appointmentAdapter; private AppointmentAdapter appointmentAdapter;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, Bundle savedInstanceState) { @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false); binding = FragmentHomeBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// --- Greeting & Profile Picture (Real-time) --- // --- Greeting & Profile Picture (Real-time) ---
com.google.firebase.auth.FirebaseAuth auth = com.google.firebase.auth.FirebaseAuth.getInstance(); FirebaseAuth auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) { if (auth.getCurrentUser() != null) {
String userId = auth.getCurrentUser().getUid(); String userId = auth.getCurrentUser().getUid();
com.google.firebase.firestore.FirebaseFirestore db = com.google.firebase.firestore.FirebaseFirestore.getInstance(); FirebaseFirestore db = FirebaseFirestore.getInstance();
// Listen to 'utilizadores' for basic profile info // Listen to 'utilizadores' for basic profile info
db.collection("utilizadores").document(userId) db.collection("utilizadores").document(userId)
.addSnapshotListener((documentSnapshot, error) -> { .addSnapshotListener((documentSnapshot, error) -> {
if (error != null) { if (error != null) {
android.util.Log.e("HomeFragment", "Error loading profile", error); Log.e("HomeFragment", "Error loading profile", error);
return; return;
} }
if (documentSnapshot != null && documentSnapshot.exists() && isAdded()) { if (documentSnapshot != null && documentSnapshot.exists() && isAdded()) {
@@ -52,38 +62,33 @@ public class HomeFragment extends Fragment {
// Also listen to 'Pacientes' (collection where Professional app saves data) // Also listen to 'Pacientes' (collection where Professional app saves data)
db.collection("Pacientes").document(userId) db.collection("Pacientes").document(userId)
.addSnapshotListener((documentSnapshot, error) -> { .addSnapshotListener((documentSnapshot, error) -> {
if (error != null) {
Log.e("HomeFragment", "Error loading paciente profile", error);
return;
}
if (documentSnapshot != null && documentSnapshot.exists() && isAdded()) { if (documentSnapshot != null && documentSnapshot.exists() && isAdded()) {
updateGreetingUI(documentSnapshot); updateGreetingUI(documentSnapshot);
} }
}); });
} else { } else {
binding.textGreeting.setText("Olá"); binding.textGreeting.setText(R.string.hello);
} }
// --- Setup Adapters --- // --- Setup Adapters ---
// Consultations
appointmentAdapter = new AppointmentAdapter(); appointmentAdapter = new AppointmentAdapter();
binding.recyclerAppointments.setLayoutManager(new LinearLayoutManager(getContext())); binding.recyclerAppointments.setLayoutManager(new LinearLayoutManager(requireContext()));
binding.recyclerAppointments.setAdapter(appointmentAdapter); binding.recyclerAppointments.setAdapter(appointmentAdapter);
// Schedule Button // Schedule Button
if (binding.buttonSchedule != null) { binding.buttonSchedule.setOnClickListener(v ->
binding.buttonSchedule.setOnClickListener(v -> { Navigation.findNavController(v).navigate(R.id.action_home_to_schedule_appointment));
if (getActivity() != null) {
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, new com.example.cuida.ui.schedule.ScheduleAppointmentFragment())
.addToBackStack(null)
.commit();
}
});
}
// --- Load Appointments --- // --- Load Appointments ---
appointmentsViewModel = new ViewModelProvider(this).get(AppointmentsViewModel.class); AppointmentsViewModel appointmentsViewModel = new ViewModelProvider(this).get(AppointmentsViewModel.class);
appointmentsViewModel.getFutureAppointments().observe(getViewLifecycleOwner(), appointments -> { appointmentsViewModel.getFutureAppointments().observe(getViewLifecycleOwner(), (List<Appointment> appointments) -> {
if (appointments != null && !appointments.isEmpty()) { if (appointments != null && !appointments.isEmpty()) {
// Force status "Aceite" as per prompt instructions // Force status "Aceite" as per prompt instructions
for(Appointment app : appointments) { for (Appointment app : appointments) {
app.status = "Aceite"; app.status = "Aceite";
} }
binding.recyclerAppointments.setVisibility(View.VISIBLE); binding.recyclerAppointments.setVisibility(View.VISIBLE);
@@ -94,13 +99,9 @@ public class HomeFragment extends Fragment {
binding.textEmptyAppointments.setVisibility(View.VISIBLE); binding.textEmptyAppointments.setVisibility(View.VISIBLE);
} }
}); });
return binding.getRoot();
} }
private void updateGreetingUI(com.google.firebase.firestore.DocumentSnapshot documentSnapshot) { private void updateGreetingUI(DocumentSnapshot documentSnapshot) {
if (!isAdded() || binding == null) return; if (!isAdded() || binding == null) return;
String name = documentSnapshot.getString("nome_completo"); String name = documentSnapshot.getString("nome_completo");
@@ -109,32 +110,24 @@ public class HomeFragment extends Fragment {
if (name != null && !name.isEmpty()) { if (name != null && !name.isEmpty()) {
String firstName = name.split(" ")[0]; String firstName = name.split(" ")[0];
binding.textGreeting.setText("Olá, " + firstName); binding.textGreeting.setText(getString(R.string.hello_user, firstName));
} }
String profilePictureUri = documentSnapshot.getString("profilePictureUri"); String profilePictureUri = documentSnapshot.getString("profilePictureUri");
if (binding.imageProfileHome != null) {
binding.imageProfileHome.setVisibility(View.VISIBLE); binding.imageProfileHome.setVisibility(View.VISIBLE);
if (profilePictureUri != null && !profilePictureUri.isEmpty() && getContext() != null) { if (profilePictureUri != null && !profilePictureUri.isEmpty()) {
Glide.with(getContext()) Glide.with(this)
.load(profilePictureUri) .load(profilePictureUri)
.placeholder(R.drawable.ic_user) .placeholder(R.drawable.ic_user)
.circleCrop() .circleCrop()
.into(binding.imageProfileHome); .into(binding.imageProfileHome);
} else if (profilePictureUri == null || profilePictureUri.isEmpty()) { } else {
binding.imageProfileHome.setImageResource(R.drawable.ic_user); binding.imageProfileHome.setImageResource(R.drawable.ic_user);
} }
// Clique para entrar no perfil // Clique para entrar no perfil
binding.imageProfileHome.setOnClickListener(v -> { binding.imageProfileHome.setOnClickListener(v ->
if (getActivity() != null) { Navigation.findNavController(v).navigate(R.id.navigation_profile));
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, new com.example.cuida.ui.profile.ProfileFragment())
.addToBackStack(null)
.commit();
}
});
}
} }
@Override @Override

View File

@@ -18,4 +18,7 @@
<string name="register_button">Registar</string> <string name="register_button">Registar</string>
<string name="no_account">Não tem conta?</string> <string name="no_account">Não tem conta?</string>
<string name="already_account">Já tem conta?</string> <string name="already_account">Já tem conta?</string>
<string name="hello">Olá</string>
<string name="hello_user">Olá, %1$s</string>
</resources> </resources>