Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Cutta committed Sep 21, 2017
1 parent feaf4f2 commit 04e0bcf
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
ext {
supportVersion = "26.1.0"
daggerVersion = "2.11"
archVersion = "1.0.0-alpha5"
archVersion = "1.0.0-alpha9-1"
}

dependencies {
Expand All @@ -37,12 +37,16 @@ dependencies {
compile "com.android.support:appcompat-v7:$supportVersion"

//arch
compile "android.arch.persistence.room:runtime:$archVersion"
compile "android.arch.lifecycle:runtime:$archVersion"
compile "android.arch.lifecycle:extensions:$archVersion"



implementation "android.arch.persistence.room:runtime:$archVersion"
implementation "android.arch.lifecycle:runtime:$archVersion"
implementation "android.arch.lifecycle:extensions:$archVersion"
annotationProcessor "android.arch.persistence.room:compiler:$archVersion"
annotationProcessor "android.arch.lifecycle:compiler:$archVersion"


//dagger
compile "com.google.dagger:dagger:$daggerVersion"
compile "com.google.dagger:dagger-android:$daggerVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface TMDBService {
@GET("authentication/session/new")
Observable<ResponseSessionId> getSessionId(@Query("api_key") String apiKey,
@Query("request_token") String requestToken);

/*
//TV
@GET("tv/on_the_air")
Observable<ResponseResultList<ResultTv>> getTvOnTheAir(@Query("api_key") String apiKey,
Expand All @@ -49,34 +49,34 @@ Observable<ResponseResultList<ResultTv>> getTopRatedTv(@Query("api_key") String
Observable<ResponseResultList<ResultTv>> getAiringToday(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

*/
//FILM

@GET("movie/popular")
Observable<ResponseResultList<Movie>> getPopularMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);
Observable<ResponseResultList> getPopularMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);


@GET("movie/popular")
LiveData<ApiResponse<List<Movie>>> getPopularM(@Query("api_key") String apiKey,
LiveData<ApiResponse<ResponseResultList>> getPopularM(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

@GET("movie/top_rated")
Observable<ResponseResultList<Movie>> getTopRatedMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);
Observable<ResponseResultList> getTopRatedMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

@GET("movie/upcoming")
Observable<ResponseResultList<Movie>> getUpcomingMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);
Observable<ResponseResultList> getUpcomingMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

@GET("movie/now_playing")
Observable<ResponseResultList<Movie>> getNowPlayingMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);
Observable<ResponseResultList> getNowPlayingMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

@GET("movie/{movie_id}")
Observable<Movie> getMovieDetail(@Path("movie_id") int movieId,
Expand All @@ -90,19 +90,19 @@ Observable<ResponseCredits> getMovieCredit(@Path("movie_id") int movieId,
@Query("language") String language);

@GET("movie/{movie_id}/similar")
Observable<ResponseResultList<Movie>> getSimilarMovies(@Path("movie_id") int movieId,
@Query("api_key") String apiKey,
@Query("language") String language);
Observable<ResponseResultList> getSimilarMovies(@Path("movie_id") int movieId,
@Query("api_key") String apiKey,
@Query("language") String language);

//DISCOVER
@GET("discover/movie")
Observable<ResponseResultList<Movie>> getDiscoverMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

@GET("discover/tv")
Observable<ResponseResultList<ResultTv>> getDiscoverTv(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);
Observable<ResponseResultList> getDiscoverMovie(@Query("api_key") String apiKey,
@Query("language") String language,
@Query("page") int page);

// @GET("discover/tv")
// Observable<ResponseResultList<ResultTv>> getDiscoverTv(@Query("api_key") String apiKey,
// @Query("language") String language,
// @Query("page") int page);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public abstract class MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void insertMovies(List<Movie> movies);

@Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract void insert(PaginationResult result);

@Query("SELECT * FROM PaginationResult WHERE type = :type")
public abstract LiveData<PaginationResult> search(String type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.aac.andcun.themoviedb_mvvm.repository.MovieRepository;
import com.aac.andcun.themoviedb_mvvm.repository.TvRepository;
import com.aac.andcun.themoviedb_mvvm.util.AppExecutors;
import com.aac.andcun.themoviedb_mvvm.util.LiveDataCallAdapterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Expand Down Expand Up @@ -53,7 +54,7 @@ Retrofit provideRetrofit(OkHttpClient okHttpClient, Gson gson) {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addCallAdapterFactory(new LiveDataCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.aac.andcun.themoviedb_mvvm.repository;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand All @@ -9,8 +12,10 @@
import com.aac.andcun.themoviedb_mvvm.api.TMDBService;
import com.aac.andcun.themoviedb_mvvm.db.MovieDao;
import com.aac.andcun.themoviedb_mvvm.db.TMDBDb;
import com.aac.andcun.themoviedb_mvvm.util.AbsentLiveData;
import com.aac.andcun.themoviedb_mvvm.util.AppExecutors;
import com.aac.andcun.themoviedb_mvvm.vo.Movie;
import com.aac.andcun.themoviedb_mvvm.vo.PaginationResult;
import com.aac.andcun.themoviedb_mvvm.vo.Resource;
import com.aac.andcun.themoviedb_mvvm.vo.ResponseCredits;
import com.aac.andcun.themoviedb_mvvm.vo.ResponseResultList;
Expand All @@ -22,7 +27,7 @@
import javax.inject.Singleton;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


/**
* Created by cuneytcarikci on 24/07/2017.
Expand All @@ -35,6 +40,7 @@ public class MovieRepository {
private TMDBService service;
private MovieDao movieDao;
private AppExecutors appExecutors;
public static final String type = "popular_movies";

@Inject
public MovieRepository(TMDBService service, TMDBDb db, MovieDao movieDao, AppExecutors appExecutors) {
Expand All @@ -44,50 +50,65 @@ public MovieRepository(TMDBService service, TMDBDb db, MovieDao movieDao, AppExe
this.appExecutors = appExecutors;
}

public LiveData<Resource<List<Movie>>> getPopular(final int page) {
return new NetworkBoundResource<List<Movie>, List<Movie>>(appExecutors) {
public LiveData<Resource<List<Movie>>> getPopularMovies() {

return new NetworkBoundResource<List<Movie>, ResponseResultList>(appExecutors) {


@Override
protected void saveCallResult(@NonNull List<Movie> item) {
movieDao.insertMovies(item);
protected void saveCallResult(@NonNull ResponseResultList item) {
List<Integer> movieIds = item.getMovieIds();
PaginationResult paginationResult = new PaginationResult(type, movieIds, item.getTotalResults(), item.getNextPage());
db.beginTransaction();

try {

movieDao.insertMovies(item.getResults());
movieDao.insert(paginationResult);
db.setTransactionSuccessful();

} finally {
db.endTransaction();
}

}

@Override
protected boolean shouldFetch(@Nullable List<Movie> data) {
return true;//github örneğinde bunun için bi mekanizma var 10 dkyı geçtiyse fetch ettiriyor
return data == null || data.isEmpty();
}

@NonNull
@Override
protected LiveData<List<Movie>> loadFromDb() {
return null;//todo
return Transformations.switchMap(movieDao.search(type), new Function<PaginationResult, LiveData<List<Movie>>>() {
@Override
public LiveData<List<Movie>> apply(PaginationResult input) {
if (input == null)
return AbsentLiveData.create();
else
return movieDao.loadById(input.ids);

}
});
}

@NonNull
@Override
protected LiveData<ApiResponse<List<Movie>>> createCall() {
return service.getPopularM(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page);
protected LiveData<ApiResponse<ResponseResultList>> createCall() {
return service.getPopularM(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), 1);
}

}.asLiveData();
}

public Observable<List<Movie>> getPopularMovies(int page) {

return service.getPopularMovie(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page)
.map(new Function<ResponseResultList<Movie>, List<Movie>>() {
@Override
public List<Movie> apply(ResponseResultList<Movie> result) throws Exception {
return result.getResults();
}
});

}

public Observable<List<Movie>> getNowPlayingMovies(int page) {

return service.getNowPlayingMovie(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page)
.map(new Function<ResponseResultList<Movie>, List<Movie>>() {
.map(new io.reactivex.functions.Function<ResponseResultList, List<Movie>>() {
@Override
public List<Movie> apply(ResponseResultList<Movie> result) throws Exception {
public List<Movie> apply(ResponseResultList result) throws Exception {
return result.getResults();
}
});
Expand All @@ -97,9 +118,9 @@ public List<Movie> apply(ResponseResultList<Movie> result) throws Exception {
public Observable<List<Movie>> getUpcomingMovies(int page) {

return service.getUpcomingMovie(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page)
.map(new Function<ResponseResultList<Movie>, List<Movie>>() {
.map(new io.reactivex.functions.Function<ResponseResultList, List<Movie>>() {
@Override
public List<Movie> apply(ResponseResultList<Movie> result) throws Exception {
public List<Movie> apply(ResponseResultList result) throws Exception {
return result.getResults();
}
});
Expand All @@ -116,7 +137,7 @@ public Observable<ResponseCredits> getCredits(int movieId) {
return service.getMovieCredit(movieId, ApiConstants.API_KEY, Locale.getDefault().getLanguage());
}

public Observable<ResponseResultList<Movie>> getSimilars(int movieId) {
public Observable<ResponseResultList> getSimilars(int movieId) {
return service.getSimilarMovies(movieId, ApiConstants.API_KEY, Locale.getDefault().getLanguage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,31 @@ public TvRepository(TMDBService service) {

public Observable<List<ResultTv>> getPopularTvs(int page) {

return null;
/*
return service.getPopularTv(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page)
.map(new io.reactivex.functions.Function<ResponseResultList<ResultTv>, List<ResultTv>>() {
@Override
public List<ResultTv> apply(ResponseResultList<ResultTv> result) throws Exception {
return result.getResults();
}
});
*/

}

public Observable<List<ResultTv>> getOnTheAirTvs(int page) {

return null;/*
return service.getTvOnTheAir(ApiConstants.API_KEY, Locale.getDefault().getLanguage(), page)
.map(new io.reactivex.functions.Function<ResponseResultList<ResultTv>, List<ResultTv>>() {
@Override
public List<ResultTv> apply(ResponseResultList<ResultTv> result) throws Exception {
return result.getResults();
}
});
*/

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.aac.andcun.themoviedb_mvvm.ui.movie;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
Expand All @@ -15,6 +17,7 @@
import com.aac.andcun.themoviedb_mvvm.ui.detail.MovieDetailActivity;
import com.aac.andcun.themoviedb_mvvm.util.RxTransformer;
import com.aac.andcun.themoviedb_mvvm.vo.Movie;
import com.aac.andcun.themoviedb_mvvm.vo.Resource;

import java.util.List;

Expand Down Expand Up @@ -57,24 +60,37 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

Observable<List<Movie>> observable = null;
LiveData<Resource<List<Movie>>> resourceLiveData = null;

switch (position) {
case 0:
observable = repository.getPopularMovies(1);
resourceLiveData = repository.getPopularMovies();
break;

case 1:
observable = repository.getNowPlayingMovies(1);
//observable = repository.getNowPlayingMovies(1);
resourceLiveData = repository.getPopularMovies();
break;

case 2:
observable = repository.getUpcomingMovies(1);
resourceLiveData = repository.getPopularMovies();
//observable = repository.getUpcomingMovies(1);
break;
}

resourceLiveData.observeForever(new Observer<Resource<List<Movie>>>() {
@Override
public void onChanged(@Nullable Resource<List<Movie>> listResource) {
if (listResource.data != null){
adapter.addMovieList(listResource.data);
binding.executePendingBindings();
}

}
});


observable
/* observable
.compose(RxTransformer.<List<Movie>>applyIOSchedulers())
.subscribe(new Consumer<List<Movie>>() {
@Override
Expand All @@ -90,6 +106,7 @@ public void accept(Throwable throwable) throws Exception {
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
});
*/

}

Expand Down
Loading

0 comments on commit 04e0bcf

Please sign in to comment.