Java Code Examples for rx.subjects.BehaviorSubject#asObservable()

The following examples show how to use rx.subjects.BehaviorSubject#asObservable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalPlaylistStore.java    From Jockey with Apache License 2.0 6 votes vote down vote up
private Observable<List<Song>> getAutoPlaylistSongs(AutoPlaylist playlist) {
    BehaviorSubject<List<Song>> subject;

    if (mPlaylistContents.containsKey(playlist)) {
        subject = mPlaylistContents.get(playlist);
    } else {
        subject = BehaviorSubject.create();
        mPlaylistContents.put(playlist, subject);

        playlist.generatePlaylist(mMusicStore, this, mPlayCountStore)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subject::onNext, subject::onError);

        subject.observeOn(Schedulers.io())
                .subscribe(contents -> {
                    MediaStoreUtil.editPlaylist(mContext, playlist, contents);
                }, throwable -> {
                    Timber.e(throwable, "Failed to save playlist contents");
                });
    }

    return subject.asObservable();
}
 
Example 2
Source File: ObservableGithubRepos.java    From RxRestSample with Apache License 2.0 5 votes vote down vote up
public Observable<String> updateRepo(String userName) {
    BehaviorSubject<String> requestSubject = BehaviorSubject.create();

    Observable<List<Repo>> observable = mClient.getRepos(userName);
    observable.subscribeOn(Schedulers.io())
              .observeOn(Schedulers.io())
              .subscribe(l -> {
                                mDatabase.insertRepoList(l);
                                requestSubject.onNext(userName);},
                         e -> requestSubject.onError(e),
                         () -> requestSubject.onCompleted());
    return requestSubject.asObservable();
}
 
Example 3
Source File: LocalMusicStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<Boolean> refresh() {
    mSongLoadingState.onNext(true);
    mArtistLoadingState.onNext(true);
    mAlbumLoadingState.onNext(true);
    mGenreLoadingState.onNext(true);

    BehaviorSubject<Boolean> result = BehaviorSubject.create();

    MediaStoreUtil.promptPermission(mContext)
            .observeOn(Schedulers.io())
            .map(granted -> {
                if (granted) {
                    if (mSongs != null) {
                        mSongs.onNext(getAllSongs());
                    }
                    if (mArtists != null) {
                        mArtists.onNext(getAllArtists());
                    }
                    if (mAlbums != null) {
                        mAlbums.onNext(getAllAlbums());
                    }
                    if (mGenres != null) {
                        mGenres.onNext(getAllGenres());
                    }
                }
                mSongLoadingState.onNext(false);
                mArtistLoadingState.onNext(false);
                mAlbumLoadingState.onNext(false);
                mGenreLoadingState.onNext(false);
                return granted;
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(result);

    return result.asObservable();
}
 
Example 4
Source File: DemoMusicStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<Song>> getSongs() {
    if (mSongs == null) {
        BehaviorSubject<List<Song>> subject = BehaviorSubject.create();

        Observable.fromCallable(() -> parseJson(SONGS_FILENAME, Song[].class))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subject::onNext, subject::onError);

        mSongs = subject.asObservable();
    }

    return mSongs;
}
 
Example 5
Source File: DemoMusicStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<Album>> getAlbums() {
    if (mAlbums == null) {
        BehaviorSubject<List<Album>> subject = BehaviorSubject.create();

        Observable.fromCallable(() -> parseJson(ALBUMS_FILENAME, Album[].class))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subject::onNext, subject::onError);

        mAlbums = subject.asObservable();
    }

    return mAlbums;
}
 
Example 6
Source File: DemoMusicStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<Artist>> getArtists() {
    if (mArtists == null) {
        BehaviorSubject<List<Artist>> subject = BehaviorSubject.create();

        Observable.fromCallable(() -> parseJson(ARTISTS_FILENAME, Artist[].class))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subject::onNext, subject::onError);

        mArtists = subject.asObservable();
    }

    return mArtists;
}
 
Example 7
Source File: DemoMusicStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<Genre>> getGenres() {
    if (mGenres == null) {
        BehaviorSubject<List<Genre>> subject = BehaviorSubject.create();

        Observable.fromCallable(() -> parseJson(GENRES_FILENAME, Genre[].class))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subject::onNext, subject::onError);

        mGenres = subject.asObservable();
    }

    return mGenres;
}
 
Example 8
Source File: LocalPlaylistStore.java    From Jockey with Apache License 2.0 5 votes vote down vote up
private Observable<List<Song>> getPlaylistSongs(Playlist playlist) {
    BehaviorSubject<List<Song>> subject;

    if (mPlaylistContents.containsKey(playlist)) {
        subject = mPlaylistContents.get(playlist);
    } else {
        subject = BehaviorSubject.create();
        mPlaylistContents.put(playlist, subject);

        Observable.fromCallable(() -> MediaStoreUtil.getPlaylistSongs(mContext, playlist))
                .subscribe(subject::onNext, subject::onError);
    }

    return subject.asObservable();
}