miqbaldc
09/01/2022, 3:44 AMasync {
builder inside intent {
?
Code in 🧵miqbaldc
09/01/2022, 3:47 AMfun uploadFile(uploadsBody: List<Pair<String, UploadBody>?>) = intent {
viewModelScope.launch {
val listDeferred = mutableListOf<Deferred<UploadFileModel>>()
uploadsBody.forEach { pair ->
if (pair != null) {
val deferred = async {
val result = uploadPhotoUseCase(pair.second).catch {
throw CancellationException("Gagal unggah berkas", it)
}.first()
return@async UploadFileModel(pair.first, pair.second.file, result)
}
listDeferred.add(deferred)
runCatching {
reduce {
state.copy(isLoadingButton = true)
}
listDeferred.awaitAll()
}.onSuccess { uploadedFiles ->
reduce {
state.copy(isLoadingButton = false)
}
postSideEffect(
sideEffect = FormApprovalEffect.SuccessUploadFiles(uploadedFiles),
)
}.onFailure { throwable ->
val error = errorDictionaryUseCase(throwable)
reduce {
state.copy(isLoadingButton = false)
}
postSideEffect(
sideEffect = FormApprovalEffect.ShowErrorGeneral(
summary = error.summary,
detail = error.detail,
)
)
}
}
}
}
}
but, based on previous thread in here, any other suggestion to improve it?miqbaldc
09/01/2022, 3:49 AMfun uploadFile(uploadsBody: List<Pair<String, UploadBody>?>) = intent {
coroutineScope {
launch(Dispather.Default) {
// calls the `async` builder in here(?)
}
}
}
based on: https://kotlinlang.slack.com/archives/CPM6UMD2P/p1623832989030700?thread_ts=1623832162.029000&cid=CPM6UMD2P