-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get primary measure from android locale apis
- Loading branch information
Showing
8 changed files
with
117 additions
and
76 deletions.
There are no files selected for viewing
17 changes: 16 additions & 1 deletion
17
...se-material3/src/androidMain/kotlin/dev/sargunv/maplibrecompose/material3/util.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
package dev.sargunv.maplibrecompose.material3 | ||
|
||
import android.icu.util.LocaleData | ||
import android.icu.util.ULocale | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure | ||
|
||
internal actual fun scaleBarMeasurePreference(): ScaleBarMeasure? = null | ||
@Composable | ||
internal actual fun systemDefaultPrimaryMeasure(): ScaleBarMeasure? { | ||
if (android.os.Build.VERSION.SDK_INT < 28) return null | ||
val locales = LocalContext.current.resources.configuration.locales | ||
if (locales.isEmpty) return null | ||
return when (LocaleData.getMeasurementSystem(ULocale.forLocale(locales[0]))) { | ||
LocaleData.MeasurementSystem.SI -> ScaleBarMeasure.Metric | ||
LocaleData.MeasurementSystem.US -> ScaleBarMeasure.FeetAndMiles | ||
LocaleData.MeasurementSystem.UK -> ScaleBarMeasure.YardsAndMiles | ||
else -> null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 79 additions & 58 deletions
137
...bre-compose-material3/src/commonMain/kotlin/dev/sargunv/maplibrecompose/material3/util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,91 @@ | ||
package dev.sargunv.maplibrecompose.material3 | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.text.intl.Locale | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure.* | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure.FeetAndMiles | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure.Metric | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure.YardsAndMiles | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasures | ||
|
||
/** user system preference for the scale bar measure, if any */ | ||
internal expect fun scaleBarMeasurePreference(): ScaleBarMeasure? | ||
/** use system locale APIs for the primary scale bar measure */ | ||
@Composable internal expect fun systemDefaultPrimaryMeasure(): ScaleBarMeasure? | ||
|
||
/** | ||
* default scale bar measures to use, depending on the user's locale (or system preferences, if | ||
* available) | ||
*/ | ||
internal fun defaultScaleBarMeasures(): ScaleBarMeasures = | ||
scaleBarMeasurePreference()?.let { ScaleBarMeasures(it) } | ||
?: defaultScaleBarMeasures(Locale.current.region) | ||
|
||
/** default scale bar measure to use, depending on the locale */ | ||
internal fun defaultScaleBarMeasures(country: String?): ScaleBarMeasures { | ||
if (!country.isNullOrEmpty()) { | ||
return when (country) { | ||
// United states and its unincorporated territories | ||
"US" -> ScaleBarMeasures(FeetAndMiles, Metric) | ||
"AS", | ||
"GU", | ||
"MP", | ||
"PR", | ||
"VI" -> ScaleBarMeasures(FeetAndMiles, Metric) | ||
|
||
// former United states territories / Compact of Free Association | ||
"FM", | ||
"MH", | ||
"PW" -> ScaleBarMeasures(Metric, FeetAndMiles) | ||
/** if the system APIs don't provide a primary measure, fall back to our hardcoded lists */ | ||
internal fun fallbackDefaultPrimaryMeasure(region: String?): ScaleBarMeasure = | ||
when (region) { | ||
in regionsUsingFeetAndMiles -> FeetAndMiles | ||
in regionsUsingYardsAndMiles -> YardsAndMiles | ||
else -> Metric | ||
} | ||
|
||
// United kingdom with its overseas territories and crown dependencies | ||
"GB" -> ScaleBarMeasures(YardsAndMiles, Metric) | ||
"AI", | ||
"BM", | ||
"FK", | ||
"GG", | ||
"GI", | ||
"GS", | ||
"IM", | ||
"IO", | ||
"JE", | ||
"KY", | ||
"MS", | ||
"PN", | ||
"SH", | ||
"TC", | ||
"VG" -> ScaleBarMeasures(YardsAndMiles, Metric) | ||
/** countries using non-metric units will see both systems by default */ | ||
internal fun defaultSecondaryMeasure(primary: ScaleBarMeasure, region: String?): ScaleBarMeasure? = | ||
when (primary) { | ||
FeetAndMiles -> Metric | ||
YardsAndMiles -> Metric | ||
Metric -> | ||
when (region) { | ||
in regionsUsingFeetAndMiles -> FeetAndMiles | ||
in regionsUsingYardsAndMiles -> YardsAndMiles | ||
else -> null | ||
} | ||
else -> null // should never happen because the primary is always one of the above | ||
} | ||
|
||
// former British overseas territories / colonies | ||
"BS", | ||
"BZ", | ||
"GD", | ||
"KN", | ||
"VC" -> ScaleBarMeasures(Metric, YardsAndMiles) | ||
internal val regionsUsingFeetAndMiles = | ||
listOf( | ||
// United states and its unincorporated territories | ||
"US", | ||
"AS", | ||
"GU", | ||
"MP", | ||
"PR", | ||
"VI", | ||
// former United states territories / Compact of Free Association | ||
"FM", | ||
"MH", | ||
"PW", | ||
// Liberia | ||
"LR", | ||
) | ||
|
||
// Myanmar | ||
"MM" -> ScaleBarMeasures(Metric, YardsAndMiles) | ||
// Liberia | ||
"LR" -> ScaleBarMeasures(Metric, FeetAndMiles) | ||
internal val regionsUsingYardsAndMiles = | ||
listOf( | ||
// United kingdom with its overseas territories and crown dependencies | ||
"GB", | ||
"AI", | ||
"BM", | ||
"FK", | ||
"GG", | ||
"GI", | ||
"GS", | ||
"IM", | ||
"IO", | ||
"JE", | ||
"KY", | ||
"MS", | ||
"PN", | ||
"SH", | ||
"TC", | ||
"VG", | ||
// former British overseas territories / colonies | ||
"BS", | ||
"BZ", | ||
"GD", | ||
"KN", | ||
"VC", | ||
// Myanmar | ||
"MM", | ||
) | ||
|
||
else -> ScaleBarMeasures(Metric) | ||
} | ||
} | ||
return ScaleBarMeasures(Metric) | ||
/** | ||
* default scale bar measures to use, depending on the user's locale (or system preferences, if | ||
* available) | ||
*/ | ||
@Composable | ||
internal fun defaultScaleBarMeasures(): ScaleBarMeasures { | ||
val region = Locale.current.region | ||
val primary = systemDefaultPrimaryMeasure() ?: fallbackDefaultPrimaryMeasure(region) | ||
return ScaleBarMeasures(primary = primary, secondary = defaultSecondaryMeasure(primary, region)) | ||
} |
3 changes: 2 additions & 1 deletion
3
...se-material3/src/desktopMain/kotlin/dev/sargunv/maplibrecompose/material3/util.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package dev.sargunv.maplibrecompose.material3 | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure | ||
|
||
internal actual fun scaleBarMeasurePreference(): ScaleBarMeasure? = null | ||
@Composable internal actual fun systemDefaultPrimaryMeasure(): ScaleBarMeasure? = null | ||
// TODO on macOS, there should be an API for this |
3 changes: 2 additions & 1 deletion
3
...re-compose-material3/src/iosMain/kotlin/dev/sargunv/maplibrecompose/material3/util.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ibre-compose-material3/src/jsMain/kotlin/dev/sargunv/maplibrecompose/material3/util.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package dev.sargunv.maplibrecompose.material3 | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.sargunv.maplibrecompose.material3.controls.ScaleBarMeasure | ||
|
||
internal actual fun scaleBarMeasurePreference(): ScaleBarMeasure? = null | ||
@Composable internal actual fun systemDefaultPrimaryMeasure(): ScaleBarMeasure? = null |