Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale distance formatter #668

Merged
merged 32 commits into from
Feb 7, 2018
Merged

Conversation

devotaaabel
Copy link
Contributor

@devotaaabel devotaaabel commented Jan 25, 2018

Refactored the distance formatter for simplicity, and also added locale to options, so that the numbers can be formatted properly for the given locale. I also added a test to verify that the refactored code behavior matched the previous behavior, and to test the locale formatting. Unit type is still specified in the options separately. We could potentially use the locale to pick the unit type if no unit type is specified.

Output from tests:

Meters Locale Formatted String
18124.65 en_US 11 mi
18124.65 fr_FR 18 km
109.73 en_US 350 ft
109.73 fr_FR 100 m
9812.33 fr_FR 9,8 km
9812.33 en_US 6.1 mi
13.71 fr_FR 50 m
13.71 en_US 50 ft

UNIT_STRINGS.put(UNIT_KILOMETERS, "km");
UNIT_STRINGS.put(UNIT_METERS, "m");
UNIT_STRINGS.put(UNIT_MILES, "mi");
UNIT_STRINGS.put(UNIT_FEET, "ft");
Copy link
Contributor

@ericrwolfe ericrwolfe Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that we'll eventually need to localize these for certain languages, so if we can make the "km", "m", "mi", "ft" here easily localizable then 👍:
1,2 km (fr-FR) = 1.2公里 (zh-CN) = 1,2 км (ru-RU)

That being said, the only languages on this list (https://www.mapbox.com/api-documentation/#instructions-languages) that don't support the above hardcoded unit strings would be Chinese and Russian, so this isn't a priority we need to worry too much about right now if it turns out to be a lot of work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericrwolfe good catch, should be easy.

Devota Aabel added 2 commits January 25, 2018 14:45
… made DistanceUtils instantiable so that the user doesn't have to pass in fields every time
Copy link
Contributor

@danesfeder danesfeder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great so far. ❤️ the tests you added. Let's run with your idea of using SharedPreferences for the locale value. This way devs can override it on their side if they need to for whatever reason

@@ -115,6 +121,7 @@ public static Builder builder() {
.enableNotification(true)
.isFromNavigationUi(false)
.isDebugLoggingEnabled(false)
.unitType(NavigationUnitType.TYPE_IMPERIAL);
.unitType(NavigationUnitType.TYPE_IMPERIAL)
.locale(Locale.US);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use Locale.getDefault() for the builder() here

@@ -108,6 +108,7 @@ public InstructionView(Context context, @Nullable AttributeSet attrs) {

public InstructionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
locale = context.getResources().getConfiguration().locale;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with extraction from SharedPreferences

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danesfeder I was thinking just pass the context to DistanceUtils, and then DistanceUtils can get the locale from SharedPreferences

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devotaaabel yeah that sounds good to me - just need to make sure we aren't calling DistanceUtils somewhere where we don't have Context. Or have a backup method that can be called without context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danesfeder To localize the strings like Eric said we'll need the context, but it shouldn't be an issue anyway

@@ -51,6 +49,7 @@ public SummaryBottomSheet(Context context, AttributeSet attrs) {

public SummaryBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
locale = context.getResources().getConfiguration().locale;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with the SharedPref deal

@@ -118,6 +118,7 @@ private void extractConfiguration(NavigationViewOptions.Builder options) {
MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder()
.unitType(preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE,
NavigationUnitType.TYPE_IMPERIAL))
.locale(getResources().getConfiguration().locale)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call here using the configuration locale here instead of Locale.getDefault()

@@ -118,6 +118,7 @@ private void extractConfiguration(NavigationViewOptions.Builder options) {
MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder()
.unitType(preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE,
NavigationUnitType.TYPE_IMPERIAL))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be cool to get a default unit type based on the local here (if one isn't set in the preferences) with something like https://stackoverflow.com/questions/4898237/using-locale-settings-to-detect-wheter-to-use-imperial-units.

Current catch is the unit type isn't currently nullable in MapboxNavigationOptions, so we can switch that to @Nullable and if someone doesn't set it, we dont set the preference and get the default here when setting the default value for the SharedPreference

Copy link
Contributor

@danesfeder danesfeder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just a few final nit-picks then feel free to merge so you are unblocked for the voice instruction locale PR 🚀

editor.putString(NavigationConstants.NAVIGATION_VIEW_LOCALE_COUNTRY, options.locale().getCountry());
}

editor.apply();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract the above lines 217-225 into a private method for readability

stepList = new ArrayList<>();
decimalFormat = new DecimalFormat(NavigationConstants.DECIMAL_FORMAT);
this.context = context;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than store Context here as a field, would it be okay to create the instance of DistanceUtils here with the Context and assign that to a field? Context can cause leaks sometimes when storing it like this.

return unitType;
}

// If unit type isn't specified, use default for locale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@devotaaabel devotaaabel merged commit 33ff396 into master Feb 7, 2018
@devotaaabel devotaaabel deleted the devota-locale-distance-formatter branch February 7, 2018 23:22
@devotaaabel devotaaabel restored the devota-locale-distance-formatter branch February 8, 2018 15:34
@danesfeder danesfeder mentioned this pull request Feb 26, 2018
13 tasks
@danesfeder danesfeder deleted the devota-locale-distance-formatter branch February 28, 2018 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants