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 8a2a27f..0efef1b 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 @@ -84,9 +84,55 @@ public class HomeFragment extends Fragment { .navigate(com.example.cuida.R.id.action_home_to_schedule_appointment); }); + // --- Feed Listener --- + loadFeed(); + return binding.getRoot(); } + private void loadFeed() { + com.google.firebase.auth.FirebaseAuth auth = com.google.firebase.auth.FirebaseAuth.getInstance(); + if (auth.getCurrentUser() == null) return; + String userId = auth.getCurrentUser().getUid(); + + com.google.firebase.firestore.FirebaseFirestore.getInstance().collection("feed") + .whereEqualTo("userId", userId) + .orderBy("data", com.google.firebase.firestore.Query.Direction.DESCENDING) + .addSnapshotListener((value, error) -> { + if (error != null) return; + if (value != null && isAdded()) { + binding.layoutFeedContainer.removeAllViews(); + if (value.isEmpty()) { + binding.layoutFeedContainer.addView(binding.textNoFeed); + } else { + for (com.google.firebase.firestore.DocumentSnapshot doc : value.getDocuments()) { + addFeedItem(doc); + } + } + } + }); + } + + private void addFeedItem(com.google.firebase.firestore.DocumentSnapshot doc) { + if (!isAdded()) return; + View itemView = getLayoutInflater().inflate(com.example.cuida.R.layout.item_feed, binding.layoutFeedContainer, false); + + android.widget.TextView title = itemView.findViewById(com.example.cuida.R.id.feed_item_title); + android.widget.TextView content = itemView.findViewById(com.example.cuida.R.id.feed_item_content); + android.widget.TextView date = itemView.findViewById(com.example.cuida.R.id.feed_item_date); + + title.setText(doc.getString("titulo")); + content.setText(doc.getString("conteudo")); + + com.google.firebase.Timestamp ts = doc.getTimestamp("data"); + if (ts != null) { + java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd MMM, HH:mm", java.util.Locale.getDefault()); + date.setText(sdf.format(ts.toDate())); + } + + binding.layoutFeedContainer.addView(itemView); + } + @Override public void onDestroyView() { super.onDestroyView(); diff --git a/app/src/main/java/com/example/cuida/ui/profile/ProfileFragment.java b/app/src/main/java/com/example/cuida/ui/profile/ProfileFragment.java index f4641a7..694fd94 100644 --- a/app/src/main/java/com/example/cuida/ui/profile/ProfileFragment.java +++ b/app/src/main/java/com/example/cuida/ui/profile/ProfileFragment.java @@ -79,9 +79,14 @@ public class ProfileFragment extends Fragment { currentUser.name = auth.getCurrentUser().getDisplayName(); } - db.collection("utilizadores").document(userId).get() - .addOnSuccessListener(doc -> { - if (doc.exists() && isAdded()) { + db.collection("utilizadores").document(userId) + .addSnapshotListener((doc, error) -> { + if (error != null) { + Log.e("ProfileFragment", "Listen failed.", error); + return; + } + + if (doc != null && doc.exists() && isAdded()) { currentUser.id = doc.getId(); String nome = doc.getString("nome_completo"); if (nome == null) nome = doc.getString("name"); diff --git a/app/src/main/java/com/example/cuida/ui/schedule/ScheduleViewModel.java b/app/src/main/java/com/example/cuida/ui/schedule/ScheduleViewModel.java index 1090604..f14f3d4 100644 --- a/app/src/main/java/com/example/cuida/ui/schedule/ScheduleViewModel.java +++ b/app/src/main/java/com/example/cuida/ui/schedule/ScheduleViewModel.java @@ -64,7 +64,7 @@ public class ScheduleViewModel extends AndroidViewModel { displayName += " - " + specialty; } if (!displayName.startsWith("Dr.") && !displayName.startsWith("Dra.")) { - if ("Feminino".equalsIgnoreCase(gender) || "Feminino".equals(gender)) { + if ("Feminino".equalsIgnoreCase(gender)) { displayName = "Dra. " + displayName; } else { displayName = "Dr. " + displayName; @@ -148,12 +148,17 @@ public class ScheduleViewModel extends AndroidViewModel { timeSlots.setValue(generateTimeSlots(new ArrayList<>(), date)); if (auth.getCurrentUser() == null) return; - String userId = auth.getCurrentUser().getUid(); String doctor = selectedDoctor.getValue(); - // Listen in REAL-TIME for all appointments on the selected date + if (doctor == null) { + timeSlots.setValue(generateTimeSlots(new ArrayList<>(), date)); + return; + } + + // Listen in REAL-TIME for appointments of the selected doctor on the selected date snapshotListener = db.collection("consultas") .whereEqualTo("date", date) + .whereEqualTo("type", doctor) .addSnapshotListener((queryDocumentSnapshots, e) -> { if (e != null) { Log.e("ScheduleViewModel", "Listen failed.", e); @@ -164,13 +169,8 @@ public class ScheduleViewModel extends AndroidViewModel { if (queryDocumentSnapshots != null) { for (QueryDocumentSnapshot document : queryDocumentSnapshots) { Appointment appt = document.toObject(Appointment.class); - - boolean isDoctorAppointment = doctor != null && doctor.equals(appt.type); - - if (isDoctorAppointment && appt.time != null) { - if (!bookedTimes.contains(appt.time)) { - bookedTimes.add(appt.time); - } + if (appt.time != null && !bookedTimes.contains(appt.time)) { + bookedTimes.add(appt.time); } } } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index f2cf4dc..c6b4b32 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -84,4 +84,41 @@ app:iconGravity="textStart" app:layout_constraintTop_toBottomOf="@id/card_next_medication"/> + + + + + + + + + + +