Skip to content

Commit

Permalink
feat: adding support for accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoso committed Oct 6, 2023
1 parent 8b18a79 commit 124f547
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// Copyright 2023 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 @@ -53,6 +53,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -85,7 +87,6 @@ class BasicMapActivity : ComponentActivity() {

Box(Modifier.fillMaxSize()) {
GoogleMapView(
modifier = Modifier.matchParentSize(),
cameraPositionState = cameraPositionState,
onMapLoaded = {
isMapLoaded = true
Expand Down Expand Up @@ -113,7 +114,6 @@ class BasicMapActivity : ComponentActivity() {

@Composable
fun GoogleMapView(
modifier: Modifier = Modifier,
cameraPositionState: CameraPositionState = rememberCameraPositionState(),
onMapLoaded: () -> Unit = {},
content: @Composable () -> Unit = {}
Expand All @@ -137,7 +137,9 @@ fun GoogleMapView(

if (mapVisible) {
GoogleMap(
modifier = modifier,
modifier = Modifier
.semantics(mergeDescendants = true) { }
.clearAndSetSemantics { },
cameraPositionState = cameraPositionState,
properties = mapProperties,
uiSettings = uiSettings,
Expand Down Expand Up @@ -223,7 +225,8 @@ fun GoogleMapView(
MapButton(
text = "Toggle Map",
onClick = { mapVisible = !mapVisible },
modifier = Modifier.testTag("toggleMapVisibility"),
modifier = Modifier
.testTag("toggleMapVisibility")
)
}
val coroutineScope = rememberCoroutineScope()
Expand Down Expand Up @@ -349,6 +352,6 @@ private fun DebugView(
@Composable
fun GoogleMapViewPreview() {
MapsComposeSampleTheme {
GoogleMapView(Modifier.fillMaxSize())
GoogleMapView()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// Copyright 2023 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 @@ -94,53 +94,57 @@ public fun GoogleMap(
Box(modifier = modifier)
return
}
Box(
modifier = modifier
) {
val context = LocalContext.current
val mapView = remember { MapView(context, googleMapOptionsFactory()) }

val context = LocalContext.current
val mapView = remember { MapView(context, googleMapOptionsFactory()) }

AndroidView(modifier = modifier, factory = { mapView })
MapLifecycle(mapView)
AndroidView(modifier = modifier, factory = { mapView })
MapLifecycle(mapView)

// rememberUpdatedState and friends are used here to make these values observable to
// the subcomposition without providing a new content function each recomposition
val mapClickListeners = remember { MapClickListeners() }.also {
it.indoorStateChangeListener = indoorStateChangeListener
it.onMapClick = onMapClick
it.onMapLongClick = onMapLongClick
it.onMapLoaded = onMapLoaded
it.onMyLocationButtonClick = onMyLocationButtonClick
it.onMyLocationClick = onMyLocationClick
it.onPOIClick = onPOIClick
}
val currentLocationSource by rememberUpdatedState(locationSource)
val currentCameraPositionState by rememberUpdatedState(cameraPositionState)
val currentContentPadding by rememberUpdatedState(contentPadding)
val currentUiSettings by rememberUpdatedState(uiSettings)
val currentMapProperties by rememberUpdatedState(properties)
// rememberUpdatedState and friends are used here to make these values observable to
// the subcomposition without providing a new content function each recomposition
val mapClickListeners = remember { MapClickListeners() }.also {
it.indoorStateChangeListener = indoorStateChangeListener
it.onMapClick = onMapClick
it.onMapLongClick = onMapLongClick
it.onMapLoaded = onMapLoaded
it.onMyLocationButtonClick = onMyLocationButtonClick
it.onMyLocationClick = onMyLocationClick
it.onPOIClick = onPOIClick
}
val currentLocationSource by rememberUpdatedState(locationSource)
val currentCameraPositionState by rememberUpdatedState(cameraPositionState)
val currentContentPadding by rememberUpdatedState(contentPadding)
val currentUiSettings by rememberUpdatedState(uiSettings)
val currentMapProperties by rememberUpdatedState(properties)

val parentComposition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)
val parentComposition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)

LaunchedEffect(Unit) {
disposingComposition {
mapView.newComposition(parentComposition) {
MapUpdater(
contentDescription = contentDescription,
cameraPositionState = currentCameraPositionState,
clickListeners = mapClickListeners,
contentPadding = currentContentPadding,
locationSource = currentLocationSource,
mapProperties = currentMapProperties,
mapUiSettings = currentUiSettings,
)
CompositionLocalProvider(
LocalCameraPositionState provides cameraPositionState,
) {
currentContent?.invoke()
LaunchedEffect(Unit) {
disposingComposition {
mapView.newComposition(parentComposition) {
MapUpdater(
contentDescription = contentDescription,
cameraPositionState = currentCameraPositionState,
clickListeners = mapClickListeners,
contentPadding = currentContentPadding,
locationSource = currentLocationSource,
mapProperties = currentMapProperties,
mapUiSettings = currentUiSettings,
)
CompositionLocalProvider(
LocalCameraPositionState provides cameraPositionState,
) {
currentContent?.invoke()
}
}
}
}
}

}

internal suspend inline fun disposingComposition(factory: () -> Composition) {
Expand Down Expand Up @@ -204,13 +208,15 @@ private fun MapView.lifecycleObserver(previousState: MutableState<Lifecycle.Even
this.onCreate(Bundle())
}
}

Lifecycle.Event.ON_START -> this.onStart()
Lifecycle.Event.ON_RESUME -> this.onResume()
Lifecycle.Event.ON_PAUSE -> this.onPause()
Lifecycle.Event.ON_STOP -> this.onStop()
Lifecycle.Event.ON_DESTROY -> {
//handled in onDispose
}

else -> throw IllegalStateException()
}
previousState.value = event
Expand Down

0 comments on commit 124f547

Please sign in to comment.