Last active
October 16, 2019 02:39
-
-
Save BalakrishnanPT/7a3924467f4ca94f8bbf6c1abd763119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LifecycleOwner; | |
import androidx.lifecycle.LiveData; | |
import androidx.lifecycle.LiveDataReactiveStreams; | |
import androidx.lifecycle.MediatorLiveData; | |
import androidx.lifecycle.Observer; | |
import com.toyaround.android.communities.questionsActivity.network.BoredActivities; | |
import com.toyaround.android.communities.questionsActivity.network.BoredActivitiesAPI; | |
import io.reactivex.functions.Function; | |
import io.reactivex.schedulers.Schedulers; | |
/** | |
* Created by BalaKrishnan | |
*/ | |
public class APIExample { | |
BoredActivitiesAPI sampleAPI; | |
LifecycleOwner lifecycleOwner; | |
public APIExample(BoredActivitiesAPI boredActivitiesAPI, LifecycleOwner lifecycleOwner) { | |
this.sampleAPI = boredActivitiesAPI; | |
this.lifecycleOwner = lifecycleOwner; | |
} | |
public LiveData<String> makeAPICall() { | |
MediatorLiveData<String> linkData = new MediatorLiveData<>(); | |
LiveData<String> source = LiveDataReactiveStreams.fromPublisher( | |
sampleAPI.getFlowableResponse() | |
.onErrorReturn(new Function<Throwable, BoredActivities>() { | |
@Override | |
public BoredActivities apply(Throwable throwable) throws Exception { | |
BoredActivities boredActivities = new BoredActivities(); | |
boredActivities.setLink(""); | |
return boredActivities; | |
} | |
}) | |
.map(new Function<BoredActivities, String>() { | |
@Override | |
public String apply(BoredActivities boredActivities) throws Exception { | |
return boredActivities.getLink(); | |
} | |
}) | |
.subscribeOn(Schedulers.io())); | |
linkData.addSource(source, new Observer<String>() { | |
@Override | |
public void onChanged(String responseResponseWrapper) { | |
linkData.setValue(responseResponseWrapper); | |
linkData.removeSource(source); | |
} | |
}); | |
return linkData; | |
} | |
public void updateUI() { | |
makeAPICall().observe(lifecycleOwner, new Observer<String>() { | |
@Override | |
public void onChanged(String s) { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment