skip to Main Content

The expertFragment() which shows lists of all users (except the current user). What I want is to divide users in two types- 1. Expert 2. normal Users.
And to show experts to users and users to expert.

Expert should logIn and through their assigned id & password while the user can sign in and log in.
Experts account should be created mannuly in Firebase Realtime Database with the key in the user ‘isExpert’.

What I tried-

   class ExpertFragment : Fragment(), iExpertAdapter {

private var _binding: FragmentExpertBinding? = null
private val binding get() = _binding!!

private lateinit var adapter: ExpertAdapter
private lateinit var expertList: ArrayList<User>

private lateinit var auth: FirebaseAuth
private lateinit var dbRef: DatabaseReference


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {

    _binding = FragmentExpertBinding.inflate(inflater, container, false)
    val root: View = binding.root

    auth = Firebase.auth
    dbRef = FirebaseDatabase.getInstance().reference

    expertList = ArrayList()
    adapter = ExpertAdapter(this.requireContext(),expertList,this)

    binding.expertRecylerView.adapter = adapter

    dbRef.child("user").addValueEventListener(object: ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {

            expertList.clear()
            for(postSnapshot in snapshot.children){

                val currentUser = postSnapshot.getValue(User::class.java)

                if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
                    expertList.add(currentUser!!)
                }
            }
            adapter.notifyDataSetChanged()

        }

        override fun onCancelled(error: DatabaseError) {

        }

    })

        return root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

override fun onConsultBtnClicked(v:View, name: String?, uid: String) {
    val bundle = bundleOf("name" to name,"uid" to uid)
    v.findNavController().navigate(R.id.action_navigation_expert_to_chatFragment,bundle)


}

}

  data class User(
val uid: String = "",
val name: String? = "",
val mail: String? = null,
val phoneNo: String? = null,
val address: String? = null,

val vehicleModleNo: String? = null,
val vehicleRegYear: String? = null,
val vehicleName: String? = null,
val rtoNo: String? = null,
val distanceTraveled: String? = null,

val isExpert: Boolean = false

)

But writing this && currentUser!!.isExpert in the if block hides all the users from the fragment.

  if(auth.currentUser?.uid != currentUser?.uid && currentUser!!.isExpert){
                    expertList.add(currentUser!!)
                }

2

Answers


  1. Chosen as BEST ANSWER
        dbRef.child("user").addValueEventListener(object: ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
    
                expertList.clear()
    
                // getting current user isExpert status.
                val isExpert = snapshot.child(auth.currentUser!!.uid).child("isExpert").value
                Log.e("isExpert",isExpert.toString())
    
                for(postSnapshot in snapshot.children){
    
                    val currentUser = postSnapshot.getValue(User::class.java)
    
                    // if currentUser is an Expert then only users will be visible
                    if(isExpert == true) {
                        if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == false) {
                            expertList.add(currentUser!!)
                        }
                    }
                    // if currentUser is a user then only experts will be visible
                    else{
                        if (auth.currentUser?.uid != currentUser?.uid && currentUser?.isExpert == true) {
                            expertList.add(currentUser!!)
                        }
                    }
    
                }
                adapter.notifyDataSetChanged()
    
            }
    
            override fun onCancelled(error: DatabaseError) {
    
            }
    
        })
    

    Reference- How do i display 2 interfaces on the same app based on different credentials?


    1. Double bang is used for definite value when you want to compare the value you can use nullable as well

      instead of currentUser!!.isExpert

      use currentUser?.isExpert == true

    this will check whether the current user is nullable or not. after this expert will be checked with true value.

    1. Boolean value is not read directly in the data model when mapped from firestore and realtime database. User JVMField annotation to read it

    @field:JvmField

    val isExpert: Boolean = false

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search