Java Code Examples for io.reactivex.Observable.publish()

The following are Jave code examples for showing how to use publish() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: gigreminder   File: ConcertDetailsPresenter.java   Source Code and License Vote up 6 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadConcertEvent, Result> loadConcert = loadEvents ->
            loadEvents.flatMap(event -> {
                return getRepository().getConcert(concertId)
                        .map(LoadConcertResult::success)
                        .onErrorReturn(LoadConcertResult::error)
                        .toObservable()
                        .startWith(LoadConcertResult.IN_PROGRESS);
            });

    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadConcertEvent.class, loadConcert),
            sharedEvents.ofType(Result.class)
    ));
}
 
Example 2
Project: RxJava2-Android-Sample   File: ConnectableExampleActivity.java   Source Code and License Vote up 5 votes
private void testRefCount() {
    Observable<Long> observable = Observable.interval(1, TimeUnit.SECONDS).take(6);
    ConnectableObservable<Long> connectableObservable = observable.publish();
    connectableObservable.connect();
    Observable<Long> longObservable = connectableObservable.refCount();
    longObservable.delaySubscription(2, TimeUnit.SECONDS)
            .compose(Utils.<Long>ioToMain())
            .subscribe(getFirstObserver());
    longObservable.compose(Utils.<Long>ioToMain()).subscribe(getSecondObserver());
}
 
Example 3
Project: gigreminder   File: ArtistsPresenter.java   Source Code and License Vote up 5 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadArtistsEvent, Result> loadArtists = loadEvents ->
            loadEvents.flatMap(event ->
                    getRepository().getArtists()
                            .map(LoadArtistsResult::success)
                            .onErrorReturn(LoadArtistsResult::error)
                            .startWith(LoadArtistsResult.IN_PROGRESS)
            );
    ObservableTransformer<DeleteArtistsEvent, Result> deleteArtists = deleteEvents ->
            deleteEvents.flatMap(event -> {
                List<Artist> artists = event.getUiModel().getSelectedArtists();

                return getRepository().deleteArtists(artists)
                        .toSingleDefault(DeleteArtistsResult.SUCCESS)
                        .onErrorReturn(DeleteArtistsResult::error)
                        .toObservable()
                        .startWith(DeleteArtistsResult.IN_PROGRESS);
            });

    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadArtistsEvent.class, loadArtists),
            handleEventsOfClass(sharedEvents, DeleteArtistsEvent.class,
                    deleteArtists),
            sharedEvents.ofType(Result.class))
    );
}
 
Example 4
Project: gigreminder   File: LocationChoicePresenter.java   Source Code and License Vote up 5 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadLocationsEvent, Result> loadLocations = loadEvents ->
            loadEvents.flatMap(event ->
                    getRepository().getAvailableLocations()
                            .map(LoadLocationsResult::success)
                            .onErrorReturn(LoadLocationsResult::error)
                            .toObservable()
                            .startWith(LoadLocationsResult.IN_PROGRESS)
            );
    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadLocationsEvent.class, loadLocations),
            sharedEvents.ofType(Result.class)
    ));
}
 
Example 5
Project: gigreminder   File: EditArtistPresenter.java   Source Code and License Vote up 5 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadArtistEvent.class, this::loadArtist),
            handleEventsOfClass(sharedEvents, SaveArtistEvent.class, this::saveArtist),
            sharedEvents.ofType(Result.class)
    ));
}
 
Example 6
Project: gigreminder   File: ArtistImportPresenter.java   Source Code and License Vote up 5 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadArtistsEvent, Result> loadArtists = loadEvents ->
            loadEvents.flatMap(event ->
                    getRepository().loadArtistsFromGoogleMusic()
                            .map(LoadArtistsResult::success)
                            .onErrorReturn(LoadArtistsResult::error)
                            .toObservable()
                            .startWith(LoadArtistsResult.IN_PROGRESS)
            );

    ObservableTransformer<SaveArtistsEvent, Result> saveArtists = saveEvents ->
            saveEvents.flatMap(event -> {
                List<String> names = event.getUiModel().getSelectedArtists();

                if (names.isEmpty()) {
                    return Observable.just(SaveArtistsResult.error(
                            R.string.artist_import_error_no_artists_to_save));
                }

                List<Artist> artists = Observable.fromIterable(names)
                        .map(Artist::new)
                        .toList()
                        .blockingGet();

                return getRepository().saveArtists(artists)
                        .toSingleDefault(SaveArtistsResult.SUCCESS)
                        .onErrorReturnItem(SaveArtistsResult.error(R.string.error_saving_data))
                        .toObservable()
                        .startWith(SaveArtistsResult.IN_PROGRESS);
            });

    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadArtistsEvent.class, loadArtists),
            handleEventsOfClass(sharedEvents, SaveArtistsEvent.class, saveArtists),
            sharedEvents.ofType(Result.class))
    );
}
 
Example 7
Project: gigreminder   File: ConcertsPresenter.java   Source Code and License Vote up 5 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadConcertsEvent, Result> loadConcerts = loadEvents ->
            loadEvents.flatMap(event ->
                    getRepository().getConcerts()
                            .map(LoadConcertsResult::success)
                            .onErrorReturn(LoadConcertsResult::error)
                            .startWith(LoadConcertsResult.IN_PROGRESS)
            );

    Observable<Result> syncing = SyncEventBus.getEventBus()
            .flatMap(busEvent -> {
                if (busEvent instanceof SyncStartEvent) {
                    return Observable.just(SyncResult.create(true));
                } else if (busEvent instanceof SyncFinishEvent) {
                    return Observable.just(SyncResult.create(false));
                }

                return Observable.empty();
            });

    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadConcertsEvent.class, loadConcerts),
            syncing,
            sharedEvents.ofType(Result.class)
    ));
}
 
Example 8
Project: gigreminder   File: LocationsPresenter.java   Source Code and License Vote up 4 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    ObservableTransformer<LoadLocationsEvent, Result> loadLocations = loadEvents ->
            loadEvents.flatMap(event ->
                    getRepository().getLocations()
                            .map(LoadLocationsResult::success)
                            .onErrorReturn(LoadLocationsResult::error)
                            .startWith(LoadLocationsResult.IN_PROGRESS)
            );
    ObservableTransformer<DeleteLocationEvent, Result> deleteLocation = deleteEvents ->
            deleteEvents
                    .flatMap(event -> {
                        int position = event.getPosition();
                        LocationsUiModel model = event.getUiModel();
                        int maxPosition = model.getLocations().size() - 1;

                        if (position > maxPosition) {
                            return Observable.empty();
                        }

                        Location location = model.getLocations().get(position);

                        return Observable.just(location);
                    })
                    .flatMap(location ->
                            getRepository().deleteLocation(location)
                                    .toSingleDefault(DeleteLocationResult.SUCCESS)
                                    .onErrorReturn(DeleteLocationResult::error)
                                    .toObservable()
                                    .startWith(DeleteLocationResult.IN_PROGRESS)
                    );
    ObservableTransformer<SaveLocationEvent, Result> saveLocation = saveEvents ->
            saveEvents.flatMap(event ->
                    getRepository().saveLocation(event.getLocation())
                            .toSingleDefault(SaveLocationResult.SUCCESS)
                            .onErrorReturn(SaveLocationResult::error)
                            .toObservable()
                            .startWith(SaveLocationResult.IN_PROGRESS)
            );

    return events.publish(sharedEvents -> Observable.merge(
            handleEventsOfClass(sharedEvents, LoadLocationsEvent.class, loadLocations),
            handleEventsOfClass(sharedEvents, DeleteLocationEvent.class, deleteLocation),
            handleEventsOfClass(sharedEvents, SaveLocationEvent.class, saveLocation),
            sharedEvents.ofType(Result.class)
    ));
}