What is the idiomatic way of refreshing using RxBinding on a SwipeRefreshLayout - android

I'm struggling to understand how to properly use RxBinding, if I want to call a network request when a user swipes down on a SwipeRefreshLayout, I would expect to say something like
RxSwipeRefreshLayout.refreshes(swipeContainer)
.flatMap { networkRequest() }
.subscribeBy(
onNext = { list: List<Data> -> Timber.d(data) },
onError = { showErrorSnackbar(it) },
onComplete = { Timber.d("On Complete") })
But this doesn't work for me, because I have that wrapped in a function called setupSwipeRefresh() which I call in onStart, so as soon as onStart is called the network request is made because that's when the layout is subscribed to.
Now I feel unsure about what to do. I could just put the whole thing in refreshListener but that kind of defeats the purpose of RxBinding.
Or I could execute the networkRequest in the onNext of the swipeContainer. But then it would look something like
RxSwipeRefreshLayout.refreshes(swipeContainer)
.subscribeBy(
onNext = {
networkRequest()
.subscribeBy(
onNext = { list: List<Data> ->
Timber.d(data)
})
},
onError = { showErrorSnackbar(it) },
onComplete = { Timber.d("On Complete") })
But calling subscribe twice just seems like an Anti-Pattern,
So yeah, since SwipeRefreshLayout is in the RxBinding library, there must be an idiomatic way of doing this, because it seems like the most common use case.

You are looking for something like this:
SwipeRefreshLayout viewById = findViewById(R.id.activity_main_swipe_refresh_layout);
Observable<State> swipe = RxSwipeRefreshLayout.refreshes(viewById)
.map(o -> new IsLoading());
Observable<State> stateObservable = Observable.<State>just(new IsLoading())
.mergeWith(swipe)
.switchMap(state -> Observable.concat(
Observable.just(new IsLoading()),
Observable.<State>timer(500, TimeUnit.MILLISECONDS)
.map(aLong -> new LoadingResult(Collections.emptyList())
)
)
).distinct();
stateObservable
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
state -> {
if (state instanceof IsLoading) {
Log.d("state", "isLoading");
} else if (state instanceof LoadingResult) {
Log.d("state", "loadingResult");
viewById.setRefreshing(false);
}
});
Events
interface State { }
class IsLoading implements State { }
class LoadingResult implements State {
private final List<String> result;
public LoadingResult(List<String> result) {
this.result = result;
}
}
SwitchMap is like FlatMap but it will switch over to the new observable and discard incomming events from previouse observable.

Related

RxJava - how to wait to result of async tasks wihiting doOnNext

How can I achieve that doOnNext wait to the results of multiple asynchronous tasks?
For example -
public void getImages(User user) {
Flowable.create(new FlowableOnSubscribe<User>() {
#Override
public void subscribe(#io.reactivex.rxjava3.annotations.NonNull FlowableEmitter<User> emitter) throws Throwable {
emitter.onNext(user);
}
}, BackpressureStrategy.BUFFER)
.observeOn(Schedulers.io())
.doOnNext(user -> {
ArrayList<String> imagesUrls = user.getUrls();
for (String url : imagesUrls) {
storage.getReference().child("images").child(url).getBytes(ParametersConventions.FIREBASE_DOWNLOAD_IMAGE_MAX_SIZE).
addOnSuccessListener(bytes -> {
doSomething(bytes);
});
}
})
.doOnNext(user -> {
doSomething();
})
.doOnComplete(...);
}
and I want that the doOnNext which calls to doSomething will be called after all the asynchronous calls to download the images are finished.
Turn that API call into a reactive type and merge it into the main flow:
int max = ParametersConventions.FIREBASE_DOWNLOAD_IMAGE_MAX_SIZE;
public Completable downloadAsync(URL url) {
return Completable.create(inner -> {
storage.getReference()
.child("images")
.child(url)
.getBytes(max)
.addOnSuccessListener(bytes -> {
doSomething(bytes);
inner.onComplete();
});
});
}
Together:
Flowable.create(emitter-> {
emitter.onNext(user);
}, BackpressureStrategy.BUFFER)
.observeOn(Schedulers.io())
.concatMapSingle(user ->
Flowable.fromIterable(user.getUrls())
.concatMapCompletable(url -> downloadAsync(url))
.andThen(Single.just(user))
)
.doOnNext(user -> {
doSomething();
})
.doOnComplete(...);
doOnNext operator is fired every time there is a new item on a stream so it is not the best option for you. Try using map/flatMap/concatMap operator depending on your needs. If you need to make several calls and then do something with the data you can look at similar question I've already answered link: Chaining API Requests with Retrofit + Rx
in which you can find a way to make sequential network calls and then do whatever you want with a list of data :D

Can an AsyncSubject handle the SingleLiveEvent case in RxJava?

Problem
The issue with reactive programming patterns for one-time events is that they may be re-emitted to the subscriber after the initial one-time event has occurred.
For LiveData the SingleLiveEvent provides a solution using an EventObserver which may also be applied to Kotlin Flow.
Question
Can an AsyncSubject observable be created to handle the case of the SingleLiveEvent in RxJava? The main issue seems to be if there a way for an AsyncSubject to be manually "re-opened" to re-emit data after onComplete is called?
Potential solution
AsyncSubject seems like a potential solution for RxJava, without creating an EventObserver, as the documentation states that it will only publish it when the sequence is completed.
Implementation - Loading status sample
A loading boolean is emitted from the ViewModel method initFeed and view effect state to the view, a fragment in this case. The loading boolean works as expected on the initialization of the fragment and ViewModel sending true via onNext, and completing with onComplete on either a successful or erroneous attempt.
However, the attempt to re-emit a value fails when for example a swipe to refresh initiates the same initFeed method. It seems that onNext cannot be used after onComplete is called for the same object.
SomeViewEffect.kt
data class _FeedViewEffect(
val _isLoading: AsyncSubject<Boolean> = AsyncSubject.create(),
)
data class FeedViewEffect(private val _viewEffect: _FeedViewEffect) {
val isLoading: AsyncSubject<Boolean> = _viewEffect._isLoading
}
SomeViewModel.kt
private fun initFeed(toRetry: Boolean) {
val disposable = feedRepository.initFeed(pagedListBoundaryCallback(toRetry))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { results ->
when (results.status) {
LOADING -> {
Log.v(LOG_TAG, "initFeed ${LOADING.name}")
_viewEffect._isLoading.onNext(true)
}
SUCCESS -> {
Log.v(LOG_TAG, "initFeed ${SUCCESS.name}")
_viewEffect._isLoading.onNext(false)
_viewEffect._isLoading.onComplete()
_viewState._feed.onNext(results.data)
}
ERROR -> {
Log.v(LOG_TAG, "initFeed ${ERROR.name}")
_viewEffect._isLoading.onNext(false)
_viewEffect._isLoading.onComplete()
_viewEffect._isError.onNext(true)
}
}
}
disposables.add(disposable)
}
SomeFragment.kt
private fun initViewEffects() {
val isLoadingDisposable = viewModel.viewEffect.isLoading
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { Log.v(LOG_TAG, "Error loading isLoading") }
.subscribe { isLoading ->
if (isLoading) progressBar.visibility = VISIBLE
else {
progressBar.visibility = GONE
swipeToRefresh.isRefreshing = false
}
}
compositeDisposable.addAll(isLoadingDisposable, isErrorDisposable)
}
It is not very clear why you need AsyncSubject which emits only last event. Did you try to use Behavior or Publish Processor for this situation?
Use an Event Wrapper
An AsyncSubject does not seem to be a suitable solution to handle one-time occurrence emissions from an Observable to a Subscriber. After onComplete is called an AsyncSubject can not "re-open" to emit future one-time events.
Using an event wrapper such as an Event, as outlined in LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case) is the best approach.
FeedViewEffect.kt
data class _FeedViewEffect(
val _isLoading: BehaviorSubject<Event<Boolean>> = BehaviorSubject.create()
)
data class FeedViewEffect(private val _viewEffect: _FeedViewEffect) {
val isLoading: BehaviorSubject<Event<Boolean>> = _viewEffect._isLoading
}
FeedViewModel.kt
private fun initFeed(toRetry: Boolean) {
val disposable = feedRepository.initFeed(pagedListBoundaryCallback(toRetry))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { results ->
when (results.status) {
LOADING -> _viewEffect._isLoading.onNext(Event(true))
SUCCESS -> _viewEffect._isLoading.onNext(Event(false))
ERROR -> _viewEffect._isLoading.onNext(Event(false))
}
}
disposables.add(disposable)
}
FeedFragment.kt
#ExperimentalCoroutinesApi
private fun initViewEffects() {
val isLoadingDisposable = viewModel.viewEffect.isLoading
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { Log.v(LOG_TAG, "Error loading isLoading") }
.subscribe { isLoading ->
if (isLoading.getContentIfNotHandled() == true) {
progressBar.visibility = VISIBLE
} else {
progressBar.visibility = GONE
swipeToRefresh.isRefreshing = false
}
}
compositeDisposable.addAll(isLoadingDisposable)
}

Chaining Calls on RxJava

There are cases when I need to chain RxJava calls.
The simplest one:
ViewModel:
fun onResetPassword(email: String) {
...
val subscription = mTokenRepository.resetPassword(email)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
//UI update calls
)
...
}
My Repository:
fun resetPassword(email: String): Single<ResetPassword> {
return Single.create { emitter ->
val subscription = mSomeApiInterface.resetPassword(email)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe({
emitter.onSuccess(...)
}, { throwable ->
emitter.onError(throwable)
})
...
}
}
My Question
Do I need to Add:
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
for both calls to avoid any app freeze? or the second one for API call is enough?
No, you don't need to add
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
for the repo and the viewmodel.
.observeOn usually should be called right before handling the ui rendering. So usually, you'll need it in the ViewModel right before updating the ui or emitting the LiveData values.
Also, you properly don't need to subscribe to mSomeApiInterface in your repo, I think it would be better off to just return in as it's from your method up the chain, somthing like this:
fun resetPassword(email: String): Single<ResetPassword> {
return mSomeApiInterface.resetPassword(email);
}
and if you have any mapping needed you can chain it normally
fun resetPassword(email: String): Single<ResetPassword> {
return mSomeApiInterface.resetPassword(email)
.map{it -> }
}
This way you can write your ViewModel code as follow
fun onResetPassword(email: String) {
...
// note the switcing between subscribeOn and observeOn
// the switching is in short: subscribeOn affects the upstream,
// while observeOn affects the downstream.
// So we want to do the work on IO thread, then deliver results
// back to the mainThread.
val subscription = mTokenRepository.resetPassword(email)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
//UI update calls
)
...
}
This will run the API request on the io thread, will returning the result on the mainThread, which is probably what you want. :)
This artical has some good examples and explanations for subscribeOn and observeOn, I strongly recommend checking it.
Observable<RequestFriendModel> folderAllCall = service.getUserRequestslist(urls.toString());
folderAllCall.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(result -> result.getRequested())
.subscribe(this::handleResults, this::handleError);
private void handleResults(List<Requested> folderList) {
if (folderList != null && folderList.size() != 0) {
usersList.addAll(folderList);
}
adapter.notifyDataSetChanged();
}
}
private void handleError(Throwable t) {
Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
in interface:
#Headers({ "Content-Type: application/json;charset=UTF-8"})
#GET
Observable<RequestFriendModel> getUserRequestslist(#Url String url);
POJO model :
public class RequestFriendModel {
#SerializedName("requested")
#Expose
private List<Requested> requested = null;
public List<Requested> getRequested() {
return requested;
}
public void setRequested(List<Requested> requested) {
this.requested = requested;
}
}

Pass value from single across completable to result in rxjava Android

This is what I want:
Check if I have data about products in database.
If I have data I run Single to get data from DB.
If not I run Single for get data from backend
If I get response I want to save data in DB using Completable.
After saving data I want to map values from step 2 or 3 to view model
In result I want to send data to activity.
This is what I have now:
checkProductsInDBUseCase.run()
.flatMap {
if (it) {
getProductsFromDBUseCase.run()
} else {
getProductsUseCase.run(3)
}
}.map {
it.products.map { item -> item.toViewModel() }
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
view.showBikes(it)
},
onError = {
view.showBikesError(it.message.toString())
}
).addTo(disposables)
Between flat map and map I need to run saveDataUseCase(it), but I don't know how to pass itfrom completable to map. Any ideas?
If your saveDataUseCase() is Completable then you can do this
checkProductsInDBUseCase.run()
.flatMap {
if (it) {
getProductsFromDBUseCase.run()
} else {
getProductsUseCase.run(3)
}
}
.faltMap {
saveDataUseCase(it).toSingleDefault(it)
}
.map {
it.products.map { item -> item.toViewModel() }
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
view.showBikes(it)
},
onError = {
view.showBikesError(it.message.toString())
}
).addTo(disposables)
But if you change return type of saveDataUseCase() to Unit, you can use Fred's answer. It would be better
Here I'd use doOnSuccess. This seems ideal especially because you're creating a side effect, which we usually use the doOnXXX methods for.
checkProductsInDBUseCase.run()
.flatMap {
if (it) {
getProductsFromDBUseCase.run()
} else {
getProductsUseCase.run(3)
}
}
.doOnSuccess {
saveDataUseCase(it)
}
.map {
it.products.map { item -> item.toViewModel() }
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onSuccess = {
view.showBikes(it)
},
onError = {
view.showBikesError(it.message.toString())
}
).addTo(disposables)
The method will not change the result of the flatMap so you will still get the correct object inside the map function.

RxJava/RxKotlin complains about accessing views

I have the following call to retrieve some data from server and update the UI according to response.
poiAPIService.getPoiDetails(poiId!!)
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { showProgressBar(true) }
.doFinally { showProgressBar(false) }
.subscribeOn(Schedulers.io()).subscribe(
{ poiDetails ->
bindPoiDetails(poiDetails)
},
{
(getActivity() as MainOverviewActivity).fragmentControl.hidePoiDetailsFragment()
})
}
It complains about showProgressBar that the Views are only accessable on thread that created them.
If I change the call like this, everything seems to be fine again.
showProgressBar(true)
poiAPIService.getPoiDetails(poiId!!)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(
{ poiDetails ->
showProgressBar(false)
bindPoiDetails(poiDetails)
},
{
showProgressBar(false)
(getActivity() as MainOverviewActivity).fragmentControl.hidePoiDetailsFragment()
})
}
I have done by using below code, using RxJava 2.x
poiAPIService.getPoiDetails(poiId!!)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Consumer < Disposable >() {
#Override
public void accept(Disposable disposable) throws Exception {
showProgressBar(true);
}
})
.doFinally(new Action () {
#Override
public void run() throws Exception {
showProgressBar(false);
}
})
.subscribe(/**your subscription here**/);
Try using above code and let me know.
did you tried to do something like this...
poiAPIService.getPoiDetails(poiId!!)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.doOnSubscribe { showProgressBar(true) }
.doFinally { showProgressBar(false) }
.subscribe(
{ poiDetails ->
bindPoiDetails(poiDetails)
},
{
(getActivity() as MainOverviewActivity).fragmentControl.hidePoiDetailsFragment()
})
pay attention to observeOn and subscribeOn
Looks like you use observeOn and subscribeOn not correctly...
take a look to How RXJava Scheduler/Threading works for different operator?

Categories

Resources