Skip to content

1.0.9

Choose a tag to compare

@skydoves skydoves released this 18 Jan 15:13
5769eb2

馃帀 Released a new version 1.0.9! 馃帀

What's New?

- onSuccess and suspendOnSuccess can receive ApiSuccessModelMapper as a parameter.

If we want to get the transformed data from the start in the lambda, we can pass the mapper as a parameter for the suspendOnSuccess.

.suspendOnSuccess(SuccessPosterMapper) {
    val poster = this
}

- onError and suspendOnError can receive ApiErrorModelMapper as a parameter.

// Maps the ApiResponse.Failure.Error to a custom error model using the mapper.
response.onError(ErrorEnvelopeMapper) {
    val code = this.code
    val message = this.message
}

- Added a new extension toLiveData and toSuspendLiveData with a transformer lambda.

If we want to transform the original data and get a LiveData which contains transformed data using successful data if the response is a ApiResponse.Success.

posterListLiveData = liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
  emitSource(
   disneyService.fetchDisneyPosterList()
    .onError {
      // handle the error case
    }.onException {
      // handle the exception case
    }.toLiveData {
      this.onEach { poster -> poster.date = SystemClock.currentThreadTimeMillis() }
    }) // returns an observable LiveData
   }

- Added a new extension toFlow and toSsuspendFlow with a transformer lambda.

We can get a Flow that emits successful data if the response is an ApiResponse.Success and the data is not null.

disneyService.fetchDisneyPosterList()
  .onError {
    // stub error case
  }.onException {
    // stub exception case
  }.toFlow() // returns a coroutines flow
  .flowOn(Dispatchers.IO)

If we want to transform the original data and get a flow that contains transformed data using successful data if the response is an ApiResponse.Success and the data is not null.

val response = pokedexClient.fetchPokemonList(page = page)
response.toFlow { pokemons ->
  pokemons.forEach { pokemon -> pokemon.page = page }
  pokemonDao.insertPokemonList(pokemons)
  pokemonDao.getAllPokemonList(page)
}.flowOn(Dispatchers.IO)

- Added a new transformer onProcedure and suspendOnProcedure expressions.

We can pass onSuccess, onError, and onException as arguments.

.suspendOnProcedure(
              // handle the case when the API request gets a successful response.
              onSuccess = {
                Timber.d("$data")

                data?.let { emit(it) }
              },
              // handle the case when the API request gets an error response.
              // e.g., internal server error.
              onError = {
                Timber.d(message())

                // handling error based on status code.
                when (statusCode) {
                  StatusCode.InternalServerError -> toastLiveData.postValue("InternalServerError")
                  StatusCode.BadGateway -> toastLiveData.postValue("BadGateway")
                  else -> toastLiveData.postValue("$statusCode(${statusCode.code}): ${message()}")
                }

                // map the ApiResponse.Failure.Error to a customized error model using the mapper.
                map(ErrorEnvelopeMapper) {
                  Timber.d("[Code: $code]: $message")
                }
              },
              // handle the case when the API request gets a exception response.
              // e.g., network connection error.
              onException = {
                Timber.d(message())
                toastLiveData.postValue(message())
              }
            )