Merge branch 'develop' into extract-queue-item-view

This commit is contained in:
ByteHamster 2020-02-08 00:18:18 +01:00
commit f79606e3e9
3 changed files with 63 additions and 27 deletions

View File

@ -22,6 +22,7 @@ public class ChaptersListAdapter extends ArrayAdapter<Chapter> {
private Playable media; private Playable media;
private final Callback callback; private final Callback callback;
private int currentChapterIndex = -1;
public ChaptersListAdapter(Context context, int textViewResourceId, Callback callback) { public ChaptersListAdapter(Context context, int textViewResourceId, Callback callback) {
super(context, textViewResourceId); super(context, textViewResourceId);
@ -86,8 +87,7 @@ public class ChaptersListAdapter extends ArrayAdapter<Chapter> {
} }
}); });
Chapter current = ChapterUtils.getCurrentChapter(media); if (position == currentChapterIndex) {
if (current == sc) {
int playingBackGroundColor = ThemeUtils.getColorFromAttr(getContext(), R.attr.currently_playing_background); int playingBackGroundColor = ThemeUtils.getColorFromAttr(getContext(), R.attr.currently_playing_background);
holder.view.setBackgroundColor(playingBackGroundColor); holder.view.setBackgroundColor(playingBackGroundColor);
} else { } else {
@ -122,6 +122,11 @@ public class ChaptersListAdapter extends ArrayAdapter<Chapter> {
return counter; return counter;
} }
public void notifyChapterChanged(int newChapterIndex) {
currentChapterIndex = newChapterIndex;
notifyDataSetChanged();
}
private boolean ignoreChapter(Chapter c) { private boolean ignoreChapter(Chapter c) {
return media.getDuration() > 0 && media.getDuration() < c.getStart(); return media.getDuration() > 0 && media.getDuration() < c.getStart();
} }

View File

@ -11,7 +11,10 @@ import java.util.ListIterator;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.ChaptersListAdapter; import de.danoeh.antennapod.adapter.ChaptersListAdapter;
import de.danoeh.antennapod.adapter.QueueRecyclerAdapter;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
@ -20,14 +23,17 @@ import io.reactivex.Maybe;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class ChaptersFragment extends ListFragment { public class ChaptersFragment extends ListFragment {
private static final String TAG = "ChaptersFragment"; private static final String TAG = "ChaptersFragment";
private ChaptersListAdapter adapter; private ChaptersListAdapter adapter;
private PlaybackController controller; private PlaybackController controller;
private Disposable disposable; private Disposable disposable;
private EmptyViewHandler emptyView; private int focusedChapter = -1;
private Playable media;
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
@ -38,15 +44,19 @@ public class ChaptersFragment extends ListFragment {
final int vertPadding = getResources().getDimensionPixelSize(R.dimen.list_vertical_padding); final int vertPadding = getResources().getDimensionPixelSize(R.dimen.list_vertical_padding);
lv.setPadding(0, vertPadding, 0, vertPadding); lv.setPadding(0, vertPadding, 0, vertPadding);
emptyView = new EmptyViewHandler(getContext()); EmptyViewHandler emptyView = new EmptyViewHandler(getContext());
emptyView.attachToListView(lv); emptyView.attachToListView(lv);
emptyView.setIcon(R.attr.ic_bookmark); emptyView.setIcon(R.attr.ic_bookmark);
emptyView.setTitle(R.string.no_chapters_head_label); emptyView.setTitle(R.string.no_chapters_head_label);
emptyView.setMessage(R.string.no_chapters_label); emptyView.setMessage(R.string.no_chapters_label);
adapter = new ChaptersListAdapter(getActivity(), 0, pos -> { adapter = new ChaptersListAdapter(getActivity(), 0, pos -> {
if (controller.getStatus() != PlayerStatus.PLAYING) {
controller.playPause();
}
Chapter chapter = (Chapter) getListAdapter().getItem(pos); Chapter chapter = (Chapter) getListAdapter().getItem(pos);
controller.seekToChapter(chapter); controller.seekToChapter(chapter);
updateChapterSelection(pos);
}); });
setListAdapter(adapter); setListAdapter(adapter);
} }
@ -67,7 +77,7 @@ public class ChaptersFragment extends ListFragment {
} }
}; };
controller.init(); controller.init();
EventBus.getDefault().register(this);
loadMediaInfo(); loadMediaInfo();
} }
@ -85,6 +95,12 @@ public class ChaptersFragment extends ListFragment {
super.onStop(); super.onStop();
controller.release(); controller.release();
controller = null; controller = null;
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlaybackPositionEvent event) {
updateChapterSelection(getCurrentChapter(media));
} }
private int getCurrentChapter(Playable media) { private int getCurrentChapter(Playable media) {
@ -94,10 +110,9 @@ public class ChaptersFragment extends ListFragment {
int currentPosition = controller.getPosition(); int currentPosition = controller.getPosition();
List<Chapter> chapters = media.getChapters(); List<Chapter> chapters = media.getChapters();
for (final ListIterator<Chapter> it = chapters.listIterator(); it.hasNext(); ) { for (int i = 0; i < chapters.size(); i++) {
Chapter chapter = it.next(); if (chapters.get(i).getStart() > currentPosition) {
if (chapter.getStart() > currentPosition) { return i - 1;
return it.previousIndex() - 1;
} }
} }
return chapters.size() - 1; return chapters.size() - 1;
@ -108,27 +123,42 @@ public class ChaptersFragment extends ListFragment {
disposable.dispose(); disposable.dispose();
} }
disposable = Maybe.create(emitter -> { disposable = Maybe.create(emitter -> {
Playable media = controller.getMedia(); Playable media = controller.getMedia();
if (media != null) { if (media != null) {
emitter.onSuccess(media); emitter.onSuccess(media);
} else { } else {
emitter.onComplete(); emitter.onComplete();
} }
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(media -> onMediaChanged((Playable) media), .subscribe(media -> onMediaChanged((Playable) media),
error -> Log.e(TAG, Log.getStackTraceString(error))); error -> Log.e(TAG, Log.getStackTraceString(error)));
} }
private void onMediaChanged(Playable media) { private void onMediaChanged(Playable media) {
if (adapter != null) { this.media = media;
adapter.setMedia(media); focusedChapter = -1;
adapter.notifyDataSetChanged(); if (adapter == null) {
return;
}
adapter.setMedia(media);
adapter.notifyDataSetChanged();
int positionOfCurrentChapter = getCurrentChapter(media);
updateChapterSelection(positionOfCurrentChapter);
}
int positionOfCurrentChapter = getCurrentChapter(media); private void updateChapterSelection(int position) {
if (positionOfCurrentChapter != -1) { if (adapter == null) {
getListView().setSelection(positionOfCurrentChapter); return;
}
if (position != -1 && focusedChapter != position) {
focusedChapter = position;
adapter.notifyChapterChanged(focusedChapter);
if (getListView().getFirstVisiblePosition() >= position
|| getListView().getLastVisiblePosition() <= position) {
getListView().setSelectionFromTop(position, 100);
} }
} }
} }

View File

@ -186,6 +186,7 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
ComponentName cn = intent.getComponent(); ComponentName cn = intent.getComponent();
Intent mainIntent = Intent.makeRestartActivityTask(cn); Intent mainIntent = Intent.makeRestartActivityTask(cn);
startActivity(mainIntent); startActivity(mainIntent);
Runtime.getRuntime().exit(0);
}); });
d.show(); d.show();
} }