Model Class (Data Class)
data class Message(var userID:String? = "",var message:String? = "",var recID:String? = "",var isSeen:String="")
I have an an adapter that is supposed to change the value in a particular TextView based on the value of the isSeen field in my document. However this change does not happen, I thought that the adapter would make the change automatically when i call the notifyDataSetChanged() but since it did not, I added some event listener, listener registration and yet the TextView doesn't change when the isSeen changes from false to true. It only displays what it should display when the field is false.
onBindViewHolder inside Adapter Class
override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
val message:Message = MessageList[position]
if(position==MessageList.size-1){
if(message.isSeen=="true")
{
holder.textSeen.text = "Seen"
}else if(message.isSeen=="false"){
holder.textSeen.text = "Delivered"
}
}else{
holder.textSeen.visibility = View.GONE
}
}
Activity Class (where the value inside database successfully changes from false to true)
eventListener = EventListener { snapshot, _ ->
if (snapshot != null) {
val message: Message? = snapshot.toObject(Message::class.java)
if (message != null) {
if (message.recID.equals(userID) && message.userID.equals(recID)) {
val hashMap: HashMap<String, Any> = HashMap()
hashMap["isSeen"] = "true"
message.isSeen = "true"
snapshot.reference.update(hashMap)
messageAdapter.notifyDataSetChanged()
}
}
messageAdapter.notifyDataSetChanged()
}
}
private fun checkSeen() {
FirebaseFirestore.getInstance().collection("chats").document(roomID).collection("messages")
.addSnapshotListener(EventListener { results, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
return@EventListener
}
for (doc in results!!) {
val message: Message = doc.toObject(Message::class.java)
if (message.recID.equals(userID) && message.userID.equals(recID)) {
val hashMap: HashMap<String, Any> = HashMap()
hashMap["isSeen"] = "true"
message.isSeen = "true"
doc.reference.update(hashMap)
messageAdapter.notifyDataSetChanged()
}
}
})
messageAdapter.notifyDataSetChanged()
}
After Receiver opens the message
But for sender it still shows Delivered and not Seen.