Skip to content

Commit e50af0f

Browse files
committed
NAVAND-979: use distance-dependent default rounding increments
1 parent 662c318 commit e50af0f

File tree

6 files changed

+520
-10
lines changed

6 files changed

+520
-10
lines changed

changelog/unreleased/bugfixes/dd2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Made default rounding increment in `DistanceFormatterOptions` dependent on distance numerical value.

libnavigation-base/api/current.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ package com.mapbox.navigation.base.formatter {
6363
field public static final int INCREMENT_ONE_HUNDRED = 100; // 0x64
6464
field public static final int INCREMENT_TEN = 10; // 0xa
6565
field public static final int INCREMENT_TWENTY_FIVE = 25; // 0x19
66+
field public static final int INCREMENT_UNDEFINED = -1; // 0xffffffff
6667
field public static final com.mapbox.navigation.base.formatter.Rounding INSTANCE;
6768
}
6869

69-
@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 {
70+
@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 {
7071
}
7172

7273
public enum UnitType {

libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/DistanceFormatterOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class DistanceFormatterOptions private constructor(
7878
private val applicationContext: Context = applicationContext.applicationContext
7979
private var locale: Locale = applicationContext.inferDeviceLocale()
8080
private var unitType: UnitType? = null
81-
private var roundingIncrement = Rounding.INCREMENT_FIFTY
81+
private var roundingIncrement = Rounding.INCREMENT_UNDEFINED
8282

8383
/**
8484
* Policy for the various units of measurement.

libnavigation-base/src/main/java/com/mapbox/navigation/base/formatter/Rounding.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import androidx.annotation.IntDef
66
* Rounding
77
*/
88
object Rounding {
9+
10+
/**
11+
* Undefined rounding increment.
12+
*/
13+
const val INCREMENT_UNDEFINED = -1
14+
915
/**
1016
* Rounding increment 5
1117
*
@@ -46,6 +52,7 @@ object Rounding {
4652
*/
4753
@Retention(AnnotationRetention.BINARY)
4854
@IntDef(
55+
INCREMENT_UNDEFINED,
4956
INCREMENT_FIVE,
5057
INCREMENT_TEN,
5158
INCREMENT_TWENTY_FIVE,

libnavigation-core/src/main/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtil.kt

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.mapbox.navigation.core.formatter
33
import android.content.Context
44
import android.content.res.Configuration
55
import android.content.res.Resources
6+
import com.mapbox.navigation.base.formatter.Rounding
67
import com.mapbox.navigation.base.formatter.UnitType
78
import com.mapbox.navigation.core.R
89
import com.mapbox.turf.TurfConstants
@@ -16,6 +17,7 @@ import kotlin.math.roundToInt
1617
*/
1718
object MapboxDistanceUtil {
1819

20+
private const val INVALID_ROUNDING_INCREMENT = 50
1921
private val enLanguage = Locale("en").language
2022

2123
/**
@@ -84,12 +86,28 @@ object MapboxDistanceUtil {
8486
distanceInMeters !in 0.0..Double.MAX_VALUE -> smallValue(
8587
0.0,
8688
roundingIncrement,
89+
INVALID_ROUNDING_INCREMENT,
90+
TurfConstants.UNIT_METERS,
91+
UnitType.METRIC
92+
)
93+
distanceInMeters < 25.0 -> smallValue(
94+
distanceInMeters,
95+
roundingIncrement,
96+
5,
97+
TurfConstants.UNIT_METERS,
98+
UnitType.METRIC
99+
)
100+
distanceInMeters < 100 -> smallValue(
101+
distanceInMeters,
102+
roundingIncrement,
103+
25,
87104
TurfConstants.UNIT_METERS,
88105
UnitType.METRIC
89106
)
90107
distanceInMeters < 1000.0 -> smallValue(
91108
distanceInMeters,
92109
roundingIncrement,
110+
50,
93111
TurfConstants.UNIT_METERS,
94112
UnitType.METRIC
95113
)
@@ -128,6 +146,7 @@ object MapboxDistanceUtil {
128146
distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue(
129147
0.0,
130148
roundingIncrement,
149+
INVALID_ROUNDING_INCREMENT,
131150
TurfConstants.UNIT_YARDS,
132151
UnitType.IMPERIAL
133152
)
@@ -137,12 +156,29 @@ object MapboxDistanceUtil {
137156
TurfConstants.UNIT_MILES,
138157
TurfConstants.UNIT_YARDS
139158
)
140-
smallValue(
141-
distanceInYards,
142-
roundingIncrement,
143-
TurfConstants.UNIT_YARDS,
144-
UnitType.IMPERIAL
145-
)
159+
when {
160+
distanceInYards < 20 -> smallValue(
161+
distanceInYards,
162+
roundingIncrement,
163+
10,
164+
TurfConstants.UNIT_YARDS,
165+
UnitType.IMPERIAL
166+
)
167+
distanceInYards < 100 -> smallValue(
168+
distanceInYards,
169+
roundingIncrement,
170+
25,
171+
TurfConstants.UNIT_YARDS,
172+
UnitType.IMPERIAL
173+
)
174+
else -> smallValue(
175+
distanceInYards,
176+
roundingIncrement,
177+
50,
178+
TurfConstants.UNIT_YARDS,
179+
UnitType.IMPERIAL
180+
)
181+
}
146182
}
147183
distanceInMiles < 3.0 -> largeValue(
148184
distanceInMiles,
@@ -170,6 +206,7 @@ object MapboxDistanceUtil {
170206
distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue(
171207
0.0,
172208
roundingIncrement,
209+
INVALID_ROUNDING_INCREMENT,
173210
TurfConstants.UNIT_FEET,
174211
UnitType.IMPERIAL
175212
)
@@ -182,6 +219,7 @@ object MapboxDistanceUtil {
182219
smallValue(
183220
distanceInFeet,
184221
roundingIncrement,
222+
50,
185223
TurfConstants.UNIT_FEET,
186224
UnitType.IMPERIAL
187225
)
@@ -206,12 +244,18 @@ object MapboxDistanceUtil {
206244
private fun smallValue(
207245
distance: Double,
208246
roundingIncrement: Int,
247+
defaultRoundingIncrement: Int,
209248
unitTypeString: String,
210249
unitType: UnitType
211250
): FormattingData {
251+
val inferredRoundingIncrement = if (roundingIncrement == Rounding.INCREMENT_UNDEFINED) {
252+
defaultRoundingIncrement
253+
} else {
254+
roundingIncrement
255+
}
212256
val roundedValue = roundSmallDistance(
213257
distance,
214-
roundingIncrement,
258+
inferredRoundingIncrement,
215259
)
216260
return FormattingData(
217261
roundedValue.toDouble(),

0 commit comments

Comments
 (0)