From 1409537e5689495a5039ba737ee60cb50ea9b14c Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Tue, 15 Dec 2020 22:48:01 +0100 Subject: [PATCH] AggregatedStatistics: fixed ViewHolder pattern. --- .../adapters/AggregatedStatisticsAdapter.java | 152 ++++++++---------- .../adapters/IntervalStatisticsAdapter.java | 2 +- 2 files changed, 66 insertions(+), 88 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/adapters/AggregatedStatisticsAdapter.java b/src/main/java/de/dennisguse/opentracks/adapters/AggregatedStatisticsAdapter.java index bcf69c234..62d2a5fd2 100644 --- a/src/main/java/de/dennisguse/opentracks/adapters/AggregatedStatisticsAdapter.java +++ b/src/main/java/de/dennisguse/opentracks/adapters/AggregatedStatisticsAdapter.java @@ -46,49 +46,45 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { - AggregatedStatistics.AggregatedStatistic aggregatedStats = getItem(position); - ViewHolder viewHolder; + AggregatedStatistics.AggregatedStatistic aggregatedStatistic = getItem(position); - if (isSpeedSport(position)) { - viewHolder = new ViewSpeedHolder(); + ViewHolder viewHolder; + if (convertView == null) { + convertView = LayoutInflater.from(context).inflate(R.layout.aggregated_stats_list_item, parent, false); + viewHolder = new ViewHolder(convertView); + convertView.setTag(viewHolder); } else { - viewHolder = new ViewPaceHolder(); + viewHolder = (ViewHolder) convertView.getTag(); } - convertView = LayoutInflater.from(context).inflate(R.layout.aggregated_stats_list_item, parent, false); - viewHolder.setValues(convertView, getIcon(position), getName(position), aggregatedStats); + if (TrackIconUtils.isSpeedIcon(context, aggregatedStatistic.getCategory())) { + viewHolder.setSpeed(aggregatedStatistic); + } else { + viewHolder.setPace(aggregatedStatistic); + } return convertView; } - private boolean isSpeedSport(int position) { - String category = aggregatedStatistics.getCategory(position); - return TrackIconUtils.isSpeedIcon(context, category); - } - - private int getIcon(int position) { - String iconValue = TrackIconUtils.getIconValue(context, aggregatedStatistics.getCategory(position)); - return TrackIconUtils.getIconDrawable(iconValue); - } - - private String getName(int position) { - return aggregatedStatistics.getCategory(position); - } - private class ViewHolder { - protected ImageView sportIcon; - protected TextView typeLabel; - protected TextView numTracks; - protected TextView distance; - protected TextView distanceUnit; - protected TextView time; - protected boolean metricsUnits; - protected boolean reportSpeed; + private final ImageView sportIcon; + private final TextView typeLabel; + private final TextView numTracks; + private final TextView distance; + private final TextView distanceUnit; + private final TextView time; - public void setValues(View view, int iconDrawable, String name, AggregatedStatistics.AggregatedStatistic aggregatedStats) { - reportSpeed = PreferencesUtils.isReportSpeed(context, name); - metricsUnits = PreferencesUtils.isMetricUnits(context); + private final TextView avgSpeed; + private final TextView avgSpeedUnit; + private final TextView avgSpeedLabel; + private final TextView maxSpeed; + private final TextView maxSpeedUnit; + private final TextView maxSpeedLabel; + private boolean metricsUnits; + private boolean reportSpeed; + + public ViewHolder(View view) { sportIcon = view.findViewById(R.id.aggregated_stats_sport_icon); typeLabel = view.findViewById(R.id.aggregated_stats_type_label); numTracks = view.findViewById(R.id.aggregated_stats_num_tracks); @@ -96,86 +92,68 @@ public class AggregatedStatisticsAdapter extends BaseAdapter { distanceUnit = view.findViewById(R.id.aggregated_stats_distance_unit); time = view.findViewById(R.id.aggregated_stats_time); - sportIcon.setImageResource(iconDrawable); - typeLabel.setText(name); - numTracks.setText(StringUtils.valueInParentheses(String.valueOf(aggregatedStats.getCountTracks()))); - - Pair parts = StringUtils.getDistanceParts(context, aggregatedStats.getTrackStatistics().getTotalDistance(), metricsUnits); - distance.setText(parts.first); - distanceUnit.setText(parts.second); - - time.setText(StringUtils.formatElapsedTime(aggregatedStats.getTrackStatistics().getMovingTime())); - } - } - - private class ViewSpeedHolder extends ViewHolder { - private TextView avgSpeed; - private TextView avgSpeedUnit; - private TextView avgSpeedLabel; - private TextView maxSpeed; - private TextView maxSpeedUnit; - private TextView maxSpeedLabel; - - @Override - public void setValues(View view, int iconDrawable, String name, AggregatedStatistics.AggregatedStatistic aggregatedStats) { - super.setValues(view, iconDrawable, name, aggregatedStats); - avgSpeed = view.findViewById(R.id.aggregated_stats_avg_rate); avgSpeedUnit = view.findViewById(R.id.aggregated_stats_avg_rate_unit); avgSpeedLabel = view.findViewById(R.id.aggregated_stats_avg_rate_label); maxSpeed = view.findViewById(R.id.aggregated_stats_max_rate); maxSpeedUnit = view.findViewById(R.id.aggregated_stats_max_rate_unit); maxSpeedLabel = view.findViewById(R.id.aggregated_stats_max_rate_label); + } + public void setSpeed(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { + setCommonValues(aggregatedStatistic); { - Pair parts = StringUtils.getSpeedParts(context, aggregatedStats.getTrackStatistics().getAverageMovingSpeed(), metricsUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(context, aggregatedStatistic.getTrackStatistics().getAverageMovingSpeed(), metricsUnits, reportSpeed); avgSpeed.setText(parts.first); avgSpeedUnit.setText(parts.second); avgSpeedLabel.setText(context.getString(R.string.stats_average_moving_speed)); } { - Pair parts = StringUtils.getSpeedParts(context, aggregatedStats.getTrackStatistics().getMaxSpeed(), metricsUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(context, aggregatedStatistic.getTrackStatistics().getMaxSpeed(), metricsUnits, reportSpeed); maxSpeed.setText(parts.first); maxSpeedUnit.setText(parts.second); maxSpeedLabel.setText(context.getString(R.string.stats_max_speed)); } } - } - - private class ViewPaceHolder extends ViewHolder { - private TextView avgPace; - private TextView avgPaceUnit; - private TextView avgPaceLabel; - private TextView maxPace; - private TextView maxPaceUnit; - private TextView maxPaceLabel; - - @Override - public void setValues(View view, int iconDrawable, String name, AggregatedStatistics.AggregatedStatistic aggregatedStats) { - super.setValues(view, iconDrawable, name, aggregatedStats); - - avgPace = view.findViewById(R.id.aggregated_stats_avg_rate); - avgPaceUnit = view.findViewById(R.id.aggregated_stats_avg_rate_unit); - avgPaceLabel = view.findViewById(R.id.aggregated_stats_avg_rate_label); - - maxPace = view.findViewById(R.id.aggregated_stats_max_rate); - maxPaceUnit = view.findViewById(R.id.aggregated_stats_max_rate_unit); - maxPaceLabel = view.findViewById(R.id.aggregated_stats_max_rate_label); + public void setPace(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { + setCommonValues(aggregatedStatistic); { - Pair parts = StringUtils.getSpeedParts(context, aggregatedStats.getTrackStatistics().getAverageMovingSpeed(), metricsUnits, reportSpeed); - avgPace.setText(parts.first); - avgPaceUnit.setText(parts.second); - avgPaceLabel.setText(R.string.stats_average_moving_pace); + Pair parts = StringUtils.getSpeedParts(context, aggregatedStatistic.getTrackStatistics().getAverageMovingSpeed(), metricsUnits, reportSpeed); + avgSpeed.setText(parts.first); + avgSpeedUnit.setText(parts.second); + avgSpeedLabel.setText(context.getString(R.string.stats_average_moving_pace)); } { - Pair parts = StringUtils.getSpeedParts(context, aggregatedStats.getTrackStatistics().getMaxSpeed(), metricsUnits, reportSpeed); - maxPace.setText(parts.first); - maxPaceUnit.setText(parts.second); - maxPaceLabel.setText(R.string.stats_fastest_pace); + Pair parts = StringUtils.getSpeedParts(context, aggregatedStatistic.getTrackStatistics().getMaxSpeed(), metricsUnits, reportSpeed); + maxSpeed.setText(parts.first); + maxSpeedUnit.setText(parts.second); + maxSpeedLabel.setText(R.string.stats_fastest_pace); } } + + private void setCommonValues(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { + String category = aggregatedStatistic.getCategory(); + + reportSpeed = PreferencesUtils.isReportSpeed(context, category); + metricsUnits = PreferencesUtils.isMetricUnits(context); + + sportIcon.setImageResource(getIcon(aggregatedStatistic)); + typeLabel.setText(category); + numTracks.setText(StringUtils.valueInParentheses(String.valueOf(aggregatedStatistic.getCountTracks()))); + + Pair parts = StringUtils.getDistanceParts(context, aggregatedStatistic.getTrackStatistics().getTotalDistance(), metricsUnits); + distance.setText(parts.first); + distanceUnit.setText(parts.second); + + time.setText(StringUtils.formatElapsedTime(aggregatedStatistic.getTrackStatistics().getMovingTime())); + } + + private int getIcon(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { + String iconValue = TrackIconUtils.getIconValue(context, aggregatedStatistic.getCategory()); + return TrackIconUtils.getIconDrawable(iconValue); + } } } diff --git a/src/main/java/de/dennisguse/opentracks/adapters/IntervalStatisticsAdapter.java b/src/main/java/de/dennisguse/opentracks/adapters/IntervalStatisticsAdapter.java index 6b478353b..59e8ac59c 100644 --- a/src/main/java/de/dennisguse/opentracks/adapters/IntervalStatisticsAdapter.java +++ b/src/main/java/de/dennisguse/opentracks/adapters/IntervalStatisticsAdapter.java @@ -21,7 +21,6 @@ public class IntervalStatisticsAdapter extends ArrayAdapter intervalList, String category, StackMode stackMode) { @@ -52,6 +51,7 @@ public class IntervalStatisticsAdapter extends ArrayAdapter 0) { sumDistance_m = actualPosition * getItem(actualPosition - 1).getDistance_m() + interval.getDistance_m(); } else {