From f1769af246704a8452544a0015f7415a39fd656c Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sun, 2 Feb 2020 00:19:07 +0100 Subject: [PATCH] Added ViewPager indicator that shows chapter availability --- app/build.gradle | 1 - .../activity/MediaplayerInfoActivity.java | 17 ++- .../antennapod/view/PagerIndicatorView.java | 105 ++++++++++++++++++ .../res/layout/mediaplayerinfo_activity.xml | 11 +- 4 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/view/PagerIndicatorView.java diff --git a/app/build.gradle b/app/build.gradle index 498111189..9e862d982 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -161,7 +161,6 @@ dependencies { implementation "com.joanzapata.iconify:android-iconify-fontawesome:$iconifyVersion" implementation "com.joanzapata.iconify:android-iconify-material:$iconifyVersion" implementation "com.yqritc:recyclerview-flexibledivider:$recyclerviewFlexibledividerVersion" - implementation "com.githang:viewpagerindicator:2.5.1@aar" implementation "com.github.shts:TriangleLabelView:$triangleLabelViewVersion" implementation 'com.leinardi.android:speed-dial:3.0.0' implementation "com.github.AntennaPod:AntennaPod-AudioPlayer:$audioPlayerVersion" diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java index 21f4ad5be..62e8187f4 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerInfoActivity.java @@ -28,8 +28,6 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; -import com.viewpagerindicator.CirclePageIndicator; - import java.util.List; import de.danoeh.antennapod.R; @@ -38,6 +36,7 @@ import de.danoeh.antennapod.core.asynctask.FeedRemover; import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.event.FeedListUpdateEvent; import de.danoeh.antennapod.core.event.MessageEvent; +import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -59,6 +58,7 @@ import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.fragment.SubscriptionFragment; import de.danoeh.antennapod.menuhandler.NavDrawerActivity; +import de.danoeh.antennapod.view.PagerIndicatorView; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; @@ -102,6 +102,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem private int mPosition = -1; private ViewPager pager; + private PagerIndicatorView pageIndicator; private MediaplayerInfoPagerAdapter pagerAdapter; private Disposable disposable; @@ -261,7 +262,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem pager.setOffscreenPageLimit(3); pagerAdapter = new MediaplayerInfoPagerAdapter(getSupportFragmentManager()); pager.setAdapter(pagerAdapter); - CirclePageIndicator pageIndicator = findViewById(R.id.page_indicator); + pageIndicator = findViewById(R.id.page_indicator); pageIndicator.setViewPager(pager); loadLastFragment(); pager.onSaveInstanceState(); @@ -269,6 +270,16 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem navList.post(this::supportStartPostponedEnterTransition); } + @Override + boolean loadMediaInfo() { + if (controller != null) { + List chapters = controller.getMedia().getChapters(); + boolean hasChapters = chapters != null && !chapters.isEmpty(); + pageIndicator.setDisabledPage(hasChapters ? -1 : 2); + } + return super.loadMediaInfo(); + } + @Override protected void onReloadNotification(int notificationCode) { if (notificationCode == PlaybackService.EXTRA_CODE_VIDEO) { diff --git a/app/src/main/java/de/danoeh/antennapod/view/PagerIndicatorView.java b/app/src/main/java/de/danoeh/antennapod/view/PagerIndicatorView.java new file mode 100644 index 000000000..60ef820a9 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/PagerIndicatorView.java @@ -0,0 +1,105 @@ +package de.danoeh.antennapod.view; + +import android.content.Context; +import android.content.res.TypedArray; +import android.database.DataSetObserver; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.util.AttributeSet; +import android.view.View; +import androidx.annotation.Nullable; +import androidx.vectordrawable.graphics.drawable.ArgbEvaluator; +import androidx.viewpager.widget.ViewPager; + +public class PagerIndicatorView extends View { + private final Paint paint = new Paint(); + private float position = 0; + private int numPages = 0; + private int disabledPage = -1; + private int circleColor = 0; + private int circleColorHighlight = -1; + + public PagerIndicatorView(Context context) { + super(context); + setup(); + } + + public PagerIndicatorView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + setup(); + } + + public PagerIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setup(); + } + + private void setup() { + paint.setAntiAlias(true); + paint.setStyle(Paint.Style.FILL); + + int[] colorAttrs = new int[] { android.R.attr.textColorSecondary }; + TypedArray a = getContext().obtainStyledAttributes(colorAttrs); + circleColorHighlight = a.getColor(0, 0xffffffff); + circleColor = (Integer) new ArgbEvaluator().evaluate(0.8f, 0x00ffffff, circleColorHighlight); + a.recycle(); + } + + public void setViewPager(ViewPager pager) { + numPages = pager.getAdapter().getCount(); + pager.getAdapter().registerDataSetObserver(new DataSetObserver() { + @Override + public void onChanged() { + numPages = pager.getAdapter().getCount(); + invalidate(); + } + }); + pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + PagerIndicatorView.this.position = position + positionOffset; + invalidate(); + } + }); + } + + public void setDisabledPage(int disabledPage) { + this.disabledPage = disabledPage; + invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + for (int i = 0; i < numPages; i++) { + if ((int) Math.floor(position) == i) { + // This is the current dot + drawCircle(canvas, i, (float) (1 - (position - Math.floor(position)))); + } else if ((int) Math.ceil(position) == i) { + // This is the next dot + drawCircle(canvas, i, (float) (position - Math.floor(position))); + } else { + drawCircle(canvas, i, 0); + } + } + } + + private void drawCircle(Canvas canvas, int position, float frac) { + float circleRadiusSmall = canvas.getHeight() * 0.26f; + float circleRadiusBig = canvas.getHeight() * 0.35f; + float circleRadiusDelta = (circleRadiusBig - circleRadiusSmall); + float start = 0.5f * (canvas.getWidth() - numPages * 1.5f * canvas.getHeight()); + paint.setStrokeWidth(canvas.getHeight() * 0.3f); + + if (position == disabledPage) { + paint.setStyle(Paint.Style.STROKE); + } else { + paint.setStyle(Paint.Style.FILL_AND_STROKE); + } + + paint.setColor((Integer) new ArgbEvaluator().evaluate(frac, circleColor, circleColorHighlight)); + canvas.drawCircle(start + (position * 1.5f + 0.75f) * canvas.getHeight(), 0.5f * canvas.getHeight(), + circleRadiusSmall + frac * circleRadiusDelta, paint); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/mediaplayerinfo_activity.xml b/app/src/main/res/layout/mediaplayerinfo_activity.xml index 636cbb015..6f9aacd2f 100644 --- a/app/src/main/res/layout/mediaplayerinfo_activity.xml +++ b/app/src/main/res/layout/mediaplayerinfo_activity.xml @@ -26,16 +26,13 @@ android:minHeight="?attr/actionBarSize" tools:background="@android:color/darker_gray"/> - + android:background="@android:color/transparent"/>