Show progress of current chapter (#4725)

This commit is contained in:
ByteHamster 2020-11-30 22:22:54 +01:00 committed by GitHub
parent d20557c1b0
commit 5645407620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View File

@ -22,12 +22,14 @@ import de.danoeh.antennapod.core.util.EmbeddedChapterImage;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.view.CircularProgressBar;
public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapter.ChapterHolder> {
private Playable media;
private final Callback callback;
private final Context context;
private int currentChapterIndex = -1;
private long currentChapterPosition = -1;
private boolean hasImages = false;
public ChaptersListAdapter(Context context, Callback callback) {
@ -48,7 +50,6 @@ public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapte
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(@NonNull ChapterHolder holder, int position) {
Chapter sc = getItem(position);
@ -82,9 +83,14 @@ public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapte
if (position == currentChapterIndex) {
int playingBackGroundColor = ThemeUtils.getColorFromAttr(context, R.attr.currently_playing_background);
holder.itemView.setBackgroundColor(playingBackGroundColor);
float progress = ((float) (currentChapterPosition - sc.getStart())) / duration;
progress = Math.max(progress, CircularProgressBar.MINIMUM_PERCENTAGE);
progress = Math.min(progress, CircularProgressBar.MAXIMUM_PERCENTAGE);
holder.progressBar.setPercentage(progress, position);
holder.secondaryActionIcon.setImageResource(ThemeUtils.getDrawableFromAttr(context, R.attr.av_replay));
} else {
holder.itemView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
holder.progressBar.setPercentage(0, null);
}
if (hasImages) {
@ -136,6 +142,7 @@ public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapte
final ImageView image;
final View secondaryActionButton;
final ImageView secondaryActionIcon;
final CircularProgressBar progressBar;
public ChapterHolder(@NonNull View itemView) {
super(itemView);
@ -146,14 +153,23 @@ public class ChaptersListAdapter extends RecyclerView.Adapter<ChaptersListAdapte
duration = itemView.findViewById(R.id.txtvDuration);
secondaryActionButton = itemView.findViewById(R.id.secondaryActionButton);
secondaryActionIcon = itemView.findViewById(R.id.secondaryActionIcon);
progressBar = itemView.findViewById(R.id.secondaryActionProgress);
}
}
public void notifyChapterChanged(int newChapterIndex) {
currentChapterIndex = newChapterIndex;
currentChapterPosition = getItem(newChapterIndex).getStart();
notifyDataSetChanged();
}
public void notifyTimeChanged(long timeMs) {
currentChapterPosition = timeMs;
// Passing an argument prevents flickering.
// See EpisodeItemListAdapter.notifyItemChangedCompat.
notifyItemChanged(currentChapterIndex, "foo");
}
private boolean ignoreChapter(Chapter c) {
return media.getDuration() > 0 && media.getDuration() < c.getStart();
}

View File

@ -106,6 +106,7 @@ public class ChaptersFragment extends Fragment {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(PlaybackPositionEvent event) {
updateChapterSelection(getCurrentChapter(media));
adapter.notifyTimeChanged(event.getPosition());
}
private int getCurrentChapter(Playable media) {

View File

@ -11,7 +11,8 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.util.ThemeUtils;
public class CircularProgressBar extends View {
private static final float EPSILON = 0.005f;
public static final float MINIMUM_PERCENTAGE = 0.005f;
public static final float MAXIMUM_PERCENTAGE = 1 - MINIMUM_PERCENTAGE;
private final Paint paintBackground = new Paint();
private final Paint paintProgress = new Paint();
@ -74,11 +75,11 @@ public class CircularProgressBar extends View {
bounds.set(padding, padding, getWidth() - padding, getHeight() - padding);
canvas.drawArc(bounds, 0, 360, false, paintBackground);
if (percentage > EPSILON && 1 - percentage > EPSILON) {
if (MINIMUM_PERCENTAGE <= percentage && percentage <= MAXIMUM_PERCENTAGE) {
canvas.drawArc(bounds, -90, percentage * 360, false, paintProgress);
}
if (Math.abs(percentage - targetPercentage) > EPSILON) {
if (Math.abs(percentage - targetPercentage) > MINIMUM_PERCENTAGE) {
float speed = 0.02f;
if (Math.abs(targetPercentage - percentage) < 0.1 && targetPercentage > percentage) {
speed = 0.006f;