Scroll to chapter currently being played

This is especially helpful for episodes that have a large number of
chapters.

Closes: #2853
This commit is contained in:
Anderson Mesquita 2019-05-04 23:56:16 -04:00
parent 0107cf22cc
commit 419c10632b
1 changed files with 21 additions and 1 deletions

View File

@ -5,6 +5,9 @@ import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ListView;
import java.util.List;
import java.util.ListIterator;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChaptersListAdapter;
import de.danoeh.antennapod.core.event.ServiceEvent;
@ -13,7 +16,6 @@ import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.greenrobot.event.EventBus;
public class ChaptersFragment extends ListFragment {
private static final String TAG = "ChaptersFragment";
private ChaptersListAdapter adapter;
@ -67,6 +69,23 @@ public class ChaptersFragment extends ListFragment {
controller = null;
}
private void scrollTo(int position) {
getListView().setSelection(position);
}
private int getCurrentChapter(Playable media) {
int currentPosition = controller.getPosition();
List<Chapter> chapters = media.getChapters();
for (final ListIterator<Chapter> it = chapters.listIterator(); it.hasNext(); ) {
Chapter chapter = it.next();
if (chapter.getStart() > currentPosition) {
return it.previousIndex() - 1;
}
}
return chapters.size() - 1;
}
public void onEventMainThread(ServiceEvent event) {
if (event.action == ServiceEvent.Action.SERVICE_STARTED && controller != null) {
controller.init();
@ -81,6 +100,7 @@ public class ChaptersFragment extends ListFragment {
setEmptyText(getString(R.string.no_chapters_label));
} else {
setEmptyText(null);
scrollTo(getCurrentChapter(media));
}
}
}