Added GetMeasurementUnits function.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako 2022-09-12 08:25:06 +03:00
parent d79bae7de0
commit 46294f2240
11 changed files with 35 additions and 86 deletions

View file

@ -947,7 +947,9 @@ Java_com_mapswithme_maps_Framework_nativeFormatAltitude(JNIEnv * env, jclass, jd
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_Framework_nativeFormatSpeed(JNIEnv * env, jclass, jdouble speed)
{
return jni::ToJavaString(env, measurement_utils::FormatSpeed(speed));
auto const units = measurement_utils::GetMeasurementUnits();
return jni::ToJavaString(env, measurement_utils::FormatSpeedNumeric(speed, units) + " " +
platform::GetLocalizedSpeedUnits(units));
}
/*

View file

@ -54,10 +54,9 @@ Java_com_mapswithme_util_StringUtils_nativeFilterContainsNormalized(JNIEnv * env
JNIEXPORT jobject JNICALL Java_com_mapswithme_util_StringUtils_nativeFormatSpeedAndUnits(
JNIEnv * env, jclass thiz, jdouble metersPerSecond)
{
auto units = measurement_utils::Units::Metric;
settings::Get(settings::kMeasurementUnits, units);
auto const units = measurement_utils::GetMeasurementUnits();
return MakeJavaPair(env, measurement_utils::FormatSpeedNumeric(metersPerSecond, units),
measurement_utils::FormatSpeedUnits(units));
platform::GetLocalizedSpeedUnits(units));
}
JNIEXPORT jstring JNICALL

View file

@ -224,14 +224,12 @@ double RulerHelper::CalcMetersDiff(double value)
auto conversionFn = &Identity;
auto units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
if (units == measurement_utils::Units::Imperial)
using namespace measurement_utils;
if (GetMeasurementUnits() == Units::Imperial)
{
arrU = g_arrFeets;
count = ARRAY_SIZE(g_arrFeets);
conversionFn = &measurement_utils::MetersToFeet;
conversionFn = &MetersToFeet;
}
int prevUnitRange = m_rangeIndex;

View file

@ -1699,10 +1699,7 @@ void Framework::SetupMeasurementSystem()
{
GetPlatform().SetupMeasurementSystem();
auto units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
m_routingManager.SetTurnNotificationsUnits(units);
m_routingManager.SetTurnNotificationsUnits(measurement_utils::GetMeasurementUnits());
}
void Framework::SetWidgetLayout(gui::TWidgetsLayoutInfo && layout)

View file

@ -362,10 +362,7 @@ RoutingManager::RoutingManager(Callbacks && callbacks, Delegate & delegate)
return;
double speed = cameraSpeedKmPH;
measurement_utils::Units units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
if (units == measurement_utils::Units::Imperial)
if (measurement_utils::GetMeasurementUnits() == measurement_utils::Units::Imperial)
speed = measurement_utils::KmphToMiph(cameraSpeedKmPH);
mark->SetTitle(strings::to_string(static_cast<int>(speed + 0.5)));

View file

@ -20,7 +20,7 @@ const LocalizedUnits & GetLocalizedUnits(measurement_utils::Units units, Measure
{
static LocalizedUnits UnitsLenghImperial = {GetLocalizedString("foot"), GetLocalizedString("mile")};
static LocalizedUnits UnitsLenghMetric = {GetLocalizedString("meter"), GetLocalizedString("kilometer")};
static LocalizedUnits UnitsSpeedImperial = {GetLocalizedString("foot"), GetLocalizedString("miles_per_hour")};
static LocalizedUnits UnitsSpeedMetric = {GetLocalizedString("meter"), GetLocalizedString("kilometers_per_hour")};
@ -47,18 +47,12 @@ const LocalizedUnits & GetLocalizedUnits(measurement_utils::Units units, Measure
LocalizedUnits GetLocalizedDistanceUnits()
{
auto units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
return GetLocalizedUnits(units, MeasurementType::Distance);
return GetLocalizedUnits(measurement_utils::GetMeasurementUnits(), MeasurementType::Distance);
}
LocalizedUnits GetLocalizedAltitudeUnits()
{
auto units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
return GetLocalizedUnits(units, MeasurementType::Altitude);
return GetLocalizedUnits(measurement_utils::GetMeasurementUnits(), MeasurementType::Altitude);
}
const std::string & GetLocalizedSpeedUnits(measurement_utils::Units units)
@ -68,9 +62,6 @@ const std::string & GetLocalizedSpeedUnits(measurement_utils::Units units)
std::string GetLocalizedSpeedUnits()
{
auto units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
return GetLocalizedSpeedUnits(units);
return GetLocalizedSpeedUnits(measurement_utils::GetMeasurementUnits());
}
} // namespace platform

View file

@ -15,12 +15,12 @@
#include <iomanip>
#include <sstream>
namespace measurement_utils
{
using namespace settings;
using namespace std;
using namespace strings;
namespace measurement_utils
{
namespace
{
string ToStringPrecision(double d, int pr)
@ -73,6 +73,13 @@ std::string DebugPrint(Units units)
UNREACHABLE();
}
Units GetMeasurementUnits()
{
Units units = measurement_utils::Units::Metric;
settings::TryGet(settings::kMeasurementUnits, units);
return units;
}
double ToSpeedKmPH(double speed, Units units)
{
switch (units)
@ -85,9 +92,7 @@ double ToSpeedKmPH(double speed, Units units)
std::string FormatDistanceWithLocalization(double m, OptionalStringRef high, OptionalStringRef low)
{
auto units = Units::Metric;
TryGet(kMeasurementUnits, units);
Units const units = GetMeasurementUnits();
switch (units)
{
case Units::Imperial: return FormatDistanceImpl(units, m, low ? *low : "ft", high ? *high : "mi");
@ -203,9 +208,7 @@ string FormatAltitude(double altitudeInMeters)
string FormatAltitudeWithLocalization(double altitudeInMeters, OptionalStringRef localizedUnits)
{
Units units = Units::Metric;
TryGet(kMeasurementUnits, units);
Units const units = GetMeasurementUnits();
switch (units)
{
case Units::Imperial:
@ -216,14 +219,6 @@ string FormatAltitudeWithLocalization(double altitudeInMeters, OptionalStringRef
UNREACHABLE();
}
string FormatSpeed(double metersPerSecond)
{
auto units = Units::Metric;
TryGet(kMeasurementUnits, units);
return FormatSpeedNumeric(metersPerSecond, units) + " " + FormatSpeedUnits(units);
}
double MpsToUnits(double metersPerSecond, Units units)
{
switch (units)
@ -240,16 +235,6 @@ string FormatSpeedNumeric(double metersPerSecond, Units units)
return ToStringPrecision(unitsPerHour, unitsPerHour >= 10.0 ? 0 : 1);
}
string FormatSpeedUnits(Units units)
{
switch (units)
{
case Units::Imperial: return "mph";
case Units::Metric: return "km/h";
}
UNREACHABLE();
}
string FormatOsmLink(double lat, double lon, int zoom)
{
static constexpr char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~";

View file

@ -2,8 +2,6 @@
#include "geometry/point2d.hpp"
#include "base/assert.hpp"
#include <optional>
#include <string>
@ -19,6 +17,8 @@ enum class Units
std::string DebugPrint(Units units);
Units GetMeasurementUnits();
inline double MetersToMiles(double m) { return m * 0.000621371192; }
inline double MilesToMeters(double mi) { return mi * 1609.344; }
inline double MiphToKmph(double miph) { return MilesToMeters(miph) / 1000.0; }
@ -41,13 +41,11 @@ double MpsToUnits(double mps, Units units);
std::string FormatDistance(double distanceInMeters);
std::string FormatDistanceWithLocalization(double m, OptionalStringRef high, OptionalStringRef low);
/// We always use meters and feet/yards for altitude
/// @return Localized meters or feets string for altitude, depending on current measurement units.
std::string FormatAltitude(double altitudeInMeters);
std::string FormatAltitudeWithLocalization(double altitudeInMeters, OptionalStringRef localizedUnits);
// Return value is measured in km/h for Metric and in mph for Imperial.
std::string FormatSpeed(double metersPerSecond);
/// @return Speed value string (without suffix) in km/h for Metric and in mph for Imperial.
std::string FormatSpeedNumeric(double metersPerSecond, Units units);
std::string FormatSpeedUnits(Units units);
/// @param[in] dac Digits after comma in seconds.
/// Use dac == 3 for our common conversions to DMS.

View file

@ -161,21 +161,6 @@ UNIT_TEST(FormatAltitude)
TEST_EQUAL(FormatAltitude(5), "5 m", ());
}
UNIT_TEST(FormatSpeed)
{
{
ScopedSettings guard(Units::Metric);
TEST_EQUAL(FormatSpeed(10), "36 km/h", ());
TEST_EQUAL(FormatSpeed(1), "3.6 km/h", ());
}
{
ScopedSettings guard(Units::Imperial);
TEST_EQUAL(FormatSpeed(10), "22 mph", ());
TEST_EQUAL(FormatSpeed(1), "2.2 mph", ());
}
}
UNIT_TEST(FormatSpeedNumeric)
{
TEST_EQUAL(FormatSpeedNumeric(10, Units::Metric), "36", ());
@ -185,12 +170,6 @@ UNIT_TEST(FormatSpeedNumeric)
TEST_EQUAL(FormatSpeedNumeric(1, Units::Imperial), "2.2", ());
}
UNIT_TEST(FormatSpeedUnits)
{
TEST_EQUAL(FormatSpeedUnits(Units::Metric), "km/h", ());
TEST_EQUAL(FormatSpeedUnits(Units::Imperial), "mph", ());
}
UNIT_TEST(OSMDistanceToMetersString)
{
TEST_EQUAL(OSMDistanceToMetersString(""), "", ());

View file

@ -8,7 +8,7 @@
namespace settings
{
/// Metric or Feet.
/// Metric or Imperial.
extern char const * kMeasurementUnits;
template <class T>

View file

@ -495,8 +495,11 @@ void DrawWidget::SubmitFakeLocationPoint(m2::PointD const & pt)
}
else
{
LOG(LDEBUG, ("Distance:", loc.m_distToTarget + loc.m_targetUnitsSuffix, "Time:", loc.m_time,
loc.m_speedLimitMps < 0 ? "" : "SpeedLimit: " + measurement_utils::FormatSpeed(loc.m_speedLimitMps),
std::string speed;
if (loc.m_speedLimitMps > 0)
speed = "SpeedLimit: " + measurement_utils::FormatSpeedNumeric(loc.m_speedLimitMps, measurement_utils::Units::Metric);
LOG(LDEBUG, ("Distance:", loc.m_distToTarget + loc.m_targetUnitsSuffix, "Time:", loc.m_time, speed,
GetTurnString(loc.m_turn), (loc.m_exitNum != 0 ? ":" + std::to_string(loc.m_exitNum) : ""),
"in", loc.m_distToTurn + loc.m_turnUnitsSuffix,
loc.m_targetName.empty() ? "" : "to " + loc.m_targetName ));