Added ViewPager indicator that shows chapter availability

This commit is contained in:
ByteHamster 2020-02-02 00:19:07 +01:00
parent 63290ae762
commit f1769af246
4 changed files with 123 additions and 11 deletions

View File

@ -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"

View File

@ -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<Chapter> 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) {

View File

@ -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);
}
}

View File

@ -26,16 +26,13 @@
android:minHeight="?attr/actionBarSize"
tools:background="@android:color/darker_gray"/>
<com.viewpagerindicator.CirclePageIndicator
<de.danoeh.antennapod.view.PagerIndicatorView
android:id="@+id/page_indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_width="wrap_content"
android:layout_marginTop="-12dp"
android:layout_marginBottom="4dp"
android:background="@android:color/transparent"
app:fillColor="?android:attr/textColorSecondary"
app:strokeColor="?android:attr/textColorSecondary"
app:radius="4dp" />
android:background="@android:color/transparent"/>
</com.google.android.material.appbar.AppBarLayout>