/result-builder

"Simplify asynchronous execution with Coroutines using a Builder pattern inspired by Flow operators."

Primary LanguageKotlin

result-builder

Simplify asynchronous execution with Coroutines using a Builder pattern inspired by Flow operators.

The article provides a clear overview of the use of a builder pattern to manage async calls in a clean architecture Android project. It also discusses the benefits of using the builder pattern and how it can improve the readability and maintainability of code, as well as the potential risks of using a long-lived scope when launching coroutines.

See more about this project on Medium

How to use this ResultBuilder?

class MainViewModel(
    private val getAddressUseCase: GetAddressUseCase,
) : ViewModel() {

    ...

    fun getAddress(code: String) {
        runAsyncSafely {
            getAddressUseCase(code)
        }.onStart {
            _state.update { it.copy(shouldShowLoading = true) }
        }.onCompletion {
            _state.update { it.copy(shouldShowLoading = false) }
        }.onFailure { cause ->
            getAddressFailure(cause)
        }.onSuccess { address ->
            getAddressSuccess(address)
        }.launchIn(viewModelScope)
    }

    ...
}

In the project you can see more details about this class and all implementation and tests.

result-builder

Simplify asynchronous execution with Coroutines using a Builder pattern inspired by Flow operators.

The article provides a clear overview of the use of a builder pattern to manage async calls in a clean architecture Android project. It also discusses the benefits of using the builder pattern and how it can improve the readability and maintainability of code, as well as the potential risks of using a long-lived scope when launching coroutines.

See more about this project on Medium

How to use this ResultBuilder?

class MainViewModel(
    private val getAddressUseCase: GetAddressUseCase,
) : ViewModel() {

    ...

    fun getAddress(code: String) {
        runAsyncSafely {
            getAddressUseCase(code)
        }.onStart {
            _state.update { it.copy(shouldShowLoading = true) }
        }.onCompletion {
            _state.update { it.copy(shouldShowLoading = false) }
        }.onFailure { cause ->
            getAddressFailure(cause)
        }.onSuccess { address ->
            getAddressSuccess(address)
        }.launchIn(viewModelScope)
    }

    ...
}

In the project you can see more details about this class and all implementation and tests.

image