Aggregated stats: ordered by number number of tracks per category.

This commit is contained in:
Dennis Guse
2020-12-13 22:44:44 +01:00
parent e1e7c24d9f
commit 5675dc800d
4 changed files with 59 additions and 50 deletions
@@ -62,17 +62,17 @@ public class AggregatedStatisticsAdapter extends BaseAdapter {
}
private boolean isSpeedSport(int position) {
String category = aggregatedStatistics.getSportName(position);
String category = aggregatedStatistics.getCategory(position);
return TrackIconUtils.isSpeedIcon(context, category);
}
private int getIcon(int position) {
String iconValue = TrackIconUtils.getIconValue(context, aggregatedStatistics.getSportName(position));
String iconValue = TrackIconUtils.getIconValue(context, aggregatedStatistics.getCategory(position));
return TrackIconUtils.getIconDrawable(iconValue);
}
private String getName(int position) {
return aggregatedStatistics.getSportName(position);
return aggregatedStatistics.getCategory(position);
}
private class ViewHolder {
@@ -1,6 +1,10 @@
package de.dennisguse.opentracks.viewmodels;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -10,50 +14,62 @@ import de.dennisguse.opentracks.stats.TrackStatistics;
public class AggregatedStatistics {
private final Map<String, AggregatedStatistic> aggregatedStatistics = new HashMap<>();
private final Map<String, AggregatedStatistic> dataMap = new HashMap<>();
public void aggregate(List<Track> tracks) {
private final List<AggregatedStatistic> dataList = new ArrayList<>();
public AggregatedStatistics(@NonNull List<Track> tracks) {
for (Track track : tracks) {
aggregate(track);
}
dataList.addAll(dataMap.values());
Collections.sort(dataList, (o1, o2) -> {
if (o1.getCountTracks() == o2.getCountTracks()) {
return o1.getCategory().compareTo(o2.getCategory());
}
return (o1.getCountTracks() < o2.getCountTracks() ? 1 : -1);
});
}
public void aggregate(Track track) {
@VisibleForTesting
public void aggregate(@NonNull Track track) {
String category = track.getCategory();
if (aggregatedStatistics.containsKey(category)) {
aggregatedStatistics.get(category).add(track.getTrackStatistics());
if (dataMap.containsKey(category)) {
dataMap.get(category).add(track.getTrackStatistics());
} else {
aggregatedStatistics.put(category, new AggregatedStatistic(track.getTrackStatistics()));
dataMap.put(category, new AggregatedStatistic(category, track.getTrackStatistics()));
}
}
public int getCount() {
return aggregatedStatistics.size();
}
public String getSportName(int position) {
return getKey(position);
return dataMap.size();
}
public AggregatedStatistic get(String category) {
return aggregatedStatistics.get(category);
return dataMap.get(category);
}
private String getKey(int position) {
return (new ArrayList<>(aggregatedStatistics.keySet())).get(position);
public String getCategory(int position) {
return dataList.get(position).getCategory();
}
public AggregatedStatistic getItem(int position) {
return (new ArrayList<>(aggregatedStatistics.values())).get(position);
return dataList.get(position);
}
public static class AggregatedStatistic {
private final String category;
private final TrackStatistics trackStatistics;
private int countTracks;
private int countTracks = 1;
public AggregatedStatistic(TrackStatistics trackStatistics) {
public AggregatedStatistic(String category, TrackStatistics trackStatistics) {
this.category = category;
this.trackStatistics = trackStatistics;
this.countTracks = 1;
}
public String getCategory() {
return category;
}
public TrackStatistics getTrackStatistics() {
@@ -33,8 +33,7 @@ public class AggregatedStatisticsModel extends AndroidViewModel {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(getApplication().getApplicationContext());
List<Track> tracks = contentProviderUtils.getTracks();
AggregatedStatistics aggregatedStatistics = new AggregatedStatistics();
aggregatedStatistics.aggregate(tracks);
AggregatedStatistics aggregatedStatistics = new AggregatedStatistics(tracks);
aggregatedStats.postValue(aggregatedStatistics);
}).start();