Merge pull request #177 from poisdeux/episodelistfragment
Implemented restoring state in TVShowEpisodeListFragment
This commit is contained in:
commit
0d34518c9b
|
@ -50,7 +50,7 @@ import org.xbmc.kore.service.LibrarySyncService;
|
|||
import org.xbmc.kore.utils.LogUtils;
|
||||
import org.xbmc.kore.utils.UIUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
|
@ -69,22 +69,32 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
public static final String TVSHOWID = "tvshow_id";
|
||||
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
|
||||
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
|
||||
private int tvshowId = -1;
|
||||
|
||||
// Activity listener
|
||||
private OnEpisodeSelectedListener listenerActivity;
|
||||
|
||||
private CursorTreeAdapter adapter;
|
||||
private SeasonsEpisodesAdapter adapter;
|
||||
|
||||
@InjectView(R.id.list) ExpandableListView seasonsEpisodesListView;
|
||||
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
|
||||
@InjectView(android.R.id.empty) TextView emptyView;
|
||||
|
||||
|
||||
@Override
|
||||
protected View createView(LayoutInflater inflater, ViewGroup container) {
|
||||
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);
|
||||
ButterKnife.inject(this, root);
|
||||
|
||||
//UIUtils.setSwipeRefreshLayoutColorScheme(swipeRefreshLayout);
|
||||
// Configure the adapter and start the loader
|
||||
adapter = new SeasonsEpisodesAdapter(getActivity());
|
||||
seasonsEpisodesListView.setAdapter(adapter);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
@ -131,8 +143,10 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
|
||||
if (parent.isGroupExpanded(groupPosition)) {
|
||||
parent.collapseGroup(groupPosition);
|
||||
groupsExpanded.remove(groupPosition);
|
||||
} else {
|
||||
parent.expandGroup(groupPosition);
|
||||
groupsExpanded.put(groupPosition, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -141,19 +155,23 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
@Override
|
||||
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
|
||||
// Get the movie id from the tag
|
||||
EpisodeViewHolder tag = (EpisodeViewHolder)v.getTag();
|
||||
EpisodeViewHolder tag = (EpisodeViewHolder) v.getTag();
|
||||
// Notify the activity
|
||||
listenerActivity.onEpisodeSelected(tvshowId, tag.episodeId);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Configure the adapter and start the loader
|
||||
adapter = new SeasonsEpisodesAdapter(getActivity());
|
||||
getLoaderManager().initLoader(LOADER_SEASONS, null, this);
|
||||
seasonsEpisodesListView.setAdapter(adapter);
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
public void onDetach() {
|
||||
super.onDetach();
|
||||
|
@ -265,38 +308,25 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
|
||||
LogUtils.LOGD(TAG, "onLoadFinished, Loader id: " + cursorLoader.getId() + ". Rows: " +
|
||||
cursor.getCount());
|
||||
switch (cursorLoader.getId()) {
|
||||
case LOADER_SEASONS:
|
||||
adapter.setGroupCursor(cursor);
|
||||
if (cursor.getCount() == 1) {
|
||||
// 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());
|
||||
}
|
||||
setState(cursor);
|
||||
// To prevent the empty text from appearing on the first load, set it now
|
||||
emptyView.setText(getString(R.string.no_episodes_found));
|
||||
break;
|
||||
default:
|
||||
// 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) {
|
||||
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;
|
||||
}
|
||||
|
@ -311,7 +341,7 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
break;
|
||||
default:
|
||||
// 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) {
|
||||
try {
|
||||
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.
|
||||
*/
|
||||
|
@ -458,11 +519,9 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
|
||||
ImageView indicator = (ImageView)view.findViewById(R.id.status_indicator);
|
||||
if (isExpanded) {
|
||||
// view.setPadding(0, separatorPadding, 0, 0);
|
||||
indicator.setImageResource(iconCollapseResId);
|
||||
} else {
|
||||
indicator.setImageResource(iconExpandResId);
|
||||
// view.setPadding(0, separatorPadding, 0, separatorPadding);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -490,16 +549,6 @@ public class TVShowEpisodeListFragment extends AbstractDetailsFragment
|
|||
} else {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue