Skip to content

Commit abec7f7

Browse files
Duy PhạmDuy Phạm
authored andcommitted
Remove MapPermissionsHandler
The `MapPermissionsHandler` composable has been removed. Permission handling logic is now expected to be managed by the developer using the component.
1 parent e4a0754 commit abec7f7

File tree

1 file changed

+77
-128
lines changed

1 file changed

+77
-128
lines changed

lib-compose/src/main/java/com/what3words/components/compose/maps/W3WMapComponent.kt

Lines changed: 77 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -314,153 +314,102 @@ internal fun W3WMapContent(
314314
onMapViewPortProvided: (W3WGridScreenCell) -> Unit,
315315
onRecallButtonPositionProvided: ((PointF) -> Unit),
316316
) {
317-
// Handles check location permissions, if isMyLocationEnabled enable
318-
MapPermissionsHandler(mapState = mapState, onError = onError) {
317+
var bounds by remember { mutableStateOf(Rect.Zero) }
319318

320-
var bounds by remember { mutableStateOf(Rect.Zero) }
319+
// Check if the map is initialized, use to prevent LaunchedEffect to re-run on configuration changes
320+
val isInitialized = rememberSaveable { mutableStateOf(false) }
321321

322-
// Check if the map is initialized, use to prevent LaunchedEffect to re-run on configuration changes
323-
val isInitialized = rememberSaveable { mutableStateOf(false) }
324-
325-
// // Fetch current location when launch, but not on configuration changes
326-
LaunchedEffect(isInitialized) {
327-
if (!isInitialized.value) {
328-
if (mapState.isMyLocationEnabled && mapConfig.shouldFocusOnMyLocationOnInitialization) onMyLocationClicked.invoke()
329-
isInitialized.value = true
330-
}
322+
// // Fetch current location when launch, but not on configuration changes
323+
LaunchedEffect(isInitialized) {
324+
if (!isInitialized.value) {
325+
if (mapState.isMyLocationEnabled && mapConfig.shouldFocusOnMyLocationOnInitialization) onMyLocationClicked.invoke()
326+
isInitialized.value = true
331327
}
328+
}
332329

333-
val mapColor = remember(mapState.mapType, mapState.isDarkMode) {
334-
when (mapState.mapType) {
335-
W3WMapType.NORMAL,
336-
W3WMapType.TERRAIN -> {
337-
if (mapState.isDarkMode) mapColors.darkMapColor else mapColors.normalMapColor
338-
}
339-
340-
W3WMapType.HYBRID,
341-
W3WMapType.SATELLITE -> {
342-
mapColors.satelliteMapColor
343-
}
330+
val mapColor = remember(mapState.mapType, mapState.isDarkMode) {
331+
when (mapState.mapType) {
332+
W3WMapType.NORMAL,
333+
W3WMapType.TERRAIN -> {
334+
if (mapState.isDarkMode) mapColors.darkMapColor else mapColors.normalMapColor
344335
}
345-
}
346336

347-
val recallButtonColor = remember(mapState.selectedAddress, mapState.markers, mapColor) {
348-
derivedStateOf {
349-
val markersAtSelectedSquare =
350-
mapState.markers.filter { it.square.contains(mapState.selectedAddress?.center) }
351-
val color = when (markersAtSelectedSquare.size) {
352-
0 -> mapColor.markerColors.selectedZoomOutColor
353-
1 -> markersAtSelectedSquare.first().color
354-
else -> mapColor.markerColors.defaultMarkerColor
355-
}
356-
W3WMapButtonsDefault.RecallButtonColor(
357-
recallArrowColor = color.slash,
358-
recallBackgroundColor = color.background
359-
)
337+
W3WMapType.HYBRID,
338+
W3WMapType.SATELLITE -> {
339+
mapColors.satelliteMapColor
360340
}
361341
}
342+
}
362343

363-
val buttonsLayoutConfig = layoutConfig.buttonsLayoutConfig
364-
365-
LaunchedEffect(bounds) {
366-
bounds.let {
367-
val leftTop = PointF(it.left, it.top)
368-
val rightTop = PointF(it.right, it.top)
369-
val rightBottom = PointF(it.right, it.bottom)
370-
val leftBottom = PointF(it.left, it.bottom)
371-
onMapViewPortProvided.invoke(
372-
W3WGridScreenCell(
373-
leftTop,
374-
rightTop,
375-
rightBottom,
376-
leftBottom
377-
)
378-
)
344+
val recallButtonColor = remember(mapState.selectedAddress, mapState.markers, mapColor) {
345+
derivedStateOf {
346+
val markersAtSelectedSquare =
347+
mapState.markers.filter { it.square.contains(mapState.selectedAddress?.center) }
348+
val color = when (markersAtSelectedSquare.size) {
349+
0 -> mapColor.markerColors.selectedZoomOutColor
350+
1 -> markersAtSelectedSquare.first().color
351+
else -> mapColor.markerColors.defaultMarkerColor
379352
}
380-
}
381-
382-
Box(
383-
modifier = modifier
384-
.onGloballyPositioned { coordinates ->
385-
bounds = coordinates.boundsInParent()
386-
}
387-
) {
388-
W3WMapView(
389-
modifier = modifier,
390-
layoutConfig = layoutConfig,
391-
mapConfig = mapConfig,
392-
mapColor = mapColor,
393-
mapProvider = mapProvider,
394-
mapState = mapState,
395-
onMarkerClicked = onMarkerClicked,
396-
onMapClicked = onMapClicked,
397-
content = content,
398-
onCameraUpdated = onCameraUpdated,
399-
onMapProjectionUpdated = onMapProjectionUpdated
400-
)
401-
402-
MapButtons(
403-
modifier = Modifier
404-
.align(buttonsLayoutConfig.buttonAlignment.toComposeAlignment())
405-
.padding(buttonsLayoutConfig.buttonPadding),
406-
buttonConfig = mapConfig.buttonConfig,
407-
buttonState = buttonState,
408-
mapType = mapState.mapType,
409-
locationButtonColor = locationButtonColor,
410-
recallButtonColor = recallButtonColor.value,
411-
onMapTypeClicked = onMapTypeClicked,
412-
onMyLocationClicked = onMyLocationClicked,
413-
onRecallClicked = onRecallClicked,
414-
onRecallButtonPositionProvided = onRecallButtonPositionProvided,
353+
W3WMapButtonsDefault.RecallButtonColor(
354+
recallArrowColor = color.slash,
355+
recallBackgroundColor = color.background
415356
)
416357
}
417358
}
418-
}
419359

420-
/**
421-
* A composable function that handles location permissions for the map.
422-
*
423-
* This function checks if the "My Location" feature is enabled in the map state
424-
* and requests the necessary location permissions if needed. If the permissions
425-
* are granted, it displays the provided content. Otherwise, it invokes the
426-
* `onError` callback with a [W3WError] indicating that permissions are required.
427-
*
428-
* @param mapState The [W3WMapState] object that holds the state of the map.
429-
* @param onError Callback invoked when an error occurs, such as when location
430-
* permissions are denied.
431-
* @param content The composable content to be displayed if location permissions
432-
* are granted or if the "My Location" feature is disabled.
433-
*/
434-
@OptIn(ExperimentalPermissionsApi::class)
435-
@Composable
436-
internal fun MapPermissionsHandler(
437-
mapState: W3WMapState,
438-
onError: ((W3WError) -> Unit)? = null,
439-
content: @Composable () -> Unit
440-
) {
441-
if (mapState.isMyLocationEnabled) {
442-
val permissionState = rememberMultiplePermissionsState(
443-
listOf(
444-
Manifest.permission.ACCESS_COARSE_LOCATION,
445-
Manifest.permission.ACCESS_FINE_LOCATION
360+
val buttonsLayoutConfig = layoutConfig.buttonsLayoutConfig
361+
362+
LaunchedEffect(bounds) {
363+
bounds.let {
364+
val leftTop = PointF(it.left, it.top)
365+
val rightTop = PointF(it.right, it.top)
366+
val rightBottom = PointF(it.right, it.bottom)
367+
val leftBottom = PointF(it.left, it.bottom)
368+
onMapViewPortProvided.invoke(
369+
W3WGridScreenCell(
370+
leftTop,
371+
rightTop,
372+
rightBottom,
373+
leftBottom
374+
)
446375
)
447-
)
448-
449-
LaunchedEffect(Unit) {
450-
permissionState.launchMultiplePermissionRequest()
451376
}
377+
}
452378

453-
when {
454-
permissionState.allPermissionsGranted -> {
455-
content()
379+
Box(
380+
modifier = modifier
381+
.onGloballyPositioned { coordinates ->
382+
bounds = coordinates.boundsInParent()
456383
}
384+
) {
385+
W3WMapView(
386+
modifier = modifier,
387+
layoutConfig = layoutConfig,
388+
mapConfig = mapConfig,
389+
mapColor = mapColor,
390+
mapProvider = mapProvider,
391+
mapState = mapState,
392+
onMarkerClicked = onMarkerClicked,
393+
onMapClicked = onMapClicked,
394+
content = content,
395+
onCameraUpdated = onCameraUpdated,
396+
onMapProjectionUpdated = onMapProjectionUpdated
397+
)
457398

458-
else -> {
459-
onError?.invoke(W3WError(message = "Map component needs permission"))
460-
}
461-
}
462-
} else {
463-
content()
399+
MapButtons(
400+
modifier = Modifier
401+
.align(buttonsLayoutConfig.buttonAlignment.toComposeAlignment())
402+
.padding(buttonsLayoutConfig.buttonPadding),
403+
buttonConfig = mapConfig.buttonConfig,
404+
buttonState = buttonState,
405+
mapType = mapState.mapType,
406+
locationButtonColor = locationButtonColor,
407+
recallButtonColor = recallButtonColor.value,
408+
onMapTypeClicked = onMapTypeClicked,
409+
onMyLocationClicked = onMyLocationClicked,
410+
onRecallClicked = onRecallClicked,
411+
onRecallButtonPositionProvided = onRecallButtonPositionProvided,
412+
)
464413
}
465414
}
466415

0 commit comments

Comments
 (0)