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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
val otherParticipant = chat.participants.firstOrNull { it != currentUserId }
if (otherParticipant == null) {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.no_participants_found),
Toast.LENGTH_SHORT
).show()
}
return
}
firestore.collection("users")
.document(otherParticipant)
.get()
.addOnSuccessListener { document ->
if (isAdded) {
val otherUserRole = document.getString("role")
val currentUserRole = fetchUserRole(currentUserId)
if (canCommunicate(currentUserRole, otherUserRole)) {
val bundle = Bundle().apply {
putString("selectedUser", otherParticipant)
}
findNavController().navigate(
R.id.action_userSelectionFragment_to_chatFragment,
bundle
)
} else {
Toast.makeText(
requireContext(),
getString(R.string.cannot_communicate),
Toast.LENGTH_SHORT
).show()
}
}
}
.addOnFailureListener {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.error_loading_user_data),
Toast.LENGTH_SHORT
).show()
}
}
}
private fun canCommunicate(currentUserRole: String?, otherUserRole: String?): Boolean {
return when (currentUserRole) {
"PM" -> otherUserRole == "PL"
"PL" -> otherUserRole == "PM" || otherUserRole == "DEV"
"DEV" -> otherUserRole == "PL" || otherUserRole == "DEV"
else -> false
}
}
private fun fetchUserRole(userId: String?): String? {
var role: String? = null
if (userId.isNullOrEmpty()) return role
firestore.collection("users")
.document(userId)
.get()
.addOnSuccessListener { document ->
if (isAdded) {
role = document.getString("role")
}
}
.addOnFailureListener {
if (isAdded) {
Toast.makeText(
requireContext(),
getString(R.string.error_retrieving_role),
Toast.LENGTH_SHORT
).show()
}
}
return role
}
}