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

View File

@@ -18,4 +18,7 @@
<string name="register_button">Registar</string>
<string name="no_account">Não 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>