Android Open Source - android-javarx-example 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;
/*ww  w.  j  ava2s  . c  o m*/
import android.util.Log;

import com.bnsantos.movies.App;
import com.bnsantos.movies.model.Movie;
import com.bnsantos.movies.model.MovieListType;
import com.bnsantos.movies.model.MovieResponse;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by bruno on 17/11/14.
 */
public abstract class MovieProvider {
    protected final String TAG = MovieProvider.class.getName();

    private Observable<List<Movie>> retrieveServerMovies() {
        return App.getInstance().getMovieService().retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), App.getInstance().getApiToken(), 10, 1, "us")
                .map(new Func1<MovieResponse, List<Movie>>() {
                    @Override
                    public List<Movie> call(MovieResponse movieResponse) {
                        Log.d(TAG, "Mapping server response");
                        return movieResponse.getMovies();
                    }
                });
    }

    protected Observable<List<Movie>> cacheAndRetrieveMovies() {
        return retrieveServerMovies().flatMap(new Func1<List<Movie>, Observable<List<Movie>>>() {
            @Override
            public Observable<List<Movie>> call(List<Movie> movieList) {
                Log.d(TAG, "Caching movies fetched from server");
                App.getInstance().getMovieCaching().cache(movieList);
                return Observable.create(new Observable.OnSubscribe<List<Movie>>() {
                    @Override
                    public void call(Subscriber<? super List<Movie>> subscriber) {
                        Log.d(TAG, "Emitting movies cached");
                        subscriber.onNext(App.getInstance().getMovieCaching().fetch());
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    protected Observable<List<Movie>> retrieveCachedMovies() {
        return Observable.create(new Observable.OnSubscribe<List<Movie>>() {
            @Override
            public void call(Subscriber<? super List<Movie>> subscriber) {
                Log.d(TAG, "Retrieving movies from cache");
                subscriber.onNext(App.getInstance().getMovieCaching().fetch());
                subscriber.onCompleted();
            }
        });
    }

    public abstract Observable<List<Movie>> subscribe();
}




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