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)
}
...
}
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)
}
...
}