1

I have a fragment that contains a RecyclerView that displays search results of users. Each user row in the RecyclerView contains a Follow/Unfollow button, which upon clicking, causing the hosting fragment to execute the appropriate network call in its Presenter (using MVP).

The problem that I'm looking to solve, is how to make sure that if a User is hammering away at a follow/unfollow button on a given row, that a network call is only executed once (I know this particular problem could be solved with switchmap if there was only one button). However, since each row has a button, I do not want prior network calls to be cancelled if for example the user clicks Follow on one user, and then right away clicks Follow on a different user.

Here is an example of what the Fragment looks like:

enter image description here

Any suggestions on how to accomplish this would be appreciated.

EDIT: forgot to mention I am using RxJava for networking and I'm hoping to use it for the solution

sbearben
  • 323
  • 6
  • 16

3 Answers3

1

You mentioned switchMap, so you probably going to use RxJava for networking, in this case, I will suggest disabling buttons during network processing. In will look something like this:

followButton.clicks()
    .doOnNext { followButton.setEnabled(false) }
    .switchMap( your network Observable or Single)
    .subscribe {
        followButton.setEnable(true);
        followButton.setText("newText");
    }
Oknesif
  • 526
  • 5
  • 11
0

You need to keep the state of each row (e.g. following=t/f) and also whether the request to transition state (following -> not following and vice versa) has been issued (e.g. transitioning=t/f). Then the view should reflect this state and clicking on button should change it accordingly (e.g. make state is transitioning only if it's not already transitioning)

user3118604
  • 854
  • 6
  • 12
  • Since every row has a unique ID (user ID), what do you think of a solution whereby in the presenter each time a "follow/unfollow" network call is made, the disposable is added to a hashmap with the User ID as the key. If a button is clicked again and the given user who's follow button was clicked is already in the HashMap, then it is disposed and removed from the map. Is this a reasonable solution? – sbearben Aug 27 '18 at 16:43
  • So, just check if you HashTable has key to particular User ID and if has, so block a button. In this approach you have to remove User ID from HashTable after a task in this disposable chain is complete. – Oknesif Aug 28 '18 at 08:06
0

You could disable the button or remove it as soon as the user presses the button so they can't press it multiple times then reshow the button if the network call fails.

Another option would be to have the button emit an object on to a stream and do a take(1) on that stream to emit. Then if the network connection fails you could create a new button clicks stream and resubscribe.

Zachary Sweigart
  • 1,051
  • 9
  • 23