Retrofit 2 LiveData adapter

A CallAdapter.Factory for Android LiveData

License

License

Categories

Categories

Data Retrofit Net HTTP Clients
GroupId

GroupId

com.github.leonardoxh
ArtifactId

ArtifactId

retrofit2-livedata-adapter
Last Version

Last Version

1.1.2
Release Date

Release Date

Type

Type

aar
Description

Description

Retrofit 2 LiveData adapter
A CallAdapter.Factory for Android LiveData
Project URL

Project URL

https://github.com/leonardoxh/livedata-call-adapter
Source Code Management

Source Code Management

https://github.com/leonardoxh/livedata-call-adapter

Download retrofit2-livedata-adapter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.leonardoxh/retrofit2-livedata-adapter/ -->
<dependency>
    <groupId>com.github.leonardoxh</groupId>
    <artifactId>retrofit2-livedata-adapter</artifactId>
    <version>1.1.2</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.leonardoxh/retrofit2-livedata-adapter/
implementation 'com.github.leonardoxh:retrofit2-livedata-adapter:1.1.2'
// https://jarcasting.com/artifacts/com.github.leonardoxh/retrofit2-livedata-adapter/
implementation ("com.github.leonardoxh:retrofit2-livedata-adapter:1.1.2")
'com.github.leonardoxh:retrofit2-livedata-adapter:aar:1.1.2'
<dependency org="com.github.leonardoxh" name="retrofit2-livedata-adapter" rev="1.1.2">
  <artifact name="retrofit2-livedata-adapter" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.leonardoxh', module='retrofit2-livedata-adapter', version='1.1.2')
)
libraryDependencies += "com.github.leonardoxh" % "retrofit2-livedata-adapter" % "1.1.2"
[com.github.leonardoxh/retrofit2-livedata-adapter "1.1.2"]

Dependencies

compile (2)

Group / Artifact Type Version
com.squareup.retrofit2 : retrofit jar 2.4.0
android.arch.lifecycle » livedata jar 1.1.1

Project Modules

There are no modules declared in this project.

Android Arsenal Build Status

LiveData Call Adapter for Retrofit

A Retrofit 2 CallAdapter.Factory for Android LiveData

Usage

Add LiveDataCallAdapterFactory as a Call adapter when building your Retrofit instance:

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://example.com")
        .addCallAdapterFactory(LiveDataCallAdapterFactory.create())
        .addConverterFactory(LiveDataResponseBodyConverterFactory.create())
        .addConverterFactory(YourConverterFactory.create())
        .build();

Your service methods can now use LiveData as their return type, but, note that we also have a LiveDataResponseBodyConverterFactory this wrapper is necessary when you have a converter that touch on the return type like moshi or gson, to bypass the Resource<T> class and give to the Converter.Factory the correct type to serialize.

public interface SuperService {
    @GET("/pimba") LiveData<Resource<Pimba>> getPimba();
    @GET("/pimba") LiveData<Response<Resource<Pimba>>> getPimbas();
}

Please note the usage of the Resource object, it is required to provide the error callback to the LiveData object, so when you want verify what is happening on your network call for example:

retrofit.create(SuperService.class)
        .getPimba()
        .observe(this, new Observer<Resource<Pimba>>() {
            @Override
            public void onChange(Resource<Pimba> resource) {
                if (resource.isSuccess()) {
                    //doSuccessAction with resource.resource
                } else {
                    //doErrorAction with resource.error
                }
            }
        })

Gradle dependency

dependencies {
    implementation "com.github.leonardoxh:retrofit2-livedata-adapter:1.1.2"
}

Inspiration

License

Copyright 2018 Leonardo Rossetto

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
1.1.2
1.1.1
1.1.0
1.0.2
1.0.1
1.0.0