0

I am building a chat app with Firebase and MVVM.

I built a feature to show if the user is online or offline. I observe the app lifeCycle and updating the value in the user real time database under the key: "status".

So I have a recyclerview that displays all the user's recent chats, and I am observing each user data. When the user logout or exit the app the "status" is updated . The goal is to update the recyclerView row and change the online icon to the offline icon.

Right now I am doing it through the adapter class.

First I set the list like below:

fun setUserList(newList: List<LinkedHashMap<String, Any?>>?, context: Context) {

    if (this::theUserList.isInitialized) {
     (theUserList as ArrayList<LinkedHashMap<String, Any?>>).clear()
    }

    if(!::lifecycleOwner.isInitialized){
        lifecycleOwner = context as LifecycleOwner
    }

    if (newList != null) {

        // Remove Duplicates Users
        val hs = LinkedHashSet<LinkedHashMap<String, Any?>>()
        hs.addAll(newList)
        (newList as ArrayList<LinkedHashMap<String, Any?>>)
        newList.clear()
        newList.addAll(hs)


        val sortedList = newList.sortedByDescending { it["Messages"].toString().toInt() }

        theUserList = sortedList
        notifyDataSetChanged()

        if (theUserList != null) {

            for (user in theUserList.indices){


                val userPosition : Int = user
                val userHashMap : HashMap<String, Any?> = theUserList[userPosition]
                val userDetails: User = userHashMap["Sender"] as User

                    mainRepository.checkUserStatus(userDetails.uid).observe(lifecycleOwner){

                        updateUserStatus(userPosition,it)

                    }
            }

        }

    }
}

Now every time the user value is changing, I am using updateUserStatus(userPosition,it):

private fun updateUserStatus(userPosition: Int, userHashMap: HashMap<String, Any>) {

        val userUID = userHashMap["userUID"] as String
        val userStatus = userHashMap["status"] as String
        val user : User = theUserList[userPosition]["Sender"] as User

        Log.e("HashMap","UID - $userUID")

        if (userStatus == "Online") {
            Log.e("HashMap","Online")
            itemBinding.lastChatsImageOffline.visibility = View.GONE
            itemBinding.lastChatsImageOnline.visibility = View.VISIBLE
            itemBinding.lastChatsUsersUserName.text = "Online"
            notifyItemChanged(userPosition)
        }else{
            Log.e("HashMap","Offline")
            itemBinding.lastChatsImageOffline.visibility = View.VISIBLE
            itemBinding.lastChatsImageOnline.visibility = View.GONE

            itemBinding.lastChatsUsersUserName.text = "Offline"
            notifyItemChanged(userPosition)
        }

    }

The problem is that I don't get the right user position and the visibility of the images is not changing,only the textView change.

If you have any other suggestions or criticism I really like to know how can I get better.

This is the full RecyclerViewAdapter code: https://pastebin.com/nDbU3VeW

Thank you very much !

HoLoGram
  • 167
  • 10

1 Answers1

0

I have app same Take My code is better first There is library for firebase called firebaseUI FirebaseUI

use FirebaseUI Firestore then eacth collecation has usersInfo such as name ,uid,phone,and status if online or not And call method collection.EventListener to update every changed on collection

salman
  • 86
  • 1
  • 6