+18
-3
@@ -25,8 +25,23 @@ internal class MapTilerTileServerStyleUriBuilder(
|
||||
darkMapId = BuildConfig.MAPTILER_DARK_MAP_ID,
|
||||
)
|
||||
|
||||
override fun build(darkMode: Boolean): String {
|
||||
val mapId = if (darkMode) darkMapId else lightMapId
|
||||
return "$baseUrl/$mapId/style.json?key=$apiKey"
|
||||
override fun build(
|
||||
customMapStyleUrl: String?,
|
||||
darkMode: Boolean,
|
||||
): String {
|
||||
return buildString {
|
||||
if (customMapStyleUrl.isNullOrBlank()) {
|
||||
val mapId = if (darkMode) darkMapId else lightMapId
|
||||
append(baseUrl)
|
||||
append("/")
|
||||
append(mapId)
|
||||
append("/")
|
||||
append("style.json")
|
||||
} else {
|
||||
append(customMapStyleUrl.removeSuffix("/"))
|
||||
}
|
||||
append("?key=")
|
||||
append(apiKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-5
@@ -19,21 +19,25 @@ import io.element.android.compound.theme.ElementTheme
|
||||
*/
|
||||
interface TileServerStyleUriBuilder {
|
||||
fun build(
|
||||
customMapStyleUrl: String?,
|
||||
darkMode: Boolean,
|
||||
): String
|
||||
}
|
||||
|
||||
fun TileServerStyleUriBuilder(): TileServerStyleUriBuilder = MapTilerTileServerStyleUriBuilder()
|
||||
|
||||
/**
|
||||
* Provides and remembers a style URI for a MapLibre compatible tile server.
|
||||
*
|
||||
* Used for rendering dynamic maps.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberTileStyleUrl(): String {
|
||||
fun rememberTileStyleUrl(
|
||||
customMapStyleUrl: String?,
|
||||
): String {
|
||||
val darkMode = !ElementTheme.isLightTheme
|
||||
return remember(darkMode) {
|
||||
TileServerStyleUriBuilder().build(darkMode)
|
||||
return remember(darkMode, customMapStyleUrl) {
|
||||
MapTilerTileServerStyleUriBuilder().build(
|
||||
customMapStyleUrl = customMapStyleUrl,
|
||||
darkMode = darkMode,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+28
-2
@@ -22,14 +22,40 @@ class MapTilerTileServerStyleUriBuilderTest {
|
||||
@Test
|
||||
fun `light map uri`() {
|
||||
assertThat(
|
||||
builder.build(darkMode = false)
|
||||
builder.build(
|
||||
customMapStyleUrl = null,
|
||||
darkMode = false,
|
||||
)
|
||||
).isEqualTo("https://base.url/aLightMapId/style.json?key=anApiKey")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `dark map uri`() {
|
||||
assertThat(
|
||||
builder.build(darkMode = true)
|
||||
builder.build(
|
||||
customMapStyleUrl = null,
|
||||
darkMode = true,
|
||||
)
|
||||
).isEqualTo("https://base.url/aDarkMapId/style.json?key=anApiKey")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `custom map uri light`() {
|
||||
assertThat(
|
||||
builder.build(
|
||||
customMapStyleUrl = "https://custom.url/style.json",
|
||||
darkMode = false,
|
||||
)
|
||||
).isEqualTo("https://custom.url/style.json?key=anApiKey")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `custom map uri dark`() {
|
||||
assertThat(
|
||||
builder.build(
|
||||
customMapStyleUrl = "https://custom.url/style.json",
|
||||
darkMode = true,
|
||||
)
|
||||
).isEqualTo("https://custom.url/style.json?key=anApiKey")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user