Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 288ca0c

Browse files
committedJan 5, 2023
NAVAND-979: use distance-dependent default rounding increments
1 parent e361c64 commit 288ca0c

File tree

6 files changed

+518
-10
lines changed

6 files changed

+518
-10
lines changed
 

‎changelog/unreleased/bugfixes/dd2.md

+1
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

+2-1
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

+1-1
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

+7
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

+49-7
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,30 @@ 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+
}
182+
146183
}
147184
distanceInMiles < 3.0 -> largeValue(
148185
distanceInMiles,
@@ -170,6 +207,7 @@ object MapboxDistanceUtil {
170207
distanceInMiles !in 0.0..Double.MAX_VALUE -> smallValue(
171208
0.0,
172209
roundingIncrement,
210+
INVALID_ROUNDING_INCREMENT,
173211
TurfConstants.UNIT_FEET,
174212
UnitType.IMPERIAL
175213
)
@@ -182,6 +220,7 @@ object MapboxDistanceUtil {
182220
smallValue(
183221
distanceInFeet,
184222
roundingIncrement,
223+
50,
185224
TurfConstants.UNIT_FEET,
186225
UnitType.IMPERIAL
187226
)
@@ -206,12 +245,15 @@ object MapboxDistanceUtil {
206245
private fun smallValue(
207246
distance: Double,
208247
roundingIncrement: Int,
248+
defaultRoundingIncrement: Int,
209249
unitTypeString: String,
210250
unitType: UnitType
211251
): FormattingData {
252+
val inferredRoundingIncrement = if (roundingIncrement == Rounding.INCREMENT_UNDEFINED)
253+
defaultRoundingIncrement else roundingIncrement
212254
val roundedValue = roundSmallDistance(
213255
distance,
214-
roundingIncrement,
256+
inferredRoundingIncrement,
215257
)
216258
return FormattingData(
217259
roundedValue.toDouble(),

‎libnavigation-core/src/test/java/com/mapbox/navigation/core/formatter/MapboxDistanceUtilTest.kt

+458-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.