Skip to content

Commit

Permalink
feat!: added MapColorScheme (#591)
Browse files Browse the repository at this point in the history
* feat: added MapColorScheme

* doc: javadoc

* chore: removed unused dependency

* feat: libraries updated

* feat: added tests

* feat: added MapColorScheme as default values

* chore: scheme for everybody

* chore: renamed tests
  • Loading branch information
kikoso authored Jul 2, 2024
1 parent f4c97fe commit 569ef39
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 30 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildFeatures {
Expand All @@ -39,7 +39,7 @@ android {
}

kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@ class GoogleMapViewTests {
private val startingZoom = 10f
private val startingPosition = LatLng(1.23, 4.56)
private lateinit var cameraPositionState: CameraPositionState
private var mapColorScheme = ComposeMapColorScheme.FOLLOW_SYSTEM

private fun initMap(content: @Composable () -> Unit = {}) {
check(hasValidApiKey) { "Maps API key not specified" }
Expand All @@ -50,7 +51,8 @@ class GoogleMapViewTests {
cameraPositionState = cameraPositionState,
onMapLoaded = {
countDownLatch.countDown()
}
},
mapColorScheme = mapColorScheme
) {
content.invoke()
}
Expand All @@ -75,6 +77,19 @@ class GoogleMapViewTests {
startingPosition.assertEquals(cameraPositionState.position.target)
}

@Test
fun testRightInitialColorScheme() {
initMap()
mapColorScheme.assertEquals(ComposeMapColorScheme.FOLLOW_SYSTEM)
}

@Test
fun testRighColorSchemeAfterChangingIt() {
mapColorScheme = ComposeMapColorScheme.DARK
initMap()
mapColorScheme.assertEquals(ComposeMapColorScheme.DARK)
}

@Test
fun testCameraReportsMoving() {
initMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.google.maps.android.compose

import com.google.android.gms.maps.model.LatLng
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
const val timeout2 = 2_000L
const val timeout3 = 3_000L
const val timeout5 = 5_000L
Expand All @@ -17,7 +16,7 @@ fun LatLng.assertEquals(other: LatLng) {
assertEquals(longitude, other.longitude, assertRoundingError)
}

fun LatLng.assertNotEquals(other: LatLng) {
assertNotEquals(latitude, other.latitude, assertRoundingError)
assertNotEquals(longitude, other.longitude, assertRoundingError)

fun ComposeMapColorScheme.assertEquals(other: ComposeMapColorScheme) {
assertEquals(other, this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ fun GoogleMapView(
modifier: Modifier = Modifier,
cameraPositionState: CameraPositionState = rememberCameraPositionState(),
onMapLoaded: () -> Unit = {},
mapColorScheme: ComposeMapColorScheme = ComposeMapColorScheme.FOLLOW_SYSTEM,
content: @Composable () -> Unit = {}
) {
val singaporeState = rememberMarkerState(position = singapore)
Expand All @@ -159,6 +160,8 @@ fun GoogleMapView(
}
var mapVisible by remember { mutableStateOf(true) }

var darkMode by remember { mutableStateOf(mapColorScheme) }

if (mapVisible) {
GoogleMap(
modifier = modifier,
Expand All @@ -168,7 +171,8 @@ fun GoogleMapView(
onMapLoaded = onMapLoaded,
onPOIClick = {
Log.d(TAG, "POI clicked: ${it.name}")
}
},
mapColorScheme = darkMode
) {
// Drawing on the map is accomplished with a child-based API
val markerClick: (Marker) -> Boolean = {
Expand Down Expand Up @@ -267,6 +271,18 @@ fun GoogleMapView(
modifier = Modifier
.testTag("toggleMapVisibility")
)
MapButton(
text = "Toggle Dark Mode",
onClick = {
darkMode =
if (darkMode == ComposeMapColorScheme.DARK)
ComposeMapColorScheme.LIGHT
else
ComposeMapColorScheme.DARK
},
modifier = Modifier
.testTag("toggleDarkMode")
)
}
val coroutineScope = rememberCoroutineScope()
ZoomControls(
Expand Down Expand Up @@ -312,7 +328,7 @@ private fun MapTypeControls(
.horizontalScroll(state = ScrollState(0)),
horizontalArrangement = Arrangement.Center
) {
MapType.values().forEach {
MapType.entries.forEach {
MapTypeButton(type = it) { onMapTypeClick(it) }
}
}
Expand Down
14 changes: 7 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[versions]
activitycompose = "1.9.0"
agp = "8.4.2"
androidxtest = "1.5.0"
compose-bom = "2024.05.00"
androidxtest = "1.6.1"
compose-bom = "2024.06.00"
coroutines = "1.7.3"
dokka = "1.9.20"
espresso = "3.5.1"
espresso = "3.6.1"
jacoco-plugin = "0.2.1"
junitktx = "1.1.5"
junitktx = "1.2.1"
junit = "4.13.2"
kotlin = "2.0.0"
material = "1.12.0"
mapsktx = "5.0.0"
mapsktx = "5.1.0"
mapsecrets = "2.0.1"
org-jacoco-core = "0.8.7"
android-core = "1.13.1"
org-jacoco-core = "0.8.11"


[libraries]
android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down
6 changes: 3 additions & 3 deletions maps-compose-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildFeatures {
Expand All @@ -23,7 +23,7 @@ android {
}

kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
freeCompilerArgs += "-Xexplicit-api=strict"
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
Expand Down
6 changes: 3 additions & 3 deletions maps-compose-widgets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildFeatures {
Expand All @@ -23,7 +23,7 @@ android {
}

kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
freeCompilerArgs += listOf(
"-Xexplicit-api=strict",
"-Xopt-in=kotlin.RequiresOptIn"
Expand Down
6 changes: 3 additions & 3 deletions maps-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildFeatures {
Expand All @@ -24,7 +24,7 @@ android {
}

kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "17"
val stabilityConfigurationFile = layout.projectDirectory.file("compose_compiler_stability_config.conf").asFile
freeCompilerArgs += listOf(
"-Xexplicit-api=strict",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,15 +43,16 @@ import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.LocationSource
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MapColorScheme
import com.google.android.gms.maps.model.PointOfInterest
import com.google.maps.android.ktx.awaitMap
import kotlinx.coroutines.awaitCancellation

/**
* A compose container for a [MapView].
*
* @param mergeDescendants deactivates the map for accessibility purposes
* @param modifier Modifier to be applied to the GoogleMap
* @param mergeDescendants deactivates the map for accessibility purposes
* @param cameraPositionState the [CameraPositionState] to be used to control or observe the map's
* camera state
* @param contentDescription the content description for the map used by accessibility services to
Expand All @@ -69,12 +70,14 @@ import kotlinx.coroutines.awaitCancellation
* @param onPOIClick lambda invoked when a POI is clicked
* @param contentPadding the padding values used to signal that portions of the map around the edges
* may be obscured. The map will move the Google logo, etc. to avoid overlapping the padding.
* @param mapColorScheme Defines the color scheme for the Map. By default it will be
* [ComposeMapColorScheme.FOLLOW_SYSTEM].
* @param content the content of the map
*/
@Composable
public fun GoogleMap(
mergeDescendants: Boolean = false,
modifier: Modifier = Modifier,
mergeDescendants: Boolean = false,
cameraPositionState: CameraPositionState = rememberCameraPositionState(),
contentDescription: String? = null,
googleMapOptionsFactory: () -> GoogleMapOptions = { GoogleMapOptions() },
Expand All @@ -89,6 +92,7 @@ public fun GoogleMap(
onMyLocationClick: ((Location) -> Unit)? = null,
onPOIClick: ((PointOfInterest) -> Unit)? = null,
contentPadding: PaddingValues = DefaultMapContentPadding,
mapColorScheme: ComposeMapColorScheme = ComposeMapColorScheme.FOLLOW_SYSTEM,
content: @Composable @GoogleMapComposable () -> Unit = {},
) {
// When in preview, early return a Box with the received modifier preserving layout
Expand Down Expand Up @@ -120,6 +124,7 @@ public fun GoogleMap(
val currentContentPadding by rememberUpdatedState(contentPadding)
val currentUiSettings by rememberUpdatedState(uiSettings)
val currentMapProperties by rememberUpdatedState(properties)
val currentColorScheme by rememberUpdatedState(mapColorScheme)

val parentComposition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)
Expand All @@ -134,6 +139,7 @@ public fun GoogleMap(
locationSource = currentLocationSource,
mapProperties = currentMapProperties,
mapUiSettings = currentUiSettings,
colorMapScheme = currentColorScheme.value
)

MapClickListenerUpdater()
Expand Down Expand Up @@ -271,3 +277,15 @@ public fun googleMapFactory(
}
}

/**
* Enum representing a 1-1 mapping to [com.google.android.gms.maps.model.MapColorScheme].
*
* This enum provides equivalent values to facilitate usage with [com.google.maps.android.compose.GoogleMap].
*
* @param value The integer value corresponding to each map color scheme.
*/
public enum class ComposeMapColorScheme(public val value: Int) {
LIGHT(MapColorScheme.LIGHT),
DARK(MapColorScheme.DARK),
FOLLOW_SYSTEM(MapColorScheme.FOLLOW_SYSTEM);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.GroundOverlay
import com.google.android.gms.maps.model.MapColorScheme
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.Polyline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.LocationSource
import com.google.android.gms.maps.model.MapColorScheme

internal class MapPropertiesNode(
val map: GoogleMap,
Expand Down Expand Up @@ -104,6 +105,7 @@ internal inline fun MapUpdater(
locationSource: LocationSource?,
mapProperties: MapProperties,
mapUiSettings: MapUiSettings,
colorMapScheme: Int,
) {
val map = (currentComposer.applier as MapApplier).map
val mapView = (currentComposer.applier as MapApplier).mapView
Expand Down Expand Up @@ -139,6 +141,7 @@ internal inline fun MapUpdater(
set(mapProperties.mapType) { map.mapType = it.value }
set(mapProperties.maxZoomPreference) { map.setMaxZoomPreference(it) }
set(mapProperties.minZoomPreference) { map.setMinZoomPreference(it) }
set(colorMapScheme) { map.mapColorScheme = it }
set(contentPadding) {
val node = this
with(this.density) {
Expand Down

0 comments on commit 569ef39

Please sign in to comment.