I'm trying to use OSMDroid library in my Compose Android app. I used this approach and it works great, but when I change to dark mode in my app, the map becomes just a grid, and no tiles are displayed. It keeps the same if I revert the dark mode. If I start the app in dark mode it works fine, but when changed to light mode it breaks again. Basically, I can't change the light/dark mode while using the app. Any polygon I draw also disappears and I would like to maintain them in the view.
I've managed ti move the MapView declaration to a ViewModel and manually call the onPause() and onReseme() functions in the activity lifecycle, but I haven't solved the problem.
MainViewModel.kt
class MainViewModel(context: MainActivity): ViewModel(){
var mapView: MapView = MapView(context).apply {
id = R.id.map
clipToOutline = true
tileProvider = MapTileProviderBasic(context, TileSourceFactory.MAPNIK)
tileProvider.tileRequestCompleteHandlers.add(SimpleInvalidationHandler(this))
zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER)
setMultiTouchControls(true)
}
}
Mainscreen Composable function
@Composable
fun MapScreen(
mainViewModel: MainViewModel
) {
MapView(
modifier = Modifier.padding(paddingValues),
mapViewState = mainViewModel.mapView
)
}
MapView function (modified from GitHub)
@Composable
fun MapView(
modifier: Modifier = Modifier,
useDarkScheme: Boolean = isSystemInDarkTheme(),
mapViewState: MapView = rememberMapViewWithLifecycle(),
onLoad: ((map: MapView, controller: IMapController) -> Unit)? = null
) {
AndroidView(
factory = { mapViewState },
modifier = modifier
) { mapView ->
val mapController: IMapController = mapView.controller
onLoad?.invoke(mapView, mapController)
}
}
Any help would be appreciatted!