Dra
03/23/2022, 3:56 PM@Composable
fun TileMatrixViewer(matrix: TileMatrix) {
Text(Helper.displayer.showMap(matrix), fontFamily = FontFamily.Monospace)
}
object Helper {
val displayer by lazy { BaseDisplayer() }
/* some other things */
}
But the Text
widget never updates when I change the position of a player, how can I make it so it is updated?
You can see the whole class for TileMatrix
right here on my git if it might help to answer -> https://github.com/HFDrae/RoguEngine/blob/main/src/main/kotlin/util/graph/tiles/TileMatrix.kt
Another question that might make this one useless, how should I go if I want to display my grid as a grid of components rather than a String that represents the whole grid ?
Thanks in advance !David W
03/23/2022, 5:14 PMMutableStateFlow
David W
03/23/2022, 5:14 PMyourStateflow.value
David W
03/23/2022, 5:16 PM<https://developer.android.com/jetpack/compose/state#state-in-composables>
This section is what you're looking forDra
03/23/2022, 6:36 PMTileMatrix
in a MutableStateFlow
(tried to modify in the Stage
class) but it didn't work out... đ
I also tried to do this :
@Composable
fun App() {
val stage by remember {
mutableStateOf(
Helper.stageBuilder.setMatrixWidth(40).setMatrixLength(20).setRoomNumber(7).build()
)
}
val map by remember { MutableStateFlow(stage.map) }
// val player by Player(stage.map.)
Helper.currentStage = stage
Column(
verticalArrangement = Arrangement.Center,
modifier = Modifier.padding(5.dp)
) {
Text(text)
Text("-------------------")
TileMatrixViewer(map.value)
}
}
but the val map by remember { ... }
cannot resolve (Error is : Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
, but that's normal as MutableStateFlow
does not take the by
keyword)... Did I mess up to understand what I had to do? :#Casey Brooks
03/23/2022, 7:03 PMState<T>
variables to know when to recompose. So any mutable variables (var
, MutableList
, etc.) are not going to automatically update the UI when they change, you need to use Compose features to let the runtime know when a change has happened.
Instead, you need to use mutableStateOf()
in place of `var`s, mutableStateListOf()
in place of mutableListOf()
, etc.Casey Brooks
03/23/2022, 7:05 PMStateFlow
. Simply wrapping a value in a StateFlow
isnât going to make it observable. You need your data itself to be immutable and contained within a StateFlow, so that the only way to change the data in it is to re-set the .value
of the stateFlow. Once youâve got that, you will need to collect it in Compose with val map by stateFlow.collectAsState()
Dra
03/23/2022, 7:18 PMTileMatrix
, I'll need to change in Tile
the var entity:Entity?
by val entity by mutableStateOf<Entity?>(null)
and to wrap the TileMatrix
in a StateFlow
then in my @Composable
do the val map by matrixAsStateFlow.collectAsState()
?Casey Brooks
03/23/2022, 7:30 PMDra
03/23/2022, 7:38 PMReactivity with Compose really needs to be built-in from the beginningWell... I won't have the time to retake everything for next week so I'll have to make it work somehow... x)
Dra
03/23/2022, 7:39 PMDavid W
03/23/2022, 7:55 PMDavid W
03/23/2022, 7:55 PMDavid W
03/23/2022, 7:56 PMDavid W
03/23/2022, 7:56 PMitems_.value = modRepoCache.items
to update the observable item listDavid W
03/23/2022, 8:06 PMSnapshotStateList
via mutableStateListOf
, also as Casey suggested. that way, i wouldn't need to replace the entire list each timeDra
03/24/2022, 8:30 AM