I am trying to use Room with RxJava and Retrofit, Before You recommend use a component arch (In this opportunity is not possible, the project is in and 50% and Just need to continue with the arch clean).
So the problem is this. I have a web service that returns a POJO. Something like this:
{
"success":"true",
"message":"message",
"data":{[
"id":"id",
"name":"name",
"lname":"lname",
]}
}
POJO is more complex but for the example is ok with this. I need to do that since my view make query to invoke data from room, but if there is not data in my db call my web services,the reponse of my web services transform to entity and save in my db (room) and after return a list of data to my view.
I am using clean arch. I appreciate anyhelp with this. Again not trying to use
data layout
- database
- network
- repository
domain
- interactor
- callbacks
presentation
- presenter
- view
POJO API response
{
"success":"true",
"message":"message",
"data":{[
"id":"id",
"name":"name",
"address":"address",
"phone":"phone",
]}
}
My db entity
@Entity(tableName = "clients")
public class clients {
String id;
String name;
String address;
String phone;
String status;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
My dao for room
@Dao
public interface ClientsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void saveAll(List<Clients> clients);
@Query("SELECT * FROM Clients")
Flowable<List<Clients>> listClients();
}
RxJava help class
public class RxHelper {
private static final String TAG = RxHelper.class.getName();
@NonNull
public static <T>Observable<T> getObserbable(@NonNull final Call<T> reponse){
return Observable.create(new ObservableOnSubscribe<T>() {
@Override
public void subscribe(final ObservableEmitter<T> emitter) throws Exception {
reponse.enqueue(new Callback<T>() {
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (!emitter.isDisposed()) {
emitter.onNext(response.body());
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
if (!emitter.isDisposed()) {
emitter.onError(t);
}
}
});
}
});
}
}
My ClientsRepoFactory
public Observable<ResponseClients> getApiClients(){
String token = preferences.getValue(SDConstants.token);
return RxHelper.getObserbable(apiNetwork.getClients(token));
}
My ClientsRepo
@Override
public Observable<ResponseClients> listClients() {
return factory.listClients();
}