Search in sources :

Example 16 with Observer

use of io.reactivex.Observer in project RxJava by ReactiveX.

the class ObservableSubscriberTest method testOnStartCalledOnceViaLift.

@Test
public void testOnStartCalledOnceViaLift() {
    final AtomicInteger c = new AtomicInteger();
    Observable.just(1, 2, 3, 4).lift(new ObservableOperator<Integer, Integer>() {

        @Override
        public Observer<? super Integer> apply(final Observer<? super Integer> child) {
            return new DefaultObserver<Integer>() {

                @Override
                public void onStart() {
                    c.incrementAndGet();
                }

                @Override
                public void onComplete() {
                    child.onComplete();
                }

                @Override
                public void onError(Throwable e) {
                    child.onError(e);
                }

                @Override
                public void onNext(Integer t) {
                    child.onNext(t);
                }
            };
        }
    }).subscribe();
    assertEquals(1, c.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(io.reactivex.Observer) Test(org.junit.Test)

Example 17 with Observer

use of io.reactivex.Observer in project RxJava by ReactiveX.

the class RxJavaPluginsTest method observableStart.

@SuppressWarnings("rawtypes")
@Test
public void observableStart() {
    try {
        RxJavaPlugins.setOnObservableSubscribe(new BiFunction<Observable, Observer, Observer>() {

            @Override
            public Observer apply(Observable o, final Observer t) {
                return new Observer() {

                    @Override
                    public void onSubscribe(Disposable d) {
                        t.onSubscribe(d);
                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object value) {
                        t.onNext((Integer) value - 9);
                    }

                    @Override
                    public void onError(Throwable e) {
                        t.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        t.onComplete();
                    }
                };
            }
        });
        Observable.range(10, 3).test().assertValues(1, 2, 3).assertNoErrors().assertComplete();
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    Observable.range(10, 3).test().assertValues(10, 11, 12).assertNoErrors().assertComplete();
}
Also used : Observer(io.reactivex.Observer) Observable(io.reactivex.Observable) ConnectableObservable(io.reactivex.observables.ConnectableObservable)

Example 18 with Observer

use of io.reactivex.Observer in project SpotiQ by ZinoKader.

the class LobbyPresenter method createParty.

void createParty(String partyTitle, String partyPassword) {
    Party party = new Party(partyTitle, partyPassword);
    Observable.zip(partiesRepository.getParty(party.getTitle()), spotifyRepository.getMe(spotifyCommunicatorService.getWebApi()), (dbParty, spotifyUser) -> {
        if (dbParty.exists()) {
            throw new PartyExistsException();
        } else {
            User user = new User(spotifyUser.id, spotifyUser.display_name, spotifyUser.images);
            party.setHostSpotifyId(user.getUserId());
            return new UserPartyInformation(user, party);
        }
    }).flatMap(userPartyInformation -> Observable.zip(partiesRepository.createNewParty(userPartyInformation.getParty()), partiesRepository.addUserToParty(userPartyInformation.getUser(), userPartyInformation.getParty().getTitle()), (partyWasCreated, userWasAdded) -> {
        if (!partyWasCreated)
            throw new PartyNotCreatedException();
        if (!userWasAdded)
            throw new UserNotAddedException();
        return userPartyInformation.getParty();
    })).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Party>() {

        @Override
        public void onNext(Party party) {
            navigateToParty(party.getTitle());
        }

        @Override
        public void onError(Throwable exception) {
            if (exception instanceof PartyExistsException) {
                getView().showMessage("Party " + partyTitle + " already exists");
            } else if (exception instanceof UserNotAddedException) {
                getView().showMessage("Something went wrong when adding you to the party");
            } else {
                getView().showMessage("Something went wrong when creating the party");
            }
            Log.d(LogTag.LOG_LOBBY, "Could not create party");
            exception.printStackTrace();
        }

        @Override
        public void onComplete() {
        }

        @Override
        public void onSubscribe(Disposable d) {
        }
    });
}
Also used : PartiesRepository(se.zinokader.spotiq.repository.PartiesRepository) Bundle(android.os.Bundle) UserPrivate(kaaes.spotify.webapi.android.models.UserPrivate) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Inject(javax.inject.Inject) SpotifyRepository(se.zinokader.spotiq.repository.SpotifyRepository) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) FirebaseConstants(se.zinokader.spotiq.constant.FirebaseConstants) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Response(retrofit.client.Response) Log(android.util.Log) PartyNotCreatedException(se.zinokader.spotiq.util.exception.PartyNotCreatedException) LogTag(se.zinokader.spotiq.constant.LogTag) BasePresenter(se.zinokader.spotiq.feature.base.BasePresenter) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) UserNotAddedException(se.zinokader.spotiq.util.exception.UserNotAddedException) Party(se.zinokader.spotiq.model.Party) TimeUnit(java.util.concurrent.TimeUnit) Callback(retrofit.Callback) ApplicationConstants(se.zinokader.spotiq.constant.ApplicationConstants) Disposable(io.reactivex.disposables.Disposable) PartyDoesNotExistException(se.zinokader.spotiq.util.exception.PartyDoesNotExistException) RetrofitError(retrofit.RetrofitError) Observer(io.reactivex.Observer) SpotifyCommunicatorService(se.zinokader.spotiq.service.SpotifyCommunicatorService) User(se.zinokader.spotiq.model.User) PartyWrongPasswordException(se.zinokader.spotiq.util.exception.PartyWrongPasswordException) Disposable(io.reactivex.disposables.Disposable) Party(se.zinokader.spotiq.model.Party) User(se.zinokader.spotiq.model.User) PartyExistsException(se.zinokader.spotiq.util.exception.PartyExistsException) UserPartyInformation(se.zinokader.spotiq.model.UserPartyInformation) PartyNotCreatedException(se.zinokader.spotiq.util.exception.PartyNotCreatedException) UserNotAddedException(se.zinokader.spotiq.util.exception.UserNotAddedException)

Aggregations

Observer (io.reactivex.Observer)18 Observable (io.reactivex.Observable)12 Test (org.junit.Test)9 TestException (io.reactivex.exceptions.TestException)8 TestObserver (io.reactivex.observers.TestObserver)6 ConnectableObservable (io.reactivex.observables.ConnectableObservable)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Bundle (android.os.Bundle)2 Log (android.util.Log)2 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)2 Disposable (io.reactivex.disposables.Disposable)2 ObserveOnObserver (io.reactivex.internal.operators.observable.ObservableObserveOn.ObserveOnObserver)2 Schedulers (io.reactivex.schedulers.Schedulers)2 TimeUnit (java.util.concurrent.TimeUnit)2 Inject (javax.inject.Inject)2 UserPrivate (kaaes.spotify.webapi.android.models.UserPrivate)2 Callback (retrofit.Callback)2 RetrofitError (retrofit.RetrofitError)2 Response (retrofit.client.Response)2 ApplicationConstants (se.zinokader.spotiq.constant.ApplicationConstants)2