I'm kinda stuck right now with my implementataion, when I execute my acceptAllBlood() the generateBlood() firing off instead but if I put cancel inside the combine I have an issue that I couldn't click my backpress but the acceptAllBlood() firing off, I tried already to debug it, so when calling this acceptBlood() the job of generateBlood() is still active.
here is my viewmodel:
generateBloodJob: Job? = null
markBloodJob: Job? = null
private fun generateBlood() {
if (generateBloodJob?.isActive == true) return
generateBloodJob = viewModelScope.launch {
combine(donorStreamed(), bloodAvaiable()) { donor, blood ->
val errors =
listOf(ErrorData, FailedToGetDonor, FailedToGetBloodAvialabilit)
when {
donor in errors || blood in errors -> _error.postValue(true)
else -> if (donor is ValidDonor) {
factory.donorTitle(donor.title.orEmpty())
bloodBatch(donor)
}
}
return@combine when (blood) {
is BloodData -> {
getBlood(blood)
}
else -> emptyList()
}
}.collect { value -> avaiableList.value = value }
}
}
fun acceptAllBlood() {
if (markBloodJob?.isActive == true) return
markBloodJob = viewModelScope.launch {
avaiableList.value.takeIf { it.isNotEmpty() }?.forEach { urlPath ->
acceptAllBloodApi(urlPath)
bloodAvaiable()
.filterIsInstance<MarkedAsAccept>()
.collect { status ->
if (!status.accepted) {
throw IllegalStateException("Can't mark donor in $urlPath")
}
}
}
}
}
generateBlood() here is initialized inside the init of the viewmodel.
acceptAllBlood() will fire when the user click donate button. but what happen is that if I click donate , generateBlood() is triggering. I added also generateBloodJob?.cancel() inside the combine but my backpress doesn't work but it fires the acceptAllBlood().