BRDroid
BRDroid

Reputation: 4388

Manually create an observable of an object in Kotlin and RX observable

I have a written a function which should return Observable<GenericResponse>. I want to manually create the Observable<GenericResponse>. Is that possible?

 fun notifyOnStand(serviceType: String, id: String, action: TimestampedAction): Observable<GenericResponse>? {
        if (libraryOfflineDBHelper.getIsNetworkAvailableNow()) {
            return api.serviceOrderOnStand(serviceType, id, action)
        } else {
            val onStandApiData = JsonObject()
            onStandApiData.addProperty("serviceType", serviceType)
            onStandApiData.addProperty("id", id)
            onStandApiData.addProperty("action", action.toString())
            var result: Int = 0
            launch {
                 result = offlineDatabaseManager.insertOfflineData(OfflineData(0, onStandApiData.toString(), "notifyOnstand"))
            }
            if(result > 0) {
                val genericResponse = GenericResponse(true)
                return Observable<genericResponse> //***THIS IS WHAT I WANT TO RETURN
            }
        }
    } 

public class GenericResponse {

    @Expose
    @OnComplete
    protected Boolean success = false;

    @Expose
    @OnComplete
    protected String error;

    public GenericResponse() {}

    public GenericResponse(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return success;
    }

    public GenericResponse setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public String getError() {
        return error;
    }

    public GenericResponse setError(String error) {
        this.error = error;
        return this;
    }
}

Just an FYI on what this function is doing, It will check if there is network connection then it will call existing retrofit api calls which return an observable of GenericResponse so it works fine. If there is no network connection I insert data into the database and depending on that response, I want to manually create Observable<GenericResponse>.

How can I do that?

Upvotes: 0

Views: 1563

Answers (1)

dano
dano

Reputation: 94871

You can use Observable.just to do this:

return Observable.just(genericResponse)

Upvotes: 2

Related Questions