fixed restore state for favorites list
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.Context;
|
|||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.GridLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
@@ -44,6 +45,7 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
private static final int NUM_COLUMNS_LAND = 5;
|
private static final int NUM_COLUMNS_LAND = 5;
|
||||||
/**
|
/**
|
||||||
* The currently selected sorting order of the list
|
* The currently selected sorting order of the list
|
||||||
|
*
|
||||||
* @see TMDBApi
|
* @see TMDBApi
|
||||||
*/
|
*/
|
||||||
private int mSortingOrder = SORT_ORDER_MOST_POPULAR;
|
private int mSortingOrder = SORT_ORDER_MOST_POPULAR;
|
||||||
@@ -54,8 +56,10 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
|
|
||||||
private MovieList mMovieList;
|
private MovieList mMovieList;
|
||||||
|
|
||||||
@Bind(R.id.movie_list_grid) RecyclerView mMovieGridView;
|
@Bind(R.id.movie_list_grid)
|
||||||
@Bind(R.id.movie_list_progressBar) ProgressBar mProgressBar;
|
RecyclerView mMovieGridView;
|
||||||
|
@Bind(R.id.movie_list_progressBar)
|
||||||
|
ProgressBar mProgressBar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holding a reference to running api calls. This makes it possible to cancel them if
|
* Holding a reference to running api calls. This makes it possible to cancel them if
|
||||||
@@ -80,12 +84,15 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
mMovieGridView.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns()));
|
mMovieGridView.setLayoutManager(new GridLayoutManager(getActivity(), getNumColumns()));
|
||||||
|
if (savedInstanceState != null)
|
||||||
|
restoreState(savedInstanceState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreState(@NonNull Bundle savedInstanceState) {
|
||||||
|
mSortingOrder = savedInstanceState.getInt("sort_order");
|
||||||
|
if (savedInstanceState.containsKey("movielist"))
|
||||||
|
mMovieList = savedInstanceState.getParcelable("movielist");
|
||||||
|
|
||||||
if(savedInstanceState != null) {
|
|
||||||
mSortingOrder = savedInstanceState.getInt("sort_order");
|
|
||||||
if(savedInstanceState.containsKey("movielist"))
|
|
||||||
mMovieList = savedInstanceState.getParcelable("movielist");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,7 +101,7 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
*/
|
*/
|
||||||
private int getNumColumns() {
|
private int getNumColumns() {
|
||||||
int orientation = getActivity().getResources().getConfiguration().orientation;
|
int orientation = getActivity().getResources().getConfiguration().orientation;
|
||||||
if(orientation == Configuration.ORIENTATION_LANDSCAPE)
|
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
|
||||||
return NUM_COLUMNS_LAND;
|
return NUM_COLUMNS_LAND;
|
||||||
else
|
else
|
||||||
return NUM_COLUMNS_PORTRAIT;
|
return NUM_COLUMNS_PORTRAIT;
|
||||||
@@ -112,10 +119,10 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
* starts the download of the new movie list if no restored one is present
|
* starts the download of the new movie list if no restored one is present
|
||||||
*/
|
*/
|
||||||
private void initMovieList() {
|
private void initMovieList() {
|
||||||
if(mMovieList == null)
|
if (mMovieList == null)
|
||||||
loadMovieList();
|
loadMovieList();
|
||||||
else
|
else
|
||||||
mMovieGridView.setAdapter(new MovieListAdapter( (MovieNavigation) getActivity(), mMovieList));
|
mMovieGridView.setAdapter(new MovieListAdapter((MovieNavigation) getActivity(), mMovieList));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -150,7 +157,7 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
*/
|
*/
|
||||||
private void loadMovieList() {
|
private void loadMovieList() {
|
||||||
stopLoadingMovies(); // stop any running query before starting a new one
|
stopLoadingMovies(); // stop any running query before starting a new one
|
||||||
if(mSortingOrder != SORT_ORDER_FAVORITES) {
|
if (mSortingOrder != SORT_ORDER_FAVORITES) {
|
||||||
mProgressBar.setVisibility(View.VISIBLE);
|
mProgressBar.setVisibility(View.VISIBLE);
|
||||||
mMovieListCall = TMDBApi.discoverMovies(mSortingOrder, getString(R.string.api_key));
|
mMovieListCall = TMDBApi.discoverMovies(mSortingOrder, getString(R.string.api_key));
|
||||||
mMovieListCall.enqueue(this);
|
mMovieListCall.enqueue(this);
|
||||||
@@ -160,13 +167,14 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
|
|
||||||
private void loadMovieListFromFavorites() {
|
private void loadMovieListFromFavorites() {
|
||||||
// TODO async?
|
// TODO async?
|
||||||
|
mMovieList = null;
|
||||||
Cursor cursor = new FavoriteProviderHelper(getContext().getContentResolver()).getAllFavorites();
|
Cursor cursor = new FavoriteProviderHelper(getContext().getContentResolver()).getAllFavorites();
|
||||||
mMovieGridView.setAdapter(new CursorMovieListAdapter(cursor,(MovieNavigation) getActivity()));
|
mMovieGridView.setAdapter(new CursorMovieListAdapter(cursor, (MovieNavigation) getActivity()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Response<MovieList> response, Retrofit retrofit) {
|
public void onResponse(Response<MovieList> response, Retrofit retrofit) {
|
||||||
if(response.isSuccess()) {
|
if (response.isSuccess()) {
|
||||||
mMovieList = response.body();
|
mMovieList = response.body();
|
||||||
setListToGrid(mMovieList);
|
setListToGrid(mMovieList);
|
||||||
}
|
}
|
||||||
@@ -177,7 +185,7 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
public void onFailure(Throwable t) {
|
public void onFailure(Throwable t) {
|
||||||
Log.d(TAG, t.getClass().getSimpleName() + " " + t.getMessage());
|
Log.d(TAG, t.getClass().getSimpleName() + " " + t.getMessage());
|
||||||
mProgressBar.setVisibility(View.GONE);
|
mProgressBar.setVisibility(View.GONE);
|
||||||
if(!t.getMessage().equals("Canceled") && getActivity() != null)
|
if (!t.getMessage().equals("Canceled") && getActivity() != null)
|
||||||
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
|
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +197,7 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
* Stops the current api query
|
* Stops the current api query
|
||||||
*/
|
*/
|
||||||
private void stopLoadingMovies() {
|
private void stopLoadingMovies() {
|
||||||
if(mMovieListCall != null) {
|
if (mMovieListCall != null) {
|
||||||
mMovieListCall.cancel();
|
mMovieListCall.cancel();
|
||||||
mMovieListCall = null;
|
mMovieListCall = null;
|
||||||
}
|
}
|
||||||
@@ -204,14 +212,14 @@ public class MovieListFragment extends Fragment implements Callback<MovieList> {
|
|||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
if(!(context instanceof MovieNavigation))
|
if (!(context instanceof MovieNavigation))
|
||||||
throw new RuntimeException("Activity must implement " + MovieNavigation.class.getSimpleName());
|
throw new RuntimeException("Activity must implement " + MovieNavigation.class.getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
outState.putInt("sort_order", mSortingOrder);
|
outState.putInt("sort_order", mSortingOrder);
|
||||||
if(mMovieList != null && mSortingOrder != SORT_ORDER_FAVORITES)
|
if (mMovieList != null && mSortingOrder != SORT_ORDER_FAVORITES)
|
||||||
outState.putParcelable("movielist", mMovieList);
|
outState.putParcelable("movielist", mMovieList);
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user