I basically have a Composable function with a lambda returning a value which a text composable is supposed to display.
data class CustomUiState(
val lookupList: HashMap<UUID, MutableList<String>> = HashMap()
)
class CustomViewModel(
): ViewModel() {
private val _uiState: MutableStateFlow(CustomUiState())
val uiState: StateFlow<CustomUiState> = _uiState.asStateFlow()
fun getValue(index: Int) {
// complex lookup with lookupList
return ...
}
fun updateValue(i: Int, s: String) {
_uiState.update { state ->
state.copy(
// Update lookupList with new value
lookupList = ...
)
}
}
}
@Composable
fun Screen(viewModel: CustomViewModel) {
Widget(
value = { viewModel.getValue(it) },
onValueChanged = { i, s viewModel.updateValue(i, s) }
)
}
@Composable
fun Widget(
value: (Int) -> String,
onValueChanged: (Int, String) -> Unit
) {
LazyRow() {
items(4) {
// This does not work
// Because Compose do not recompose when value(it) changes
Text(
text = value(it),
//lots of other configurations
...
)
}
}
}
I have tried Googling but with no luck. What do I need to change to get this to work?
Adding text just because StackOverflow didn't let me post with to little text in the question.