mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 16:15:55 +01:00
Refactor formatting of distance strings
- localizing number format - allows translation of distance units - refactor
This commit is contained in:
parent
5e666df24a
commit
7cf562c8e6
@ -52,7 +52,6 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
@ -68,6 +67,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DailyTotals;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.WidgetPreferenceStorage;
|
||||
|
||||
@ -163,29 +163,11 @@ public class Widget extends AppWidgetProvider {
|
||||
int distanceGoal = activityUser.getDistanceGoalMeters() * 100;
|
||||
int stepLength = activityUser.getStepLengthCm();
|
||||
double distanceMeters = dailyTotals[0] * stepLength * 0.01;
|
||||
double distanceFeet = distanceMeters * 3.28084f;
|
||||
double distanceFormatted = 0;
|
||||
|
||||
String unit = "###m";
|
||||
distanceFormatted = distanceMeters;
|
||||
if (distanceMeters > 2000) {
|
||||
distanceFormatted = distanceMeters / 1000;
|
||||
unit = "###.#km";
|
||||
}
|
||||
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
|
||||
unit = "###ft";
|
||||
distanceFormatted = distanceFeet;
|
||||
if (distanceFeet > 6000) {
|
||||
distanceFormatted = distanceFeet * 0.0001893939f;
|
||||
unit = "###.#mi";
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(unit);
|
||||
String distanceFormatted = FormatUtils.getFormattedDistanceLabel(distanceMeters);
|
||||
|
||||
views.setTextViewText(R.id.todaywidget_steps, String.format("%1s", steps));
|
||||
views.setTextViewText(R.id.todaywidget_sleep, String.format("%1s", getHM(sleep)));
|
||||
views.setTextViewText(R.id.todaywidget_distance, df.format(distanceFormatted));
|
||||
views.setTextViewText(R.id.todaywidget_distance, distanceFormatted);
|
||||
views.setProgressBar(R.id.todaywidget_steps_progress, stepGoal, steps, false);
|
||||
views.setProgressBar(R.id.todaywidget_sleep_progress, sleepGoalMinutes, sleep, false);
|
||||
views.setProgressBar(R.id.todaywidget_distance_progress, distanceGoal, steps * stepLength, false);
|
||||
|
@ -31,6 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
||||
|
||||
public class ActivityListingAdapter extends AbstractActivityListingAdapter<ActivitySession> {
|
||||
public static final String CHART_COLOR_START = "#e74c3c";
|
||||
@ -247,37 +248,7 @@ public class ActivityListingAdapter extends AbstractActivityListingAdapter<Activ
|
||||
|
||||
@Override
|
||||
protected String getDistanceLabel(ActivitySession item) {
|
||||
double distanceMeters = item.getDistance();
|
||||
double distanceFeet = distanceMeters * 3.28084f;
|
||||
double distanceFormatted = 0;
|
||||
|
||||
String unit = "###m";
|
||||
distanceFormatted = distanceMeters;
|
||||
if (distanceMeters > 2000) {
|
||||
distanceFormatted = distanceMeters / 1000;
|
||||
unit = "###.#km";
|
||||
}
|
||||
|
||||
if (distanceMeters > 999000) {
|
||||
distanceFormatted = distanceMeters / 1000;
|
||||
unit = "###km";
|
||||
}
|
||||
|
||||
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
|
||||
unit = "###ft";
|
||||
distanceFormatted = distanceFeet;
|
||||
if (distanceFeet > 6000) {
|
||||
distanceFormatted = distanceFeet * 0.0001893939f;
|
||||
unit = "###.#mi";
|
||||
}
|
||||
if (distanceFeet > 5274721) {
|
||||
distanceFormatted = distanceFeet * 0.0001893939f;
|
||||
unit = "###mi";
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(unit);
|
||||
return df.format(distanceFormatted);
|
||||
return FormatUtils.getFormattedDistanceLabel(item.getDistance());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -49,6 +49,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummaryJsonSummary;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.activities.ActivitySummariesFilter.ALL_DEVICES;
|
||||
@ -222,8 +223,7 @@ public class ActivitySummariesAdapter extends AbstractActivityListingAdapter<Bas
|
||||
durationSumView.setText(String.format("%s", DateTimeUtils.formatDurationHoursMinutes((long) durationSum, TimeUnit.MILLISECONDS)));
|
||||
caloriesBurntSumView.setText(String.format("%s %s", (long) caloriesBurntSum, context.getString(R.string.calories_unit)));
|
||||
distanceSumView.setText(String.format("%s %s", df.format(distanceSum / 1000), context.getString(R.string.km)));
|
||||
distanceSumView.setText(getLabel(distanceSum));
|
||||
|
||||
distanceSumView.setText(FormatUtils.getFormattedDistanceLabel(distanceSum));
|
||||
|
||||
activeSecondsSumView.setText(String.format("%s", DateTimeUtils.formatDurationHoursMinutes((long) activeSecondsSum, TimeUnit.SECONDS)));
|
||||
activitiesCountView.setText(String.valueOf(activitiesCount));
|
||||
@ -316,31 +316,6 @@ public class ActivitySummariesAdapter extends AbstractActivityListingAdapter<Bas
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String getLabel(double distance) {
|
||||
double distanceMetric = distance;
|
||||
double distanceImperial = distanceMetric * 3.28084f;
|
||||
double distanceFormatted = 0;
|
||||
|
||||
String unit = "###m";
|
||||
distanceFormatted = distanceMetric;
|
||||
if (distanceMetric > 2000) {
|
||||
distanceFormatted = distanceMetric / 1000;
|
||||
unit = "###.#km";
|
||||
}
|
||||
|
||||
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
|
||||
unit = "###ft";
|
||||
distanceFormatted = distanceImperial;
|
||||
if (distanceImperial > 6000) {
|
||||
distanceFormatted = distanceImperial * 0.0001893939f;
|
||||
unit = "###.#mi";
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(unit);
|
||||
return df.format(distanceFormatted);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHrLabel(BaseActivitySummary item) {
|
||||
return null;
|
||||
|
@ -62,7 +62,6 @@ import com.jaredrummler.android.colorpicker.ColorPickerDialogListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
@ -98,6 +97,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FormatUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
/**
|
||||
@ -947,31 +947,13 @@ public class GBDeviceAdapterv2 extends RecyclerView.Adapter<GBDeviceAdapterv2.Vi
|
||||
int distanceGoal = activityUser.getDistanceGoalMeters() * 100;
|
||||
int stepLength = activityUser.getStepLengthCm();
|
||||
double distanceMeters = dailyTotals[0] * stepLength * 0.01;
|
||||
double distanceFeet = distanceMeters * 3.28084f;
|
||||
double distanceFormatted = 0;
|
||||
|
||||
String unit = "###m";
|
||||
distanceFormatted = distanceMeters;
|
||||
if (distanceMeters > 2000) {
|
||||
distanceFormatted = distanceMeters / 1000;
|
||||
unit = "###.#km";
|
||||
}
|
||||
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
|
||||
unit = "###ft";
|
||||
distanceFormatted = distanceFeet;
|
||||
if (distanceFeet > 6000) {
|
||||
distanceFormatted = distanceFeet * 0.0001893939f;
|
||||
unit = "###.#mi";
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(unit);
|
||||
String distanceFormatted = FormatUtils.getFormattedDistanceLabel(distanceMeters);
|
||||
|
||||
setUpChart(holder.TotalStepsChart);
|
||||
setChartsData(holder.TotalStepsChart, steps, stepGoal, context.getString(R.string.steps), String.valueOf(steps), context);
|
||||
|
||||
setUpChart(holder.TotalDistanceChart);
|
||||
setChartsData(holder.TotalDistanceChart, steps * stepLength, distanceGoal, context.getString(R.string.distance), df.format(distanceFormatted), context);
|
||||
setChartsData(holder.TotalDistanceChart, steps * stepLength, distanceGoal, context.getString(R.string.distance), distanceFormatted, context);
|
||||
|
||||
setUpChart(holder.SleepTimeChart);
|
||||
setChartsData(holder.SleepTimeChart, sleep, sleepGoalMinutes, context.getString(R.string.prefs_activity_in_device_card_sleep_title), String.format("%1s", getHM(sleep)), context);
|
||||
|
@ -0,0 +1,62 @@
|
||||
/* Copyright (C) 2020-2021 Petr Vaněk
|
||||
|
||||
This file is part of Gadgetbridge.
|
||||
|
||||
Gadgetbridge is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Gadgetbridge is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity;
|
||||
|
||||
public class FormatUtils {
|
||||
|
||||
/**
|
||||
* This method formats a given number into a string with m (meters), km (kilometers),
|
||||
* ft (feet) or mi (miles). These string units can be translated, so the resulting text might
|
||||
* be different in each language.
|
||||
* The number is also localizes through DecimalFormatSymbols based on current locale.
|
||||
*
|
||||
* @param distance
|
||||
*/
|
||||
public static String getFormattedDistanceLabel(double distance) {
|
||||
double distanceMeters = distance;
|
||||
double distanceFeet = distanceMeters * 3.28084f;
|
||||
double distanceFormatted = 0;
|
||||
|
||||
String unit = GBApplication.getContext().getString(R.string.distance_format_meters);
|
||||
distanceFormatted = distanceMeters;
|
||||
if (distanceMeters > 2000) {
|
||||
distanceFormatted = distanceMeters / 1000;
|
||||
unit = GBApplication.getContext().getString(R.string.distance_format_kilometers);
|
||||
}
|
||||
String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));
|
||||
if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) {
|
||||
unit = GBApplication.getContext().getString(R.string.distance_format_feet);
|
||||
distanceFormatted = distanceFeet;
|
||||
if (distanceFeet > 6000) {
|
||||
distanceFormatted = distanceFeet * 0.0001893939f;
|
||||
unit = GBApplication.getContext().getString(R.string.distance_format_miles);
|
||||
}
|
||||
}
|
||||
DecimalFormatSymbols symbols = new DecimalFormatSymbols(GBApplication.getLanguage());
|
||||
DecimalFormat df = new DecimalFormat(unit, symbols);
|
||||
|
||||
return df.format(distanceFormatted);
|
||||
}
|
||||
}
|
@ -1459,4 +1459,12 @@
|
||||
<string name="pref_media_forward">Skip forward</string>
|
||||
<string name="pref_media_rewind">Skip back</string>
|
||||
<string name="pref_device_action_broadcast">Send Broadcast</string>
|
||||
<!-- Translators: the ### indicate number of digits, keep intact -->
|
||||
<string name="distance_format_meters">###m</string>
|
||||
<!-- Translators: the ### indicate number of digits, keep intact -->
|
||||
<string name="distance_format_kilometers">###.#km</string>
|
||||
<!-- Translators: the ### indicate number of digits, keep intact -->
|
||||
<string name="distance_format_miles">###.#mi</string>
|
||||
<!-- Translators: the ### indicate number of digits, keep intact -->
|
||||
<string name="distance_format_feet">###ft</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user