Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/unreleased/bugfixes/6789.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Made default rounding increment in `DistanceFormatterOptions` dependent on distance numerical value.
3 changes: 2 additions & 1 deletion libnavigation-base/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ package com.mapbox.navigation.base.formatter {
field public static final int INCREMENT_ONE_HUNDRED = 100; // 0x64
field public static final int INCREMENT_TEN = 10; // 0xa
field public static final int INCREMENT_TWENTY_FIVE = 25; // 0x19
field public static final int INCREMENT_UNDEFINED = -1; // 0xffffffff
field public static final com.mapbox.navigation.base.formatter.Rounding INSTANCE;
}

@IntDef({com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TEN, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TWENTY_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIFTY, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_ONE_HUNDRED}) @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.BINARY) public static @interface Rounding.Increment {
@IntDef({com.mapbox.navigation.base.formatter.Rounding.INCREMENT_UNDEFINED, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TEN, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_TWENTY_FIVE, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_FIFTY, com.mapbox.navigation.base.formatter.Rounding.INCREMENT_ONE_HUNDRED}) @kotlin.annotation.Retention(kotlin.annotation.AnnotationRetention.BINARY) public static @interface Rounding.Increment {
}

public enum UnitType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DistanceFormatterOptions private constructor(
private val applicationContext: Context = applicationContext.applicationContext
private var locale: Locale = applicationContext.inferDeviceLocale()
private var unitType: UnitType? = null
private var roundingIncrement = Rounding.INCREMENT_FIFTY
private var roundingIncrement = Rounding.INCREMENT_UNDEFINED

/**
* Policy for the various units of measurement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import androidx.annotation.IntDef
* Rounding
*/
object Rounding {

/**
* Undefined rounding increment.
*/
const val INCREMENT_UNDEFINED = -1

/**
* Rounding increment 5
*
Expand Down Expand Up @@ -46,6 +52,7 @@ object Rounding {
*/
@Retention(AnnotationRetention.BINARY)
@IntDef(
INCREMENT_UNDEFINED,
INCREMENT_FIVE,
INCREMENT_TEN,
INCREMENT_TWENTY_FIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mapbox.navigation.core.formatter
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import com.mapbox.navigation.base.formatter.Rounding
import com.mapbox.navigation.base.formatter.UnitType
import com.mapbox.navigation.core.R
import com.mapbox.turf.TurfConstants
Expand All @@ -16,6 +17,7 @@ import kotlin.math.roundToInt
*/
object MapboxDistanceUtil {

private const val INVALID_ROUNDING_INCREMENT = 50
private val enLanguage = Locale("en").language

/**
Expand Down Expand Up @@ -84,12 +86,28 @@ object MapboxDistanceUtil {
distanceInMeters !in 0.0..Double.MAX_VALUE -> smallValue(
0.0,
roundingIncrement,
INVALID_ROUNDING_INCREMENT,
TurfConstants.UNIT_METERS,
UnitType.METRIC
)
distanceInMeters < 25.0 -> smallValue(
distanceInMeters,
roundingIncrement,
5,
TurfConstants.UNIT_METERS,
UnitType.METRIC
)
distanceInMeters < 100 -> smallValue(
distanceInMeters,
roundingIncrement,
25,
TurfConstants.UNIT_METERS,
UnitType.METRIC
)
distanceInMeters < 1000.0 -> smallValue(
distanceInMeters,
roundingIncrement,
50,
TurfConstants.UNIT_METERS,
UnitType.METRIC
)
Expand Down Expand Up @@ -128,6 +146,7 @@ object MapboxDistanceUtil {
distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue(
0.0,
roundingIncrement,
INVALID_ROUNDING_INCREMENT,
TurfConstants.UNIT_YARDS,
UnitType.IMPERIAL
)
Expand All @@ -137,12 +156,29 @@ object MapboxDistanceUtil {
TurfConstants.UNIT_MILES,
TurfConstants.UNIT_YARDS
)
smallValue(
distanceInYards,
roundingIncrement,
TurfConstants.UNIT_YARDS,
UnitType.IMPERIAL
)
when {
distanceInYards < 20 -> smallValue(
distanceInYards,
roundingIncrement,
10,
TurfConstants.UNIT_YARDS,
UnitType.IMPERIAL
)
distanceInYards < 100 -> smallValue(
distanceInYards,
roundingIncrement,
25,
TurfConstants.UNIT_YARDS,
UnitType.IMPERIAL
)
else -> smallValue(
distanceInYards,
roundingIncrement,
50,
TurfConstants.UNIT_YARDS,
UnitType.IMPERIAL
)
}
}
distanceInMiles < 3.0 -> largeValue(
distanceInMiles,
Expand Down Expand Up @@ -170,6 +206,7 @@ object MapboxDistanceUtil {
distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue(
0.0,
roundingIncrement,
INVALID_ROUNDING_INCREMENT,
TurfConstants.UNIT_FEET,
UnitType.IMPERIAL
)
Expand All @@ -182,6 +219,7 @@ object MapboxDistanceUtil {
smallValue(
distanceInFeet,
roundingIncrement,
50,
TurfConstants.UNIT_FEET,
UnitType.IMPERIAL
)
Expand All @@ -206,12 +244,18 @@ object MapboxDistanceUtil {
private fun smallValue(
distance: Double,
roundingIncrement: Int,
defaultRoundingIncrement: Int,
unitTypeString: String,
unitType: UnitType
): FormattingData {
val inferredRoundingIncrement = if (roundingIncrement == Rounding.INCREMENT_UNDEFINED) {
defaultRoundingIncrement
} else {
roundingIncrement
}
val roundedValue = roundSmallDistance(
distance,
roundingIncrement,
inferredRoundingIncrement,
)
return FormattingData(
roundedValue.toDouble(),
Expand Down
Loading