Skip to content

Commit

Permalink
implementation of google paging library
Browse files Browse the repository at this point in the history
  • Loading branch information
konik authored and konik committed Jun 28, 2019
1 parent f6d317c commit 7514191
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 59 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'android.arch.paging:runtime:1.0.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.kaput.popularmoviesapp.MainActivity">
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
47 changes: 18 additions & 29 deletions app/src/main/java/com/kaput/popularmoviesapp/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.kaput.popularmoviesapp;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.arch.paging.PagedList;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kaput.popularmoviesapp.model.MovieListItem;
import com.kaput.popularmoviesapp.R;
import com.kaput.popularmoviesapp.api.APIService;
import com.kaput.popularmoviesapp.api.APIUrl;
import com.kaput.popularmoviesapp.api.API;
import com.kaput.popularmoviesapp.model.Movie;
import com.kaput.popularmoviesapp.model.ResponseBody;
import com.kaput.popularmoviesapp.viewmodel.MovieViewModel;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,10 +29,11 @@
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity {

private List<Movie> movieList;
private RecyclerView movieRecyclerView;
private MovieViewModel viewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -39,41 +46,23 @@ protected void onCreate(Bundle savedInstanceState) {
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
movieRecyclerView.setLayoutManager(linearLayoutManager);
movieRecyclerView.setHasFixedSize(true);
movieRecyclerView.setItemViewCacheSize(20);
movieRecyclerView.setItemViewCacheSize(40);

Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(APIUrl.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
viewModel = ViewModelProviders.of(this).get(MovieViewModel.class);

APIService apis = retrofit.create(APIService.class);
Call<ResponseBody> call = apis.getDatum("f44ae99022116e67fae910b2c8a2a3c2", 1);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
movieList = response.body().getMovies();
listToListView();

}
final MovieAdapter movieAdapter = new MovieAdapter();

viewModel.movieList.observe(this, new Observer<PagedList<Movie>>() {
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {

public void onChanged(@Nullable PagedList<Movie> pagedList) {
movieAdapter.submitList(pagedList);
}
});

}
movieRecyclerView.setAdapter(movieAdapter);

private void listToListView() {

ArrayList<MovieListItem> itemList = new ArrayList<>();
for (int i = 0 ; i<movieList.size(); i ++) {
itemList.add(new MovieListItem(i + 1,
movieList.get(i).rating,
movieList.get(i).title,
movieList.get(i).posterPath));
}

MovieAdapter movieAdapter = new MovieAdapter(this, itemList);
movieAdapter.setHasStableIds(true);
movieRecyclerView.setAdapter(movieAdapter);
}

}
58 changes: 36 additions & 22 deletions app/src/main/java/com/kaput/popularmoviesapp/MovieAdapter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.kaput.popularmoviesapp;

import android.arch.paging.PagedListAdapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -21,39 +24,51 @@
import java.util.List;


public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder> {
public class MovieAdapter extends PagedListAdapter<Movie, MovieAdapter.MyViewHolder> {

ArrayList<MovieListItem> movieList;
//ArrayList<Movie> movieList;
LayoutInflater inflater;
Context context;

public MovieAdapter(Context context, ArrayList<MovieListItem> movieList) {
this.movieList = movieList;
this.inflater = LayoutInflater.from(context);
public MovieAdapter() {
super(Movie.DIFF_CALLBACK);
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view = inflater.inflate(R.layout.custom_item_card, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
/* View vieww = inflater.inflate(R.layout.custom_item_card, parent, false);
MyViewHolder holder = new MyViewHolder(vieww);
return holder;*/





LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view;




view = layoutInflater.inflate(R.layout.custom_item_card, parent, false);
return new MyViewHolder(view);



}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
MovieListItem selectedMovie = movieList.get(i);
myViewHolder.setData(selectedMovie, i);
}

@Override
public int getItemCount() {
return movieList.size();
myViewHolder.setData(getItem(i));
}

@Override
/* @Override
public long getItemId(int position) {
return movieList.get(position).getRanking();
return movieList.get(position).ranking;
}
*/

public class MyViewHolder extends RecyclerView.ViewHolder {

Expand All @@ -69,19 +84,18 @@ public MyViewHolder(View itemView) {

}

public void setData(MovieListItem m, int position) {
this.title.setText(m.getTitle());
this.rating.setText(m.getRating());
this.ranking.setText(String.valueOf(m.getRanking() + "."));
this.poster.setImageDrawable(LoadImageFromWebOperations(m.getPosterPath()));
public void setData(Movie m) {
this.title.setText(m.title);
this.rating.setText(m.rating);
this.ranking.setText(String.valueOf(m.ranking + "."));
this.poster.setImageDrawable(LoadImageFromWebOperations(m.posterPath));
}

public Drawable LoadImageFromWebOperations(String path) {

try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
System.out.println("https://image.tmdb.org/t/p/w92" + path);
InputStream is = (InputStream) new URL("https://image.tmdb.org/t/p/w185" + path).getContent();
Drawable d = Drawable.createFromStream(is, "themoviedb");
return d;
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/kaput/popularmoviesapp/api/API.java
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);
}
}
5 changes: 0 additions & 5 deletions app/src/main/java/com/kaput/popularmoviesapp/api/APIUrl.java

This file was deleted.

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;
}

}
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) {
}


});

}
}
Loading

0 comments on commit 7514191

Please sign in to comment.