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
Reference- How do i display 2 interfaces on the same app based on different credentials?
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.
@field:JvmField
val isExpert: Boolean = false