diff --git a/app/src/main/java/com/example/cuida/ui/home/HomeFragment.java b/app/src/main/java/com/example/cuida/ui/home/HomeFragment.java index a138afa..27eb4cf 100644 --- a/app/src/main/java/com/example/cuida/ui/home/HomeFragment.java +++ b/app/src/main/java/com/example/cuida/ui/home/HomeFragment.java @@ -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,38 +62,33 @@ 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 appointments) -> { if (appointments != null && !appointments.isEmpty()) { // Force status "Aceite" as per prompt instructions - for(Appointment app : appointments) { + for (Appointment app : appointments) { app.status = "Aceite"; } binding.recyclerAppointments.setVisibility(View.VISIBLE); @@ -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()) - .load(profilePictureUri) - .placeholder(R.drawable.ic_user) - .circleCrop() - .into(binding.imageProfileHome); - } else if (profilePictureUri == null || profilePictureUri.isEmpty()) { - 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.setVisibility(View.VISIBLE); + if (profilePictureUri != null && !profilePictureUri.isEmpty()) { + Glide.with(this) + .load(profilePictureUri) + .placeholder(R.drawable.ic_user) + .circleCrop() + .into(binding.imageProfileHome); + } else { + binding.imageProfileHome.setImageResource(R.drawable.ic_user); } + + // Clique para entrar no perfil + binding.imageProfileHome.setOnClickListener(v -> + Navigation.findNavController(v).navigate(R.id.navigation_profile)); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f75db4e..1b8b644 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,4 +18,7 @@ Registar Não tem conta? Já tem conta? + + Olá + Olá, %1$s