Jacob Bosco
06/03/2021, 5:21 AMAppState
object and it has some values in nested properties that I want to access. If I use mutableStateOf()
, the state only updates when the entire AppState
object is changed not when its properties change.
I have also tried kotllin Flow for the first time but the only solution I could get was to make each of the properties a flow and then individually watch them in the main compose function.
Is there any other way to manage state where I need to watch nested properties?Albert Chang
06/03/2021, 5:32 AMmutableStateOf()
at property level. Something similar to this.Jacob Bosco
06/03/2021, 5:37 AMstructuralEqualityPolicy()
the thing that helps deal with the nested properties?Albert Chang
06/03/2021, 5:44 AMJacob Bosco
06/03/2021, 6:06 AMJacob Bosco
06/03/2021, 6:06 AMclass AppState {
var fileState = mutableStateOf(File(""))
var coreState = mutableStateOf(emptyCore())
private fun createCore() {
val (instructions: List<Instruction>, parsedVariableData: List<ParsedData>) = Parser.parseDataFromFile(fileState.value)
val program = Program(
instructions = instructions,
parsedData = parsedVariableData
)
coreState.value = Core(program = program)
}
val loadFile: (File) -> Unit = {
fileState.value = it
createCore()
}
val executeProgram: () -> Unit = coreState.value::runProgram
}
Jacob Bosco
06/03/2021, 6:08 AMAlbert Chang
06/03/2021, 6:14 AMclass AppState {
var file by mutableStateOf(File(""))
val core by derivedStateOf {
val (instructions: List<Instruction>, parsedVariableData: List<ParsedData>) = Parser.parseDataFromFile(file)
val program = Program(
instructions = instructions,
parsedData = parsedVariableData
)
Core(program = program)
}
}
Albert Chang
06/03/2021, 6:17 AMexecuteProgram
is not updating, it's expected because you are not updating it.Jacob Bosco
06/03/2021, 6:24 AMrunProgram
updates it's internal state. If I use derivedStateOf
then it won't be able to reflect that change.Jacob Bosco
06/03/2021, 6:26 AMderivedStateOf
will evaluate the block that I pass to it when I call core.value
so it'll create a new core every-time something changes.Albert Chang
06/03/2021, 6:30 AMrunProgram
? And you don't want core
to be updated every time file
changes? Can you describe what you want more specifically?Jacob Bosco
06/03/2021, 6:35 AMmemoryArray
and registerArray
will get updated ( will have new values after running instructions like ADD, SUB etc.).
The instructions come from the file, so when I load a new file I have to parse it and for now it creates a new core.
So when file
changes I need a new core, but after runProgram
I need the same core but its property values will be different.Albert Chang
06/03/2021, 6:41 AMSo whenThe code I posted above does exactly this. If you want the properties ofchanges I need a new corefile
Core
to be observable, just define them as states (e.g. var memoryArray by mutableStateOf(emptyArray())
).Jacob Bosco
06/03/2021, 6:49 AMregisterArray
has some values that I need to observe that are nested in some more objects. I already thought of doing this but doing it individually for all of them is repetitive and fragile so I was trying to find a better approach.Albert Chang
06/03/2021, 6:53 AMvar memoryArray by mutableStateOf(emptyArray())
and var memoryArray = emptyArray()
differ much in terms of verbosity. You can't expect properties to automatically become observable.Jacob Bosco
06/03/2021, 6:55 AMmemoryArray by mutableStateOf
where Core
or MemoryArray
is defined I can't do thatAlbert Chang
06/03/2021, 6:56 AMmutableStateOf()
is UI logic?Jacob Bosco
06/03/2021, 6:57 AMJacob Bosco
06/03/2021, 6:57 AMAlbert Chang
06/03/2021, 7:02 AMStateFlow
. Anyway you need an observable mechanism as
You can't expect properties to automatically become observable.
Jacob Bosco
06/03/2021, 7:03 AMmutableStateOf
but I'll try to make them properties in AppState
by individually referencing them from the core
variable.Jacob Bosco
06/03/2021, 7:03 AMJacob Bosco
06/03/2021, 7:04 AMColton Idle
06/03/2021, 12:57 PMMichael Paus
06/04/2021, 8:11 AMmutableStateOf
always changes when any of the nested properties is changed.