mapbox-maps-android icon indicating copy to clipboard operation
mapbox-maps-android copied to clipboard

[compose] Usage of rememberRasterSourceState does not reset remembered value when key changes

Open bamsbamx opened this issue 4 months ago • 0 comments

Environment

  • Android OS version: Android 16, Compose (Multiplatform) 1.10.0 alpha 01
  • Devices affected: (Tested on) Samsung Galaxy S25
  • Maps SDK Version: 11.14.6

Observed behavior and steps to reproduce

In the following code, when mapLayer changes, the content of myCustomSourceState is not reset, even if re-composition is triggered due to a change of instance of the mapLayer. So the tiles URL cannot be changed even if map layer is different.

    @Composable
    fun rasterSourceExample(
        mapLayer: MapLayer,
    ) {
        val myCustomSourceState = rememberRasterSourceState(
            key = mapLayer.toString(),    //  if this changes, the myCustomSourceState does not reset
            sourceId = MAPBOX_SOURCE_CUSTOM_ID,
        ) {
            tileSize = LongValue(MAPBOX_SOURCE_TILE_SIZE)
            tiles = StringListValue(mapLayer.rasterLayerSourceUrl)
        }
    }

Expected behavior

myCustomSourceState should be updated with the new tiles URL from the new value of mapLayer

Notes / preliminary analysis

One can say the reason is that the value passed as key argument is the same, but is not, since mapLayer is a data class. After doing some manual implementations, seems like the Mapbox implementation of rememberRasterSourceState makes use of a deprecated overload. Instead of using key argument, inputs argument should be used to check for the remembered value invalidation. The following code works and could be the fix for the Mapbox implementation

Additional links and references

The problem: Image

The solution

Image

bamsbamx avatar Sep 26 '25 21:09 bamsbamx