Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.example.taskmanagement.chat
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.taskmanagement.R
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore
class UserSelectionFragment : Fragment() {
private lateinit var recyclerViewChats: RecyclerView
private lateinit var btnNewChat: FloatingActionButton
private val firestore = FirebaseFirestore.getInstance()
private val currentUser = FirebaseAuth.getInstance().currentUser
private val chatList = mutableListOf<Chat>()
private lateinit var chatAdapter: ChatViewAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_user_selection, container, false)
recyclerViewChats = view.findViewById(R.id.recyclerViewChats)
btnNewChat = view.findViewById(R.id.newChatButton)
setupRecyclerView()
loadChats()
btnNewChat.setOnClickListener {
val userListBottomSheet = DialogFragment()
userListBottomSheet.show(parentFragmentManager, "UserListBottomSheet")
}
return view
}
private fun setupRecyclerView() {
try {
chatAdapter = ChatViewAdapter(chatList) { chat ->
openChat(chat)
}
recyclerViewChats.layoutManager = LinearLayoutManager(requireContext())
recyclerViewChats.adapter = chatAdapter
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun loadChats() {
val currentUserId = currentUser?.email
if (currentUserId.isNullOrEmpty()) {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.user_not_authenticated),
Toast.LENGTH_SHORT
).show()
}
return
}
firestore.collection("chats")
.whereArrayContains("participants", currentUserId)
.get()
.addOnSuccessListener { documents ->
if (isAdded) {
chatList.clear()
for (document in documents) {
val chat = document.toObject(Chat::class.java).apply {
id = document.id
}
chatList.add(chat)
}
chatAdapter.notifyDataSetChanged()
}
}
.addOnFailureListener { e ->
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.error_loading_chats, e.message ?: "Unknown error"),
Toast.LENGTH_SHORT
).show()
}
}
}
private fun openChat(chat: Chat) {
val currentUserId = currentUser?.email
if (currentUserId.isNullOrEmpty()) {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.user_not_authenticated),
Toast.LENGTH_SHORT
).show()
}
return
}
if (!chat.participants.contains(currentUserId)) {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.user_not_found),
Toast.LENGTH_SHORT
).show()
}
return
}
val otherParticipant = chat.participants
.filter { it != currentUserId }
.sorted()
.joinToString("_")
if (otherParticipant.isEmpty()) {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.no_participants_found),
Toast.LENGTH_SHORT
).show()
}
return
}
firestore.collection("users")
.document(otherParticipant)
.get()
val chatUser = chat.participants.firstOrNull { it != currentUser?.email }
val bundle = Bundle().apply {
putString("selectedUser", chatUser)
findNavController().navigate(
R.id.action_userSelectionFragment_to_chatFragment,
bundle
)