All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nucleus.presenter.delivery.DeliverReplay Maven / Gradle / Ivy

Go to download

Nucleus is an Android library, which helps to use the Model-View-Presenter pattern

The newest version!
package nucleus.presenter.delivery;

import rx.Notification;
import rx.Observable;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.subjects.ReplaySubject;

public class DeliverReplay implements Observable.Transformer> {

    private final Observable view;

    public DeliverReplay(Observable view) {
        this.view = view;
    }

    @Override
    public Observable> call(Observable observable) {
        final ReplaySubject> subject = ReplaySubject.create();
        final Subscription subscription = observable
            .materialize()
            .filter(new Func1, Boolean>() {
                @Override
                public Boolean call(Notification notification) {
                    return !notification.isOnCompleted();
                }
            })
            .subscribe(subject);
        return view
            .switchMap(new Func1>>() {
                @Override
                public Observable> call(final View view) {
                    return view == null ? Observable.>never() : subject
                        .map(new Func1, Delivery>() {
                            @Override
                            public Delivery call(Notification notification) {
                                return new Delivery<>(view, notification);
                            }
                        });
                }
            })
            .doOnUnsubscribe(new Action0() {
                @Override
                public void call() {
                    subscription.unsubscribe();
                }
            });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy