T
T
Tsuzukeru2021-10-10 11:21:54
Android
Tsuzukeru, 2021-10-10 11:21:54

How to execute 2 flow requests at the same time (MVI)?

How to simultaneously execute 2 flow requests to the server using the MVI architecture?
1)userInfoUseCase()
2)getCarsListUseCase()

private fun getUserInfo() {
        intent {
            userInfoUseCase()
                .onStart {
                    reduce { state.copy(isLoading = true, isHasError = false) }
                }
                .catch {
                    val errorText = resourceProvider.getString(R.string.something_went_wrong)
                    reduce {
                        state.copy(
                            isLoading = false,
                            isHasError = true
                        )
                    }
                }
                .onEach { user ->
                    driverInfoUseCase(user.id)
                        .onStart {
                            reduce { state.copy(isLoading = true, isHasError = false) }
                        }
                        .catch {
                            val errorText =
                                resourceProvider.getString(R.string.something_went_wrong)
                            reduce {
                                state.copy(
                                    isLoading = false,
                                    errorText = errorText,
                                    isHasError = true
                                )
                            }
                        }
                        .onEach { driver ->
                            getCarsListUseCase(
                                carsFilter = CarsFilter(
                                    pageNumber = FIRST_PAGE,
                                    pageSize = DEFAULT_PAGE_SIZE,
                                    orderSort = OrderSort()
                                )
                            )
                                .onStart {
                                    reduce { state.copy(isLoading = true, isHasError = false) }
                                }
                                .catch {
                                    val errorText =
                                        resourceProvider.getString(R.string.something_went_wrong)
                                    reduce {
                                        state.copy(
                                            isLoading = false,
                                            errorText = errorText,
                                            isHasError = true
                                        )
                                    }
                                }
                                .onEach { cars ->
                                    state.copy(
                                        id = user.id,
                                        avatar = driver.avatar,
                                        customerRating = driver.rating,
                                        name = driver.name,
                                        surname = driver.surname,
                                        additionalContacts = driver.additionalContacts,
                                        email = driver.email,
                                        phone = driver.phone,
                                        passportSerialNumber = user.passportSerialNumber,
                                        passportNumber = user.passportNumber,
                                        licenceData = user.licenseData,
                                        city = driver.city,
                                        organizationName = driver.organizationName,
                                        isLoading = false,
                                        isHasError = false,
                                        carsNumber = cars.totalElements
                                    )
                                }.collect()
                        }.collect()
                }.collect()
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question