diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/DownloadStatisticsListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/DownloadStatisticsListAdapter.java index d29db5622..227eea6e0 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/DownloadStatisticsListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/DownloadStatisticsListAdapter.java @@ -5,9 +5,10 @@ import android.content.Context; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.util.Converter; +import de.danoeh.antennapod.view.PieChartView; /** - * Adapter for the download statistics list + * Adapter for the download statistics list. */ public class DownloadStatisticsListAdapter extends StatisticsListAdapter { @@ -21,25 +22,23 @@ public class DownloadStatisticsListAdapter extends StatisticsListAdapter { } @Override - void onBindHeaderViewHolder(HeaderHolder holder) { - long totalDownloadSize = 0; + String getHeaderValue() { + return Converter.byteToString((long) pieChartData.getSum()); + } - for (DBReader.StatisticsItem item: statisticsData.feeds) { - totalDownloadSize = totalDownloadSize + item.totalDownloadSize; - } - holder.totalTime.setText(Converter.byteToString(totalDownloadSize)); + @Override + PieChartView.PieChartData generateChartData(DBReader.StatisticsData statisticsData) { float[] dataValues = new float[statisticsData.feeds.size()]; for (int i = 0; i < statisticsData.feeds.size(); i++) { DBReader.StatisticsItem item = statisticsData.feeds.get(i); dataValues[i] = item.totalDownloadSize; } - holder.pieChart.setData(dataValues); + return new PieChartView.PieChartData(dataValues); } @Override - void onBindFeedViewHolder(StatisticsHolder holder, int position) { - DBReader.StatisticsItem statsItem = statisticsData.feeds.get(position - 1); - holder.value.setText(Converter.byteToString(statsItem.totalDownloadSize)); + void onBindFeedViewHolder(StatisticsHolder holder, DBReader.StatisticsItem item) { + holder.value.setText(Converter.byteToString(item.totalDownloadSize)); } } diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/PlaybackStatisticsListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/PlaybackStatisticsListAdapter.java index ce6ad2f83..8471569d3 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/PlaybackStatisticsListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/PlaybackStatisticsListAdapter.java @@ -6,9 +6,10 @@ import androidx.appcompat.app.AlertDialog; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.util.Converter; +import de.danoeh.antennapod.view.PieChartView; /** - * Adapter for the playback statistics list + * Adapter for the playback statistics list. */ public class PlaybackStatisticsListAdapter extends StatisticsListAdapter { @@ -28,20 +29,22 @@ public class PlaybackStatisticsListAdapter extends StatisticsListAdapter { } @Override - void onBindHeaderViewHolder(HeaderHolder holder) { - long time = countAll ? statisticsData.totalTimeCountAll : statisticsData.totalTime; - holder.totalTime.setText(Converter.shortLocalizedDuration(context, time)); + String getHeaderValue() { + return Converter.shortLocalizedDuration(context, (long) pieChartData.getSum()); + } + + @Override + PieChartView.PieChartData generateChartData(DBReader.StatisticsData statisticsData) { float[] dataValues = new float[statisticsData.feeds.size()]; for (int i = 0; i < statisticsData.feeds.size(); i++) { DBReader.StatisticsItem item = statisticsData.feeds.get(i); dataValues[i] = countAll ? item.timePlayedCountAll : item.timePlayed; } - holder.pieChart.setData(dataValues); + return new PieChartView.PieChartData(dataValues); } @Override - void onBindFeedViewHolder(StatisticsHolder holder, int position) { - DBReader.StatisticsItem statsItem = statisticsData.feeds.get(position - 1); + void onBindFeedViewHolder(StatisticsHolder holder, DBReader.StatisticsItem statsItem) { long time = countAll ? statsItem.timePlayedCountAll : statsItem.timePlayed; holder.value.setText(Converter.shortLocalizedDuration(context, time)); diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/StatisticsListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/StatisticsListAdapter.java index 76ae3c3d9..db65190f2 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/StatisticsListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/StatisticsListAdapter.java @@ -18,13 +18,14 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.view.PieChartView; /** - * Parent Adapter for the playback and download statistics list + * Parent Adapter for the playback and download statistics list. */ public abstract class StatisticsListAdapter extends RecyclerView.Adapter { private static final int TYPE_HEADER = 0; private static final int TYPE_FEED = 1; final Context context; - DBReader.StatisticsData statisticsData; + private DBReader.StatisticsData statisticsData; + PieChartView.PieChartData pieChartData; StatisticsListAdapter(Context context) { this.context = context; @@ -63,7 +64,9 @@ public abstract class StatisticsListAdapter extends RecyclerView.Adapter 0.05; + } + + public int getColorOfItem(int index) { + if (!isLargeEnoughToDisplay(index)) { + return Color.GRAY; + } + return COLOR_VALUES[index % COLOR_VALUES.length]; + } + } + + private static class PieChartDrawable extends Drawable { + private static final float PADDING_DEGREES = 3f; + private static final float STROKE_SIZE = 15f; + private PieChartData data; private final Paint paint; private PieChartDrawable() { @@ -78,22 +109,18 @@ public class PieChartView extends AppCompatImageView { @Override public void draw(@NonNull Canvas canvas) { - if (valueSum == 0) { - return; - } float radius = getBounds().height() - STROKE_SIZE; float center = getBounds().width() / 2.f; RectF arcBounds = new RectF(center - radius, STROKE_SIZE, center + radius, STROKE_SIZE + radius * 2); float startAngle = 180; - for (int i = 0; i < dataValues.length; i++) { - float datum = dataValues[i]; - float sweepAngle = (180f - PADDING_DEGREES) * (datum / valueSum); - if (sweepAngle < MIN_DEGREES) { + for (int i = 0; i < data.values.length; i++) { + if (!data.isLargeEnoughToDisplay(i)) { break; } - paint.setColor(COLOR_VALUES[i % COLOR_VALUES.length]); + paint.setColor(data.getColorOfItem(i)); float padding = i == 0 ? PADDING_DEGREES / 2 : PADDING_DEGREES; + float sweepAngle = (180f - PADDING_DEGREES) * data.getPercentageOfItem(i); canvas.drawArc(arcBounds, startAngle + padding, sweepAngle - padding, false, paint); startAngle = startAngle + sweepAngle; } diff --git a/app/src/main/res/layout/statistics_listitem.xml b/app/src/main/res/layout/statistics_listitem.xml index 8e0a7bf81..aaa0f666c 100644 --- a/app/src/main/res/layout/statistics_listitem.xml +++ b/app/src/main/res/layout/statistics_listitem.xml @@ -36,10 +36,26 @@ android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_toRightOf="@id/imgvCover" + android:layout_toEndOf="@id/imgvCover" android:layout_alignTop="@id/imgvCover" android:layout_alignWithParentIfMissing="true" tools:text="Feed title"/> + +