Android Open Source - android-javarx-example Subject Movie Provider






From Project

Back to project page android-javarx-example.

License

The source code is released under:

Apache License

If you think the Android project android-javarx-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bnsantos.movies.providers;
/*  w w w .jav  a  2s .c  om*/
import android.util.Log;

import com.bnsantos.movies.model.Movie;

import java.util.List;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;

/**
 * Created by bruno on 19/11/14.
 */
public class SubjectMovieProvider extends MovieProvider {
    private final BehaviorSubject<List<Movie>> mMoviesSubject;
    private final Subscription mServerSubscription;
    private final Subscription mCacheSubscription;

    public SubjectMovieProvider() {
        mMoviesSubject = BehaviorSubject.create();

        mServerSubscription = cacheAndRetrieveMovies().subscribeOn(Schedulers.newThread()).subscribe(new Action1<List<Movie>>() {
            @Override
            public void call(List<Movie> movieList) {
                Log.d(TAG, "ServerObservable received movies");
                mMoviesSubject.onNext(movieList);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.e(TAG, "Fetching movies from server error", throwable);
                mMoviesSubject.onError(throwable);
            }
        }, new Action0() {
            @Override
            public void call() {
                Log.d(TAG, "ServerObservable completed");
                mMoviesSubject.onCompleted();
            }
        });

        mCacheSubscription = retrieveCachedMovies().subscribeOn(Schedulers.newThread()).subscribe(new Action1<List<Movie>>() {
            @Override
            public void call(List<Movie> movieList) {
                Log.d(TAG, "CacheObservable received movies");
                mMoviesSubject.onNext(movieList);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.e(TAG, "Fetching movies from cache error", throwable);
                mMoviesSubject.onError(throwable);
            }
        }, new Action0() {
            @Override
            public void call() {
                Log.d(TAG, "CacheObservable completed");
                mMoviesSubject.onCompleted();
            }
        });

    }

    public void unSubscribe() {
        mCacheSubscription.unsubscribe();
        mServerSubscription.unsubscribe();
    }

    public Observable<List<Movie>> subscribe() {
        return mMoviesSubject;
    }
}




Java Source Code List

com.bnsantos.movies.App.java
com.bnsantos.movies.MovieServiceTest.java
com.bnsantos.movies.MoviesTestRunner.java
com.bnsantos.movies.TestSuite.java
com.bnsantos.movies.TestUtils.java
com.bnsantos.movies.Utils.java
com.bnsantos.movies.activities.MainActivity.java
com.bnsantos.movies.adapter.MovieAdapter.java
com.bnsantos.movies.cache.MovieCaching.java
com.bnsantos.movies.cache.PersistentManager.java
com.bnsantos.movies.model.Links.java
com.bnsantos.movies.model.MovieListType.java
com.bnsantos.movies.model.MovieResponse.java
com.bnsantos.movies.model.Movie.java
com.bnsantos.movies.model.Posters.java
com.bnsantos.movies.model.Ratings.java
com.bnsantos.movies.model.ReleaseDates.java
com.bnsantos.movies.providers.LazyMovieProvider.java
com.bnsantos.movies.providers.MovieProvider.java
com.bnsantos.movies.providers.SubjectMovieProvider.java
com.bnsantos.movies.services.MovieService.java
com.bnsantos.movies.services.RestErrorHandler.java