mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-10-06 03:38:23 +00:00
fix: guard ICU getMeasurementSystem behind API 28 check
LocaleData.getMeasurementSystem requires API level 28 but the module's minSdk is 26, which lint (NewApi) flagged as an error. Guard the ICU call behind a Build.VERSION.SDK_INT check and fall back to a country-code based heuristic on API 26-27. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JDcx7i1VDRyT7rDcuSB5Co
This commit is contained in:
+13
-4
@@ -22,8 +22,12 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.workouts
|
||||
|
||||
import android.icu.util.LocaleData
|
||||
import android.icu.util.ULocale
|
||||
import android.os.Build
|
||||
import java.util.Locale
|
||||
|
||||
/** Countries that use miles for road distance (imperial / US customary). */
|
||||
private val MILES_COUNTRIES = setOf("US", "GB", "LR", "MM")
|
||||
|
||||
/**
|
||||
* Whether the phone's measurement preference favours miles for distance.
|
||||
*
|
||||
@@ -31,7 +35,8 @@ import java.util.Locale
|
||||
* which the platform surfaces through the default locale's Unicode `-u-ms-`
|
||||
* extension (`metric` / `ussystem` / `uksystem`). When no explicit override is
|
||||
* set it falls back to ICU's locale-derived measurement system (US and UK both
|
||||
* use miles for distance), available since API 24.
|
||||
* use miles for distance), available since API 28; on older releases it falls
|
||||
* back to a country-code check.
|
||||
*/
|
||||
fun phonePrefersMiles(): Boolean {
|
||||
val locale = Locale.getDefault(Locale.Category.FORMAT)
|
||||
@@ -40,8 +45,12 @@ fun phonePrefersMiles(): Boolean {
|
||||
return ms == "ussystem" || ms == "uksystem"
|
||||
}
|
||||
|
||||
return when (LocaleData.getMeasurementSystem(ULocale.forLocale(locale))) {
|
||||
LocaleData.MeasurementSystem.US, LocaleData.MeasurementSystem.UK -> true
|
||||
else -> false
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
when (LocaleData.getMeasurementSystem(ULocale.forLocale(locale))) {
|
||||
LocaleData.MeasurementSystem.US, LocaleData.MeasurementSystem.UK -> true
|
||||
else -> false
|
||||
}
|
||||
} else {
|
||||
locale.country.uppercase(Locale.ROOT) in MILES_COUNTRIES
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user