Lena Stepanova
02/12/2021, 10:40 AMviewModelScope.launch {
validateEmail(user)?.let {
_errorLiveData.postValue(it)
stopLoading()
return@launch
}
}
And I want it to look something like
evaluate(validateEmail(user), launch)
with
fun evaluate(result: String?, job: (
context: CoroutineContext,
start: CoroutineStart,
block: suspend CoroutineScope.() -> Unit
) -> Job){
result?.let {
_errorLiveData.postValue(it)
stopLoading()
return@job
}
}
I get job is unresolved, so what am I doing wrong and how to make it right?wasyl
02/12/2021, 10:45 AMLena Stepanova
02/12/2021, 10:55 AMvalidateIpData(it)?.let {
_errorLiveData.postValue(it)
return@launch
}
Inside viewmodelScope.launch:
viewModelScope.launch {
// if cloud login, validate username as email
validateEmail(user)?.let {
_errorLiveData.postValue(it)
stopLoading()
return@launch
}
loginModel.login(user)?.let { it ->
_errorLiveData.postValue(it)
return@launch
}
// if success, move to device list view
_navigateToDeviceList.postValue(username.value)
}
And I want to make it more readablewasyl
02/12/2021, 11:05 AMBoolean
from your validation functions or e.g. throwing an exception if validation fails. Keeping it simple would probably help readability more than a fancy evaluate function which takes a while to understand in couple of months 🙂myanmarking
02/12/2021, 11:05 AMmyanmarking
02/12/2021, 11:08 AMmyanmarking
02/12/2021, 11:10 AMmyanmarking
02/12/2021, 11:10 AMmyanmarking
02/12/2021, 11:10 AMmyanmarking
02/12/2021, 11:12 AMmyanmarking
02/12/2021, 11:14 AMmyanmarking
02/12/2021, 11:14 AMLena Stepanova
02/12/2021, 11:15 AM{
_errorLiveData.postValue(it)
stopLoading()
return@launch
}
And there are actually more validations, which I wanted to make less pronounced, so that it basically reads like {
validateEmail()
validateUsername()
validateIp()
...
}Lena Stepanova
02/12/2021, 11:15 AMmyanmarking
02/12/2021, 11:18 AMmyanmarking
02/12/2021, 11:18 AMmyanmarking
02/12/2021, 11:20 AMmyanmarking
02/12/2021, 11:20 AMmyanmarking
02/12/2021, 11:20 AM