diff options
author | ByteHamster <info@bytehamster.com> | 2020-01-12 10:35:32 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-01-12 10:35:32 +0100 |
commit | f5e819f5dc01384064b0468bc2be74373cd52a36 (patch) | |
tree | b1882e1418f268c5233fd0889919effd49e080cd /app/src/main/java/de/danoeh | |
parent | 2f0c627b15af26aace320e1984d6f398a7ee0abe (diff) | |
download | AntennaPod-f5e819f5dc01384064b0468bc2be74373cd52a36.zip |
Added colorful dots to statistics
Diffstat (limited to 'app/src/main/java/de/danoeh')
4 files changed, 83 insertions, 45 deletions
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<RecyclerView.ViewHolder> { 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<Recycle @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder h, int position) { if (getItemViewType(position) == TYPE_HEADER) { - onBindHeaderViewHolder((HeaderHolder) h); + HeaderHolder holder = (HeaderHolder) h; + holder.pieChart.setData(pieChartData); + holder.totalTime.setText(getHeaderValue()); } else { StatisticsHolder holder = (StatisticsHolder) h; DBReader.StatisticsItem statsItem = statisticsData.feeds.get(position - 1); @@ -78,12 +81,14 @@ public abstract class StatisticsListAdapter extends RecyclerView.Adapter<Recycle .into(holder.image); holder.title.setText(statsItem.feed.getTitle()); - onBindFeedViewHolder(holder, position); + holder.chip.setTextColor(pieChartData.getColorOfItem(position - 1)); + onBindFeedViewHolder(holder, statsItem); } } public void update(DBReader.StatisticsData statistics) { this.statisticsData = statistics; + pieChartData = generateChartData(statistics); notifyDataSetChanged(); } @@ -102,18 +107,22 @@ public abstract class StatisticsListAdapter extends RecyclerView.Adapter<Recycle ImageView image; TextView title; TextView value; + TextView chip; StatisticsHolder(View itemView) { super(itemView); image = itemView.findViewById(R.id.imgvCover); title = itemView.findViewById(R.id.txtvTitle); value = itemView.findViewById(R.id.txtvValue); + chip = itemView.findViewById(R.id.chip); } } abstract int getHeaderCaptionResourceId(); - abstract void onBindHeaderViewHolder(HeaderHolder holder); + abstract String getHeaderValue(); - abstract void onBindFeedViewHolder(StatisticsHolder holder, int position); + abstract PieChartView.PieChartData generateChartData(DBReader.StatisticsData statisticsData); + + abstract void onBindFeedViewHolder(StatisticsHolder holder, DBReader.StatisticsItem item); } diff --git a/app/src/main/java/de/danoeh/antennapod/view/PieChartView.java b/app/src/main/java/de/danoeh/antennapod/view/PieChartView.java index d1b2abf23..c0c74c42c 100644 --- a/app/src/main/java/de/danoeh/antennapod/view/PieChartView.java +++ b/app/src/main/java/de/danoeh/antennapod/view/PieChartView.java @@ -39,14 +39,10 @@ public class PieChartView extends AppCompatImageView { } /** - * Set array od names, array of values and array of colors. + * Set of data values to display. */ - public void setData(float[] dataValues) { - drawable.dataValues = dataValues; - drawable.valueSum = 0; - for (float datum : dataValues) { - drawable.valueSum += datum; - } + public void setData(PieChartData data) { + drawable.data = data; } @Override @@ -56,15 +52,50 @@ public class PieChartView extends AppCompatImageView { setMeasuredDimension(width, width / 2); } - private static class PieChartDrawable extends Drawable { - private static final float MIN_DEGREES = 10f; - private static final float PADDING_DEGREES = 3f; - private static final float STROKE_SIZE = 15f; + public static class PieChartData { private static final int[] COLOR_VALUES = new int[]{0xFF3775E6, 0xffe51c23, 0xffff9800, 0xff259b24, 0xff9c27b0, 0xff0099c6, 0xffdd4477, 0xff66aa00, 0xffb82e2e, 0xff316395, 0xff994499, 0xff22aa99, 0xffaaaa11, 0xff6633cc, 0xff0073e6}; - private float[] dataValues; - private float valueSum; + + private final float valueSum; + private final float[] values; + + public PieChartData(float[] values) { + this.values = values; + float valueSum = 0; + for (float datum : values) { + valueSum += datum; + } + this.valueSum = valueSum; + } + + public float getSum() { + return valueSum; + } + + public float getPercentageOfItem(int index) { + if (valueSum == 0) { + return 0; + } + return values[index] / valueSum; + } + + public boolean isLargeEnoughToDisplay(int index) { + return getPercentageOfItem(index) > 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; } |