Tuesday, 22 June 2021

How to use kotlin coroutines with for a firestore query

I have created an app with Kotlin and Firebase Firestore. Now I need to implement coroutines as there is so much work on the main thread. But I'm also a beginner so it's something new to me. I've watched some tutorials on this but I didn't find complete tutorials on Firestore with coroutines. So I need some help to implement coroutines in my app In such parts like these (I tried by myself but didn't get it).

Retrieving posts from Firestore.

private fun retrievePosts() {
             FirebaseFirestore.getInstance().collection("Posts")
            .orderBy("timeStamp", Query.Direction.DESCENDING)
            .get()
            .addOnSuccessListener { queryDocumentSnapshots ->
                postList?.clear()
                for (documentSnapshot in queryDocumentSnapshots) {
                    val post = documentSnapshot.toObject(Post::class.java)
                    postList?.add(post)
                }
                postAdapter?.notifyDataSetChanged()
                postAdapter?.setOnPostClickListener(this)
                if (isRefreshed) {
                    swipe_refresh_home?.setRefreshing(false)
                    isRefreshed = false
                }
                swipe_refresh_home?.visibility = VISIBLE
                progress_bar_home?.visibility = GONE
            }.addOnFailureListener { e ->
                Log.d(TAG, "UserAdapter-retrieveUsers: ", e)
                swipe_refresh_home?.visibility = VISIBLE
                progress_bar_home?.visibility = GONE
            }
    }

Getting user data into an adapter

private fun userInfo( fullName: TextView, profileImage: CircleImageView,
                      about: TextView, uid: String,
                      userLocation: TextView, itemRoot: LinearLayout ) {

        val userRef = FirebaseFirestore.getInstance().collection("Users").document(uid)
        userRef.get()
                .addOnSuccessListener {
                    if (it != null && it.exists()) {
                        val user = it.toObject(User::class.java)
                        Glide.with(mContext).load(user?.getImage()).placeholder(R.drawable.default_pro_pic).into(profileImage)

                        fullName.text = user?.getFullName().toString()

                        about.text = user?.getAbout()

                        if (user?.getLocation() != ""){
                            userLocation.visibility = VISIBLE
                            userLocation.text = user?.getLocation()
                        }

                        if (profileImage.drawable == null){
                            itemRoot.visibility = GONE
                        }
                        else{
                            itemRoot.visibility = VISIBLE
                        }
                    }
                }
    }

And this Save post button in an adapter.

private fun savedPost(postId: String, saveButton: ImageView?) {
        FirebaseFirestore.getInstance().collection("Users").document(currentUserID)
                .collection("Saved Posts").document(postId)
                .get()
                .addOnSuccessListener {
                    if (it.exists()) {
                        saveButton?.setImageResource(drawable.ic_bookmark)
                    } else {
                        saveButton?.setImageResource(drawable.bookmark_post_ic)
                    }
                }
    }


from How to use kotlin coroutines with for a firestore query

No comments:

Post a Comment