Search code examples
androidfirebasefirebase-realtime-databasechat

How to know if one user is in contacts of the other user?


I am building a chat applications similar to WhatsApp on Android Studio. I want to build privacy feature i.e. "Show profile image to contacts only". I though a lot about it and the only solution that comes in my mind is to fetch and store contacts list of each user in the database and then just check if a user is present in the contacts list of the other user. But storing list in firebase database is not possible since Firebase works with Maps. The structure of real-time database looks something like this:

enter image description here

enter image description here

How can I achieve this? Any idea or suggestion is appreciated.


Solution

  • Found the solution myself. So I actually stored contacts list for each user like this: enter image description here

    Now when I need to check if a particular number exist in the contact list of that user I basically compare the keys of the map like this.

    val genericTypeIndicator: GenericTypeIndicator<Map<String, Any>> = object : GenericTypeIndicator<Map<String, Any>>() {}
    val map: Map<String, Any>? = snapshot.getValue(genericTypeIndicator)
    for (key in map?.keys!!) {
                                                          
    }