Implemented restoring state in TVShowEpisodeListFragment
This commit is contained in:
parent
eca7d2b2ad
commit
c792326237
|
@ -50,7 +50,7 @@ import org.xbmc.kore.service.LibrarySyncService;
|
||||||
import org.xbmc.kore.utils.LogUtils;
|
import org.xbmc.kore.utils.LogUtils;
|
||||||
import org.xbmc.kore.utils.UIUtils;
|
import org.xbmc.kore.utils.UIUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.InjectView;
|
import butterknife.InjectView;
|
||||||
|
@ -69,22 +69,32 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
public static final String TVSHOWID = "tvshow_id";
|
public static final String TVSHOWID = "tvshow_id";
|
||||||
public static final String SEASON = "season";
|
public static final String SEASON = "season";
|
||||||
|
|
||||||
|
private final String BUNDLE_SAVEDINSTANCE_LISTPOSITION = "lposition";
|
||||||
|
private final String BUNDLE_SAVEDINSTANCE_ITEMPOSITION = "iposition";
|
||||||
|
private final String BUNDLE_SAVEDINSTANCE_GROUPSEXPANDED = "groupsexpanded";
|
||||||
|
|
||||||
// Loader IDs. Must be -1 to differentiate from group position
|
// Loader IDs. Must be -1 to differentiate from group position
|
||||||
private static final int LOADER_SEASONS = -1;
|
private static final int LOADER_SEASONS = -1;
|
||||||
|
|
||||||
|
private int listPosition = 0;
|
||||||
|
private int itemPosition = 0;
|
||||||
|
|
||||||
|
private HashMap<Integer, Boolean> groupsExpanded = new HashMap<>();
|
||||||
|
private HashMap<Integer, Boolean> childCursorsLoading;
|
||||||
|
private boolean isReturning; // used to determine if we are returning to this fragment and need to restore the state
|
||||||
|
|
||||||
// Displayed show id
|
// Displayed show id
|
||||||
private int tvshowId = -1;
|
private int tvshowId = -1;
|
||||||
|
|
||||||
// Activity listener
|
// Activity listener
|
||||||
private OnEpisodeSelectedListener listenerActivity;
|
private OnEpisodeSelectedListener listenerActivity;
|
||||||
|
|
||||||
private CursorTreeAdapter adapter;
|
private SeasonsEpisodesAdapter adapter;
|
||||||
|
|
||||||
@InjectView(R.id.list) ExpandableListView seasonsEpisodesListView;
|
@InjectView(R.id.list) ExpandableListView seasonsEpisodesListView;
|
||||||
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
||||||
@InjectView(android.R.id.empty) TextView emptyView;
|
@InjectView(android.R.id.empty) TextView emptyView;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected View createView(LayoutInflater inflater, ViewGroup container) {
|
protected View createView(LayoutInflater inflater, ViewGroup container) {
|
||||||
tvshowId = getArguments().getInt(TVSHOWID, -1);
|
tvshowId = getArguments().getInt(TVSHOWID, -1);
|
||||||
|
@ -96,7 +106,9 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_tvshow_episodes_list, container, false);
|
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_tvshow_episodes_list, container, false);
|
||||||
ButterKnife.inject(this, root);
|
ButterKnife.inject(this, root);
|
||||||
|
|
||||||
//UIUtils.setSwipeRefreshLayoutColorScheme(swipeRefreshLayout);
|
// Configure the adapter and start the loader
|
||||||
|
adapter = new SeasonsEpisodesAdapter(getActivity());
|
||||||
|
seasonsEpisodesListView.setAdapter(adapter);
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
@ -131,8 +143,10 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
||||||
if (parent.isGroupExpanded(groupPosition)) {
|
if (parent.isGroupExpanded(groupPosition)) {
|
||||||
parent.collapseGroup(groupPosition);
|
parent.collapseGroup(groupPosition);
|
||||||
|
groupsExpanded.remove(groupPosition);
|
||||||
} else {
|
} else {
|
||||||
parent.expandGroup(groupPosition);
|
parent.expandGroup(groupPosition);
|
||||||
|
groupsExpanded.put(groupPosition, true);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -148,12 +162,16 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Configure the adapter and start the loader
|
|
||||||
adapter = new SeasonsEpisodesAdapter(getActivity());
|
|
||||||
getLoaderManager().initLoader(LOADER_SEASONS, null, this);
|
|
||||||
seasonsEpisodesListView.setAdapter(adapter);
|
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
|
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
listPosition = savedInstanceState.getInt(BUNDLE_SAVEDINSTANCE_LISTPOSITION, 0);
|
||||||
|
itemPosition = savedInstanceState.getInt(BUNDLE_SAVEDINSTANCE_ITEMPOSITION, 0);
|
||||||
|
groupsExpanded = (HashMap) savedInstanceState.getSerializable(BUNDLE_SAVEDINSTANCE_GROUPSEXPANDED);
|
||||||
|
isReturning = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
initLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -166,6 +184,31 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
|
||||||
|
//Save scroll position
|
||||||
|
listPosition = seasonsEpisodesListView.getFirstVisiblePosition();
|
||||||
|
View itemView = seasonsEpisodesListView.getChildAt(0);
|
||||||
|
if (itemView != null) {
|
||||||
|
itemPosition = itemView.getTop();
|
||||||
|
} else {
|
||||||
|
itemPosition = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
isReturning = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
|
||||||
|
outState.putInt(BUNDLE_SAVEDINSTANCE_ITEMPOSITION, itemPosition);
|
||||||
|
outState.putInt(BUNDLE_SAVEDINSTANCE_LISTPOSITION, listPosition);
|
||||||
|
outState.putSerializable(BUNDLE_SAVEDINSTANCE_GROUPSEXPANDED, groupsExpanded);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetach() {
|
public void onDetach() {
|
||||||
super.onDetach();
|
super.onDetach();
|
||||||
|
@ -265,38 +308,25 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
||||||
LogUtils.LOGD(TAG, "onLoadFinished, Loader id: " + cursorLoader.getId() + ". Rows: " +
|
|
||||||
cursor.getCount());
|
|
||||||
switch (cursorLoader.getId()) {
|
switch (cursorLoader.getId()) {
|
||||||
case LOADER_SEASONS:
|
case LOADER_SEASONS:
|
||||||
adapter.setGroupCursor(cursor);
|
adapter.setGroupCursor(cursor);
|
||||||
if (cursor.getCount() == 1) {
|
setState(cursor);
|
||||||
// Force collapse and expand the group, to force a reload of the episodes
|
|
||||||
// cursor, otherwise if it is already expanded it won't reload (and won't
|
|
||||||
// apply the filters to the episodes list)
|
|
||||||
seasonsEpisodesListView.collapseGroup(0);
|
|
||||||
seasonsEpisodesListView.expandGroup(0);
|
|
||||||
} else if (cursor.getCount() > 0) {
|
|
||||||
// Expand the first season that has unseen episodes
|
|
||||||
cursor.moveToFirst();
|
|
||||||
do {
|
|
||||||
int unwatched = cursor.getInt(SeasonsListQuery.EPISODE) - cursor.getInt(SeasonsListQuery.WATCHEDEPISODES);
|
|
||||||
if (unwatched > 0) {
|
|
||||||
LogUtils.LOGD(TAG, "Expanding group: " + cursor.getPosition());
|
|
||||||
seasonsEpisodesListView.collapseGroup(cursor.getPosition());
|
|
||||||
seasonsEpisodesListView.expandGroup(cursor.getPosition());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while (cursor.moveToNext());
|
|
||||||
}
|
|
||||||
// To prevent the empty text from appearing on the first load, set it now
|
// To prevent the empty text from appearing on the first load, set it now
|
||||||
emptyView.setText(getString(R.string.no_episodes_found));
|
emptyView.setText(getString(R.string.no_episodes_found));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Check if the group cursor is set before setting the children cursor
|
// Check if the group cursor is set before setting the children cursor
|
||||||
// Somehow, when poping the back stack, the children cursor are reloaded first...
|
// Somehow, when popping the back stack, the children cursor are reloaded first...
|
||||||
if (adapter.getCursor() != null) {
|
if (adapter.getCursor() != null) {
|
||||||
adapter.setChildrenCursor(cursorLoader.getId(), cursor);
|
int id = cursorLoader.getId();
|
||||||
|
adapter.setChildrenCursor(id, cursor);
|
||||||
|
childCursorsLoading.remove(id);
|
||||||
|
if (isReturning && childCursorsLoading.isEmpty()) {
|
||||||
|
isReturning = false;
|
||||||
|
//All previous expanded child cursors loaded. Now we can finally restore the list position
|
||||||
|
seasonsEpisodesListView.setSelectionFromTop(listPosition, itemPosition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -311,7 +341,7 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Check if the group cursor is set before setting the children cursor
|
// Check if the group cursor is set before setting the children cursor
|
||||||
// Somehow, when poping the back stack, the children cursor are reloaded first...
|
// Somehow, when popping the back stack, the children cursor are reloaded first...
|
||||||
if (adapter.getCursor() != null) {
|
if (adapter.getCursor() != null) {
|
||||||
try {
|
try {
|
||||||
adapter.setChildrenCursor(cursorLoader.getId(), null);
|
adapter.setChildrenCursor(cursorLoader.getId(), null);
|
||||||
|
@ -324,6 +354,37 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setState(Cursor cursor) {
|
||||||
|
if (cursor.getCount() == 1) {
|
||||||
|
seasonsEpisodesListView.expandGroup(0);
|
||||||
|
} else if (cursor.getCount() > 0) {
|
||||||
|
cursor.moveToFirst();
|
||||||
|
do {
|
||||||
|
int position = cursor.getPosition();
|
||||||
|
|
||||||
|
// Expand the first season that has unseen episodes
|
||||||
|
int unwatched = cursor.getInt(SeasonsListQuery.EPISODE) - cursor.getInt(SeasonsListQuery.WATCHEDEPISODES);
|
||||||
|
if (groupsExpanded.isEmpty() && (unwatched > 0)) {
|
||||||
|
seasonsEpisodesListView.expandGroup(position);
|
||||||
|
groupsExpanded.put(position, true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (groupsExpanded.get(position) != null) {
|
||||||
|
seasonsEpisodesListView.expandGroup(position);
|
||||||
|
}
|
||||||
|
} while (cursor.moveToNext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initLoader() {
|
||||||
|
childCursorsLoading = new HashMap<>();
|
||||||
|
for (int id : groupsExpanded.keySet()) {
|
||||||
|
childCursorsLoading.put(id, true);
|
||||||
|
}
|
||||||
|
getLoaderManager().initLoader(LOADER_SEASONS, null, this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seasons list query parameters.
|
* Seasons list query parameters.
|
||||||
*/
|
*/
|
||||||
|
@ -458,11 +519,9 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
|
|
||||||
ImageView indicator = (ImageView)view.findViewById(R.id.status_indicator);
|
ImageView indicator = (ImageView)view.findViewById(R.id.status_indicator);
|
||||||
if (isExpanded) {
|
if (isExpanded) {
|
||||||
// view.setPadding(0, separatorPadding, 0, 0);
|
|
||||||
indicator.setImageResource(iconCollapseResId);
|
indicator.setImageResource(iconCollapseResId);
|
||||||
} else {
|
} else {
|
||||||
indicator.setImageResource(iconExpandResId);
|
indicator.setImageResource(iconExpandResId);
|
||||||
// view.setPadding(0, separatorPadding, 0, separatorPadding);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -490,16 +549,6 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
||||||
} else {
|
} else {
|
||||||
viewHolder.checkmarkView.setVisibility(View.GONE);
|
viewHolder.checkmarkView.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (isLastChild) {
|
|
||||||
// view.setPadding(0, 0, 0, separatorPadding);
|
|
||||||
// } else {
|
|
||||||
// view.setPadding(0, 0, 0, 0);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// UIUtils.loadImageIntoImageview(hostManager,
|
|
||||||
// cursor.getString(EpisodesListQuery.THUMBNAIL),
|
|
||||||
// viewHolder.artView);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue