First of all thanks for your time. I'm trying to add a two option login in my app. One for a doctor and one for patient. I achieved to store both of them in realtime database as you can see below.
patient / doctor --> realtime database
As you can see, i have one tag ("isDoctor") with true o false for distinguish between both users. Wat i want to is to check the value of this "isDoctor" tag to send the user to the corresponding activity. If isDoctor is true i want to send the user to Doctor activity and if is a patient to the corresponding one. Is thinks that this checking should go in "State.Succes" once the user is sign in but i don't know how to perform it.
Here is my login code from RemoteDataSource class and from the login activity. I am using mvvm with flow for this project.
mBinding.btnLogin.setOnClickListener {
if (areFieldsReady()) {
lifecycleScope.launchWhenStarted {
loginViewModel.login(email, password).collect { loginResult ->
when (loginResult) {
is State.Loading -> {
if (loginResult.flag == true) loadingDialog.startLoading()
}
is State.Succes -> {
loadingDialog.stopLoading()
Snackbar.make(
mBinding.root,
loginResult.data.toString(),
Snackbar.LENGTH_SHORT
).show()
}
is State.Failed -> {
loadingDialog.stopLoading()
Snackbar.make(
mBinding.root,
loginResult.error,
Snackbar.LENGTH_SHORT
).show()
}
}
}
}
}
}
fun login(email: String, password: String): Flow<State<Any>> = flow<State<Any>> {
emit(State.loading(true))
val auth = Firebase.auth
val data = auth.signInWithEmailAndPassword(email, password).await()
//if the task was performend in backend
data?.let {
if (auth.currentUser?.isEmailVerified!!) {
emit(State.succes(R.string.login_succes)) //emiting the class directly by using the function
} else {
auth.currentUser?.sendEmailVerification()?.await()
emit(State.failed(R.string.verify_email_first.toString()))
}
}
}.catch {
emit(State.failed(it.message!!))
}.flowOn(Dispatchers.IO)
Of course before the login I have a "sing up" method for this propuse. I don't added here because i thinks it is not adding more information about the code and question