I have a function for the user's login. But it is suspended. I try to get its return value, but I can't. Here's what I tried to do
Code
class LoginViewModel @ViewModelInject constructor(private val remoteDataSource: OrderRemoteDataSource) :
ViewModel() {
private fun areValidCredentials(username: String?, password: String?): Boolean {
return username != null && password != null && username.length > 4 && password.length > 4
}
suspend fun login(username: String?, password: String?): Boolean {
return suspendCoroutine { it ->
val valid = areValidCredentials(username, password)
if (valid) {
// call finish so login activity won't show up after back button clicked in home fragment
try {
viewModelScope.launch {
//TODO CHECK if error code
val loginResponse =
remoteDataSource.login(LoginRequest(username!!, password!!))
if (loginResponse.status == Resource.Status.SUCCESS) {
val jwtToken = loginResponse.data?.jwtToken
if (!jwtToken.isNullOrEmpty()) {
sessionManager.saveAuthToken(jwtToken!!)
//ERROR!
it.resume(true)
}
}
}
} catch (e: Exception) {
Log.i("[LoginActivity]", e.localizedMessage!!)
it.resume(false)
e.printStackTrace()
}
} else {
Toast.makeText(
LOGIN_ACTIVITY,
"Username and password must be at least 5 letters long",
Toast.LENGTH_SHORT
).show()
}
it.resume(false)
}
}
}
And i call it
@AndroidEntryPoint
class LoginFragment : Fragment() {
private val mViewModel: LoginViewModel by viewModels()
private lateinit var navController: NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.frg_login, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("[LoginFragment]", "onCreate fun started!")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = findNavController()
loginButton.setOnClickListener {
//TODO navigate to new fragmnet
lifecycleScope.launch {
mViewModel.login(
loginUsernameText.text.toString(),
loginPasswordText.text.toString()
)
}
}
}
And i have error
E/AndroidRuntime: FATAL EXCEPTION: main Process: ru.gkomega.navigation, PID: 11863 java.lang.IllegalStateException: Already resumed at kotlin.coroutines.SafeContinuation.resumeWith(SafeContinuationJvm.kt:45) at ru.gkomega.maumarket.ui.login.LoginViewModel$login$$inlined$suspendCoroutine$lambda$1.invokeSuspend(LoginViewModel.kt:40) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) I/chatty: uid=10163(ru.gkomega.navigation) identical 16 lines W/mega.navigatio: Got a deoptimization request on un-deoptimizable method java.lang.Class java.lang.Class.classForName(java.lang.String, boolean, java.lang.ClassLoader) I/Process: Sending signal. PID: 11863 SIG: 9 Disconnected from the target VM, address: 'localhost:58264', transport: 'socket'
I don't know much about coroutines so they're probably the problem