Cam
Cam

Reputation: 79

MarkerInfoWindow content not recomposing when var is mutated [Jetpack Compose]

I am making a Map app using Jetpack Compose in Android Studio.

Basically I am trying to edit a map marker's content with a dialog popup by calling the "editPlace" function. The var "title" will be mutated by "editPlace" with the lambda expression { a: String, b: String -> title = a; info.title = a; info.desc = b }, and then I want the MarkerInfoWindowContent to recompose and show the updated var "title".

What is currently happening is that MarkerInfoWindowContent continues to show the old title after the completion of the "editPlace" function. This means that either MarkerInfoWindowContent is not recomposing or it is failing to show the recomposed content for some reason.

GoogleMap(
        modifier = modifier,
        cameraPositionState = cameraPositionState,
        }
    ) {

        val marker = _Marker(1.35, 103.87)

        drawPlace( marker )
    }
@Composable
fun drawPlace(info: _Marker) {
    var title by remember { mutableStateOf(info.title) }
    var showEdit by remember { mutableStateOf(false) }

    MarkerInfoWindowContent(

        state = MarkerState(position = LatLng(info.lat, info.long)),
        onInfoWindowClick = { showEdit = true },
    ) {

        Column {
            Text (
                title
            )

        }


        if( showEdit )
        {
            editPlace( info, { a: String, b: String -> title = a; info.title = a; } ) { showEdit = false }
        }
    }


}
@Composable
fun editPlace(info: _Marker, save: (String, String) -> Unit, onDismissRequest: () -> Unit ){

    var makeEdit by remember{ mutableStateOf( false ) }
    var title by remember { mutableStateOf( info.title ) }
    var desc by remember { mutableStateOf( info.desc ) }

    Dialog(
        onDismissRequest = { onDismissRequest() },
        properties = DialogProperties(
            dismissOnClickOutside = true
        )
    ) {

        Card() {

            TextField( value = title,
                onValueChange = { title = it})

            Row() {
                TextButton(
                    onClick = {
                        makeEdit = true
                        save( title, desc )
                        onDismissRequest()
                    } ) {
                    Text("Save")
                }
        
            }
        }
    }
}
class _Marker constructor(lat: Double, long: Double, title: String = "", desc: String = ""){
    val lat = lat
    val long = long
    var desc = desc
    var title = title
}

Upvotes: 1

Views: 127

Answers (0)

Related Questions