RxJava 2.0 was recently released so I thought I’d take a look at what’s new, the official Github Wiki has a good run down which I’d recommend reading. The easiest way for me to learn a new library (or relearn after a major update) is to start a guinea pig project and have a play around.

The point of this post is to show you how I came up with a solution for a Snackbar that allows me to subscribe to updates when a) the action is performed, and b) the Snackbar is dismissed, using RxJava 2.

Since you’re probably here from Google and aren’t interested in reading a lengthy article I’ll get straight to it. Here is the method as it appears in my Activity …

… and here’s how I’m using it in my Presenter.

Note that it’s generally a bad idea putting framework dependencies (View and resource references) in the presenter.

Now if you’re feeling a bit intimidated by this and have no idea what’s going on then stick around because I’ll be explaining the reason for going this route. There’s also an undo example at the bottom of this article.

Maybe you’ll continue reading, Maybe you’ll steal the code and leave!

Rx 2 has introduced a new reactive type called a Maybe which is described as a “union of Single and Completable providing the means to capture an emission pattern where there could be 0 or 1 item or an error signalled by some reactive source.” You either get something, nothing or an error.

Important: A Maybe flows differently to other reactive types in that only one of onSuccess, onError or onComplete will be called, each one signalling the termination of the emission. This means that a call to onSuccess is not followed by a call to onComplete and a call to onComplete does not mean that onSuccess has been called.

Creating the Maybe

One of the problems with creating your own instance of a reactive type through the create method is cleaning up after yourself. Since we’re creating a Snackbar and adding a listener we need to remove it when the Maybe instance is unsubscribed from.

This is where using comes in. The using method isn’t exclusive to Maybe and can be used on any of the reactive types, it’s also been around since Rx 1 but I’ve only just come across it when researching this solution. It allows you to bind the lifetime of a resource to the lifetime of an observable sequence. In this case, we only want the Snackbar to exist while the Subscriber is subscribed.

There’s three main parameters to this method which I’ll go through now.

Resource Supplier

The resource supplier is just a Callable that provides the resource to be used within the sequence. Here we’re just creating and returning the Snackbar, pretty simple stuff.

new Callable<Snackbar>() {

@Override

public Snackbar call() throws Exception {

return Snackbar.make(rootView, message, length);

}

}

Source Supplier

The second parameter is the meat of the method where you’ll be creating the actual MaybeSource that you will be subscribing to. RxJava 2.x no longer accepts null values and will yield NullPointerException immediately so I’m returning a boolean instead.

new Function<Snackbar, MaybeSource<Boolean>>() {

@Override

public MaybeSource<Boolean> apply(final Snackbar snackbar) throws Exception {

...

}

}

Inside the apply function is where we’ll be creating our Maybe using Maybe.Create() .

return Maybe.create(new MaybeOnSubscribe<Boolean>() {

@Override

public void subscribe(final MaybeEmitter<Boolean> e) throws Exception {

snackbar.setAction(action, v -> {

if (!e.isDisposed()) {

e.onSuccess(true);

}

});



snackbar.setCallback(new Callback() {

@Override

public void onDismissed(final Snackbar snackbar, final int event) {

if (!e.isDisposed()) {

e.onComplete();

}

}

});

snackbar.show();

}

});

Inside the create method is where we use the Snackbar by providing anonymous callbacks for click and dismiss events. Inside these callbacks are where we update the Subscriber with the relevant method. onSuccess signals that the user has clicked on the button while onComplete signals that the Snackbar has been dismissed without the user clicking the button.

Don’t forget snackbar.show() !

Resource Disposer

This is where the benefit of the using method comes into play, we simply create a new Consumer that disposes the resource we created in the Callable in the first parameter.

Here we’re just removing the Callbacks and nulling the Snackbar instance.

new Consumer<Snackbar>() {

@Override

public void accept(final Snackbar snackbar1) throws Exception {

snackbar1.setAction("", null);

snackbar1.setCallback(null);

snackbar1 = null;

}

})

Undo Snackbar using RxJava

It’s all well and good showing you the code but when will you need to use it? A good example is an Snackbar that provides an undo action. Let’s say you have a list of items in your View with the option to click delete on an item to remove it from the list. The Presenter can subscribe to events on the delete button and react by removing the item and providing the ability to undo.

Here’s the delete method in the View that the Presenter can subscribe to, the adapter has a PublishProcessor that is triggered when the user clicks on the delete button.

@Override

public Flowable<Item> onDeleteClicked() {

return adapter.onDeleteClicked();

} public Flowable onDeleteClicked() {return adapter.onDeleteClicked();

… and here’s how the Presenter deals with the delete button being clicked.

This is a simple example that doesn’t deal with removing or adding items from a database but you get the idea.