-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of google paging library
- Loading branch information
konik
authored and
konik
committed
Jun 28, 2019
1 parent
f6d317c
commit 7514191
Showing
10 changed files
with
282 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.kaput.popularmoviesapp.api; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import retrofit2.Retrofit; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class API { | ||
public static final String BASE_URL = "https://api.themoviedb.org/"; | ||
public static final int DEFAULT_PER_PAGE = 20; | ||
|
||
public static APIService createAPIService(){ | ||
Gson gson = new GsonBuilder().setLenient().create(); | ||
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build(); | ||
|
||
return retrofit.create(APIService.class); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/kaput/popularmoviesapp/data/MovieDataFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.kaput.popularmoviesapp.data; | ||
|
||
import android.arch.lifecycle.MutableLiveData; | ||
import android.arch.paging.DataSource; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
public class MovieDataFactory extends DataSource.Factory { | ||
|
||
|
||
MutableLiveData<MovieDataSource> mutableLiveData; | ||
MovieDataSource movieDataSource; | ||
Executor executor; | ||
|
||
public MovieDataFactory(Executor executor) { | ||
this.mutableLiveData = new MutableLiveData<MovieDataSource>(); | ||
this.executor = executor; | ||
|
||
} | ||
|
||
|
||
@Override | ||
public DataSource create() { | ||
movieDataSource = new MovieDataSource(executor); | ||
mutableLiveData.postValue(movieDataSource); | ||
return movieDataSource; | ||
} | ||
|
||
public MutableLiveData<MovieDataSource> getMutableLiveData() { | ||
return mutableLiveData; | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/kaput/popularmoviesapp/data/MovieDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.kaput.popularmoviesapp.data; | ||
|
||
import android.arch.paging.PageKeyedDataSource; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.kaput.popularmoviesapp.api.API; | ||
import com.kaput.popularmoviesapp.api.APIService; | ||
import com.kaput.popularmoviesapp.model.Movie; | ||
import com.kaput.popularmoviesapp.model.ResponseBody; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Executor; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
|
||
public class MovieDataSource extends PageKeyedDataSource<Integer, Movie> { | ||
|
||
APIService apiService; | ||
Integer pageNumber; | ||
private Executor executor; | ||
int ranking; | ||
|
||
|
||
public MovieDataSource(Executor executor){ | ||
apiService = API.createAPIService(); | ||
pageNumber = 1; | ||
this.executor = executor; | ||
ranking = 1; | ||
} | ||
|
||
@Override | ||
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, Movie> callback) { | ||
final List<Movie> movieList = new ArrayList(); | ||
|
||
apiService = API.createAPIService(); | ||
apiService.getDatum("f44ae99022116e67fae910b2c8a2a3c2", pageNumber).enqueue(new Callback<ResponseBody>() { | ||
@Override | ||
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { | ||
|
||
for (Movie m:response.body().getMovies()) { | ||
m.setRanking(ranking++); | ||
movieList.add(m); | ||
} | ||
|
||
// movieList.addAll(response.body().getMovies()); | ||
pageNumber++; | ||
callback.onResult(movieList, null, pageNumber); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<ResponseBody> call, Throwable t) { | ||
} | ||
|
||
|
||
}); | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Movie> callback) { | ||
|
||
} | ||
|
||
@Override | ||
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Movie> callback) { | ||
final List<Movie> movieList = new ArrayList(); | ||
|
||
apiService.getDatum("f44ae99022116e67fae910b2c8a2a3c2", params.key).enqueue(new Callback<ResponseBody>() { | ||
@Override | ||
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { | ||
for (Movie m:response.body().getMovies()) { | ||
m.setRanking(ranking++); | ||
movieList.add(m); | ||
} | ||
callback.onResult(movieList, params.key + 1); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<ResponseBody> call, Throwable t) { | ||
} | ||
|
||
|
||
}); | ||
|
||
} | ||
} |
Oops, something went wrong.