Added Loader<Cursor> to DiscoverFragment
This commit is contained in:
@@ -140,7 +140,7 @@ public class TestProvider extends AndroidTestCase {
|
||||
|
||||
Cursor cursor = mContext.getContentResolver().query(
|
||||
MovieEntry.CONTENT_URI,
|
||||
null, // projection
|
||||
null, // movieProjection
|
||||
MovieEntry.COLUMN_TMDB_ID + " = " + movieTMDBId,
|
||||
null, // Values for the "where" clause
|
||||
null // sort order
|
||||
|
||||
@@ -2,8 +2,12 @@ package com.example.android.popularmovies;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.LoaderManager;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.DisplayMetrics;
|
||||
@@ -13,6 +17,9 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.example.android.popularmovies.data.MovieContract;
|
||||
import com.example.android.popularmovies.data.MovieUtil;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
@@ -20,18 +27,17 @@ import butterknife.ButterKnife;
|
||||
/**
|
||||
* Created by Aaron Helton on 1/30/2016
|
||||
*/
|
||||
public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItemClickListener
|
||||
public class DiscoverFragment extends Fragment
|
||||
implements MovieAdapter.MovieItemClickListener, LoaderManager.LoaderCallbacks<Cursor>
|
||||
{
|
||||
//Neglecting use of ButterKnife for this fragment, only one findViewById() is used.
|
||||
private static final String LOG_TAG = DiscoverFragment.class.getSimpleName();
|
||||
private static final Integer PREFERRED_COLUMN_WIDTH = 100;
|
||||
private static final Integer MOVIE_LOADER = 0;
|
||||
|
||||
private OnMovieSelectedListener mListener;
|
||||
private RecyclerView mMoviesGridView;
|
||||
private MovieAdapter mAdapter;
|
||||
|
||||
private String sort;
|
||||
private boolean loadingMovies;
|
||||
private int currentPage;
|
||||
|
||||
@Bind(R.id.movieContainer) FrameLayout movieContainer;
|
||||
@@ -49,6 +55,13 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
sort = Utility.getSort(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle state)
|
||||
{
|
||||
super.onActivityCreated(state);
|
||||
getLoaderManager().initLoader(MOVIE_LOADER, null, this);
|
||||
}
|
||||
|
||||
public void onSaveInstanceState(Bundle bundle)
|
||||
{
|
||||
super.onSaveInstanceState(bundle);
|
||||
@@ -79,22 +92,6 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
}
|
||||
}
|
||||
};
|
||||
mMoviesGridView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
||||
int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
|
||||
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
|
||||
int pastVisiblesItems =
|
||||
((GridLayoutManager) recyclerView.getLayoutManager())
|
||||
.findFirstVisibleItemPosition();
|
||||
|
||||
if (!loadingMovies) {
|
||||
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
|
||||
addMovies();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
mMoviesGridView.setLayoutParams(new RecyclerView.LayoutParams(
|
||||
RecyclerView.LayoutParams.MATCH_PARENT,
|
||||
RecyclerView.LayoutParams.MATCH_PARENT));
|
||||
@@ -105,7 +102,7 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
mMoviesGridView.setLayoutManager(mManager);
|
||||
mMoviesGridView.setAdapter(mAdapter);
|
||||
movieContainer.addView(mMoviesGridView);
|
||||
addMovies();
|
||||
updateMovies();
|
||||
if(savedInstanceState != null)
|
||||
mMoviesGridView.setVerticalScrollbarPosition(savedInstanceState.getInt("SCROLL_POS"));
|
||||
return root;
|
||||
@@ -116,9 +113,9 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
{
|
||||
super.onResume();
|
||||
if(!sort.equals(Utility.getSort(getActivity()))) {
|
||||
mAdapter.emptyDataset();
|
||||
getLoaderManager().restartLoader(MOVIE_LOADER, null, this);
|
||||
currentPage = 1;
|
||||
addMovies();
|
||||
updateMovies();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,28 +125,9 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
mListener.onMovieSelected(id);
|
||||
}
|
||||
|
||||
public void addMovies() {
|
||||
if(currentPage < 11)
|
||||
{
|
||||
loadingMovies = true;
|
||||
DiscoverTask task = new DiscoverTask(this.getActivity()) {
|
||||
@Override
|
||||
public void onProgressUpdate(String... update)
|
||||
{
|
||||
if(update.length == 2)
|
||||
{
|
||||
mAdapter.addMovie(Long.parseLong(update[0]), update[1]);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onPostExecute(Void result) {
|
||||
loadingMovies = false;
|
||||
}
|
||||
};
|
||||
Log.d(LOG_TAG, "CURRENT PAGE: " + currentPage);
|
||||
task.execute(currentPage++);
|
||||
}
|
||||
|
||||
public void updateMovies() {
|
||||
DiscoverTask task = new DiscoverTask(this.getActivity());
|
||||
task.execute(currentPage++);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -169,6 +147,29 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
mListener = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
String sort = Utility.getSort(getActivity());
|
||||
if(sort.equalsIgnoreCase(getString(R.string.pref_sort_popularity_value)))
|
||||
sort = MovieContract.MovieEntry.COLUMN_POPULARITY + " DESC";
|
||||
else if (sort.equalsIgnoreCase(getString(R.string.pref_sort_rating_value)))
|
||||
sort = MovieContract.MovieEntry.COLUMN_RATING + " DESC";
|
||||
|
||||
return new CursorLoader(getActivity(), MovieContract.MovieEntry.CONTENT_URI,
|
||||
MovieUtil.movieProjection, null, null, sort);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
mAdapter.setDataset(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
mAdapter.setDataset(null);
|
||||
}
|
||||
|
||||
public interface OnMovieSelectedListener {
|
||||
void onMovieSelected(Long id);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.example.android.popularmovies;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.View;
|
||||
@@ -9,41 +9,31 @@ import android.view.ViewGroup.LayoutParams;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.example.android.popularmovies.data.FileUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import com.example.android.popularmovies.data.MovieContract;
|
||||
|
||||
/**
|
||||
* Created by Aaron Helton on 1/30/2016
|
||||
*/
|
||||
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
|
||||
{
|
||||
@SuppressWarnings("unused")
|
||||
private static final String LOG_TAG = MovieAdapter.class.getSimpleName();
|
||||
//Struct-like class for inner use
|
||||
private static class MovieReference {
|
||||
public final Long id;
|
||||
public final Bitmap poster;
|
||||
|
||||
public MovieReference(Long id, Bitmap poster) {
|
||||
this.id = id;
|
||||
this.poster = poster;
|
||||
}
|
||||
}
|
||||
|
||||
public interface MovieItemClickListener
|
||||
{
|
||||
void movieClicked(Long movieID);
|
||||
}
|
||||
|
||||
private ArrayList<MovieReference> dataset;
|
||||
private Cursor dataset;
|
||||
private MovieItemClickListener listener;
|
||||
private Context context;
|
||||
|
||||
public MovieAdapter() {
|
||||
dataset = new ArrayList<>();
|
||||
dataset = null;
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
|
||||
{
|
||||
@SuppressWarnings("unused")
|
||||
private final String LOG_TAG = ViewHolder.class.getName();
|
||||
|
||||
public ImageView imageView;
|
||||
@@ -57,8 +47,12 @@ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(listener != null) {
|
||||
MovieReference reference = dataset.get(this.getLayoutPosition());
|
||||
listener.movieClicked(reference.id);
|
||||
if(dataset != null) {
|
||||
dataset.moveToPosition(this.getLayoutPosition());
|
||||
long id = dataset.getLong(
|
||||
dataset.getColumnIndex(MovieContract.MovieEntry.COLUMN_TMDB_ID));
|
||||
listener.movieClicked(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,28 +75,29 @@ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position)
|
||||
{
|
||||
MovieReference movie = dataset.get(position);
|
||||
if(movie != null) {
|
||||
holder.imageView.setImageBitmap(movie.poster);
|
||||
if(dataset != null) {
|
||||
dataset.moveToPosition(position);
|
||||
Bitmap poster = FileUtils.getImage(
|
||||
dataset.getString(
|
||||
dataset.getColumnIndex(MovieContract.MovieEntry.COLUMN_IMG_PATH)));
|
||||
if (poster != null) {
|
||||
holder.imageView.setImageBitmap(poster);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addMovie(long movieId, String imagePath)
|
||||
{
|
||||
MovieReference reference =
|
||||
new MovieReference(movieId, FileUtils.getImage(imagePath));
|
||||
dataset.add(reference);
|
||||
notifyItemInserted(dataset.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return dataset.size();
|
||||
if(dataset != null) {
|
||||
return dataset.getCount();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public final void emptyDataset() {
|
||||
int prevSize = dataset.size();
|
||||
dataset.clear();
|
||||
notifyItemRangeRemoved(0, prevSize);
|
||||
public void setDataset(Cursor cursor)
|
||||
{
|
||||
this.dataset = cursor;
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,8 @@ public final class MovieUtil
|
||||
public static final String TMDB_URL_BASE = "http://api.themoviedb.org/3";
|
||||
public static final String TMDB_POSTER_URL = "http://image.tmdb.org/t/p/w185";
|
||||
|
||||
private static final String[] projection = {
|
||||
public static final String[] movieProjection = {
|
||||
MovieContract.MovieEntry.COLUMN_TMDB_ID,
|
||||
MovieContract.MovieEntry.COLUMN_TITLE,
|
||||
MovieContract.MovieEntry.COLUMN_DESC,
|
||||
MovieContract.MovieEntry.COLUMN_IMG_PATH,
|
||||
@@ -44,22 +45,23 @@ public final class MovieUtil
|
||||
MovieContract.MovieEntry.COLUMN_FAVORITE
|
||||
};
|
||||
|
||||
@SuppressWarnings("all") private static final int TITLE = 0;
|
||||
@SuppressWarnings("all") private static final int OVERVIEW = 1;
|
||||
@SuppressWarnings("all") private static final int IMG_PATH = 2;
|
||||
@SuppressWarnings("all") private static final int RELEASE = 3;
|
||||
@SuppressWarnings("all") private static final int RUNTIME = 4;
|
||||
@SuppressWarnings("all") private static final int RATING = 5;
|
||||
@SuppressWarnings("all") private static final int POPULARITY = 6;
|
||||
@SuppressWarnings("all") private static final int VOTE_COUNT = 7;
|
||||
@SuppressWarnings("all") private static final int GENRES = 8;
|
||||
@SuppressWarnings("all") private static final int FAVORITE = 9;
|
||||
@SuppressWarnings("all") public static final int TMDB_ID = 0;
|
||||
@SuppressWarnings("all") public static final int TITLE = 1;
|
||||
@SuppressWarnings("all") public static final int OVERVIEW = 2;
|
||||
@SuppressWarnings("all") public static final int IMG_PATH = 3;
|
||||
@SuppressWarnings("all") public static final int RELEASE = 4;
|
||||
@SuppressWarnings("all") public static final int RUNTIME = 5;
|
||||
@SuppressWarnings("all") public static final int RATING = 6;
|
||||
@SuppressWarnings("all") public static final int POPULARITY = 7;
|
||||
@SuppressWarnings("all") public static final int VOTE_COUNT = 8;
|
||||
@SuppressWarnings("all") public static final int GENRES = 9;
|
||||
@SuppressWarnings("all") public static final int FAVORITE = 10;
|
||||
|
||||
public static MovieInfo getMovie(@NonNull Context context, Long movieID)
|
||||
{
|
||||
Cursor cursor = context.getContentResolver().query(
|
||||
MovieContract.MovieEntry.CONTENT_URI,
|
||||
projection,
|
||||
movieProjection,
|
||||
MovieContract.MovieEntry.COLUMN_TMDB_ID + " = ?",
|
||||
new String[]{Long.toString(movieID)},
|
||||
null
|
||||
|
||||
Reference in New Issue
Block a user