mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-03 09:53:06 +02:00
Cleanup: Use Pair instead of String[2].
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package de.dennisguse.opentracks.content;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
@@ -95,8 +96,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
// Total distance
|
||||
writeDistance(
|
||||
stats.getTotalDistance(), builder, R.string.description_total_distance, lineBreak);
|
||||
writeDistance(stats.getTotalDistance(), builder, R.string.description_total_distance, lineBreak);
|
||||
|
||||
// Total time
|
||||
writeTime(stats.getTotalTime(), builder, R.string.description_total_time, lineBreak);
|
||||
@@ -108,8 +108,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
writeSpeed(stats.getAverageSpeed(), builder, R.string.description_average_speed, lineBreak);
|
||||
|
||||
// Average moving speed
|
||||
writeSpeed(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_speed,
|
||||
lineBreak);
|
||||
writeSpeed(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_speed, lineBreak);
|
||||
|
||||
// Max speed
|
||||
writeSpeed(stats.getMaxSpeed(), builder, R.string.description_max_speed, lineBreak);
|
||||
@@ -118,8 +117,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
writePace(stats.getAverageSpeed(), builder, R.string.description_average_pace_in_minute, lineBreak);
|
||||
|
||||
// Average moving pace
|
||||
writePace(stats.getAverageMovingSpeed(), builder,
|
||||
R.string.description_average_moving_pace_in_minute, lineBreak);
|
||||
writePace(stats.getAverageMovingSpeed(), builder, R.string.description_average_moving_pace_in_minute, lineBreak);
|
||||
|
||||
// Fastest pace
|
||||
writePace(stats.getMaxSpeed(), builder, R.string.description_fastest_pace_in_minute, lineBreak);
|
||||
@@ -187,8 +185,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
* @param lineBreak line break string
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void writeSpeed(
|
||||
double speed, StringBuilder builder, int resId, String lineBreak) {
|
||||
void writeSpeed(double speed, StringBuilder builder, int resId, String lineBreak) {
|
||||
double speedInKmHr = speed * UnitConversions.MS_TO_KMH;
|
||||
double speedInMiHr = speedInKmHr * UnitConversions.KM_TO_MI;
|
||||
builder.append(context.getString(resId, speedInKmHr, speedInMiHr));
|
||||
@@ -205,18 +202,13 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void writePace(double speed, StringBuilder builder, int resId, String lineBreak) {
|
||||
String[] paceInMetrics = StringUtils.getSpeedParts(context, speed, true, false);
|
||||
String[] paceInImperial = StringUtils.getSpeedParts(context, speed, false, false);
|
||||
Pair<String, String> paceInMetrics = StringUtils.getSpeedParts(context, speed, true, false);
|
||||
Pair<String, String> paceInImperial = StringUtils.getSpeedParts(context, speed, false, false);
|
||||
|
||||
if (paceInMetrics[0] == null) {
|
||||
paceInMetrics[0] = context.getString(R.string.value_unknown);
|
||||
}
|
||||
String formattedPaceMetrics = paceInMetrics.first != null ? paceInMetrics.first : context.getString(R.string.value_unknown);
|
||||
String formattedPaceImperial = paceInImperial.first != null ? paceInImperial.first : context.getString(R.string.value_unknown);
|
||||
|
||||
if (paceInImperial[0] == null) {
|
||||
paceInImperial[0] = context.getString(R.string.value_unknown);
|
||||
}
|
||||
|
||||
builder.append(context.getString(resId, paceInMetrics[0], paceInImperial[0]));
|
||||
builder.append(context.getString(resId, formattedPaceMetrics, formattedPaceImperial));
|
||||
builder.append(lineBreak);
|
||||
}
|
||||
|
||||
@@ -229,8 +221,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
* @param lineBreak line break string
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void writeElevation(
|
||||
double elevation, StringBuilder builder, int resId, String lineBreak) {
|
||||
void writeElevation(double elevation, StringBuilder builder, int resId, String lineBreak) {
|
||||
long elevationInM = Math.round(elevation);
|
||||
long elevationInFt = Math.round(elevation * UnitConversions.M_TO_FT);
|
||||
builder.append(context.getString(resId, elevationInM, elevationInFt));
|
||||
@@ -247,8 +238,7 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void writeGrade(double grade, StringBuilder builder, int resId, String lineBreak) {
|
||||
long gradeInPercent = Double.isNaN(grade) || Double.isInfinite(grade) ? 0L
|
||||
: Math.round(grade * 100);
|
||||
long gradeInPercent = Double.isNaN(grade) || Double.isInfinite(grade) ? 0L : Math.round(grade * 100);
|
||||
builder.append(context.getString(resId, gradeInPercent));
|
||||
builder.append(lineBreak);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package de.dennisguse.opentracks.util;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.util.Pair;
|
||||
import android.view.View;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
@@ -191,8 +192,8 @@ public class StatsUtils {
|
||||
* @param reportSpeed true if report speed
|
||||
*/
|
||||
private static void setSpeed(Context context, View view, int speedLabelId, int paceLabelId, double speed, boolean metricUnits, boolean reportSpeed) {
|
||||
String[] parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
|
||||
setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]);
|
||||
Pair<String, String> parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
|
||||
setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts.first, parts.second);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,8 +205,8 @@ public class StatsUtils {
|
||||
* @param metricUnits true if metric units
|
||||
*/
|
||||
private static void setDistanceValue(Context context, View view, double distance, boolean metricUnits) {
|
||||
String[] parts = StringUtils.getDistanceParts(context, distance, metricUnits);
|
||||
setItem(context, view, R.string.stats_distance, parts[0], parts[1]);
|
||||
Pair<String, String> parts = StringUtils.getDistanceParts(context, distance, metricUnits);
|
||||
setItem(context, view, R.string.stats_distance, parts.first, parts.second);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,6 +19,7 @@ import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Pair;
|
||||
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -142,22 +143,16 @@ public class StringUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance in an array of two strings.
|
||||
* The first string is the distance.
|
||||
* The second string is the unit.
|
||||
* The first string is null if the distance is invalid.
|
||||
* Get the formatted distance with unit.
|
||||
*
|
||||
* @param context the context
|
||||
* @param distance the distance
|
||||
* @param metricUnits true to use metric unit
|
||||
* @return the formatted distance (or null) and it's unit as {@link Pair}
|
||||
*/
|
||||
//TODO Return Pair<Distance, Unit>
|
||||
public static String[] getDistanceParts(Context context, double distance, boolean metricUnits) {
|
||||
String[] result = new String[2];
|
||||
static Pair<String, String> getDistanceParts(Context context, double distance, boolean metricUnits) {
|
||||
if (Double.isNaN(distance) || Double.isInfinite(distance)) {
|
||||
result[0] = null;
|
||||
result[1] = context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet);
|
||||
return result;
|
||||
return new Pair<>(null, context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet));
|
||||
}
|
||||
|
||||
int unitId;
|
||||
@@ -177,50 +172,45 @@ public class StringUtils {
|
||||
unitId = R.string.unit_feet;
|
||||
}
|
||||
}
|
||||
result[0] = formatDecimal(distance);
|
||||
result[1] = context.getString(unitId);
|
||||
return result;
|
||||
return new Pair<>(formatDecimal(distance), context.getString(unitId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the speed in an array of two strings.
|
||||
* The first string is the speed.
|
||||
* The second string is the unit.
|
||||
* The first string is null if speed is invalid.
|
||||
* Gets the formatted speed with unit.
|
||||
*
|
||||
* @param context the context
|
||||
* @param speed the speed
|
||||
* @param speed_mps the speed
|
||||
* @param metricUnits true to use metric unit
|
||||
* @param reportSpeed true to report speed
|
||||
* @param reportSpeed true to report speed; false for pace
|
||||
* @return the formatted speed (or null) and it's unit as {@link Pair}
|
||||
*/
|
||||
//TODO Return Pair<Distance, Unit>
|
||||
public static String[] getSpeedParts(Context context, double speed, boolean metricUnits, boolean reportSpeed) {
|
||||
String[] result = new String[2];
|
||||
public static Pair<String, String> getSpeedParts(Context context, double speed_mps, boolean metricUnits, boolean reportSpeed) {
|
||||
int unitId;
|
||||
if (metricUnits) {
|
||||
unitId = reportSpeed ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
|
||||
} else {
|
||||
unitId = reportSpeed ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
|
||||
}
|
||||
result[1] = context.getString(unitId);
|
||||
if (Double.isNaN(speed) || Double.isInfinite(speed)) {
|
||||
result[0] = null;
|
||||
return result;
|
||||
String unitString = context.getString(unitId);
|
||||
|
||||
if (Double.isNaN(speed_mps) || Double.isInfinite(speed_mps)) {
|
||||
return new Pair<>(null, unitString);
|
||||
}
|
||||
speed *= UnitConversions.MS_TO_KMH;
|
||||
|
||||
speed_mps *= UnitConversions.MS_TO_KMH;
|
||||
if (!metricUnits) {
|
||||
speed *= UnitConversions.KM_TO_MI;
|
||||
speed_mps *= UnitConversions.KM_TO_MI;
|
||||
}
|
||||
|
||||
if (reportSpeed) {
|
||||
result[0] = StringUtils.formatDecimal(speed);
|
||||
} else {
|
||||
// convert from hours to minutes
|
||||
double pace = speed == 0 ? 0.0 : 60.0 / speed;
|
||||
int minutes = (int) pace;
|
||||
int seconds = (int) Math.round((pace - minutes) * 60.0);
|
||||
result[0] = String.format(Locale.US, "%d:%02d", minutes, seconds);
|
||||
return new Pair<>(StringUtils.formatDecimal(speed_mps), unitString);
|
||||
}
|
||||
return result;
|
||||
|
||||
// convert from hours to minutes
|
||||
double pace = speed_mps == 0 ? 0.0 : 60.0 / speed_mps;
|
||||
int minutes = (int) pace;
|
||||
int seconds = (int) Math.round((pace - minutes) * 60.0);
|
||||
return new Pair<>(String.format(Locale.US, "%d:%02d", minutes, seconds), unitString);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user