Stylianos Gakis
10/15/2024, 11:45 AM@Stable
private class SomeStateHolder<T>() {
var value: T by mutableStateOf(...)
var someFlag: Boolean by mutableStateOf(false)
}
I want to wire things up internally so that someFlag
can be automatically also observe the changes to value
, and if there is a change to it, have it reset back to false
.
That would be the functionality of what this should be doing:
@Stable
private class SomeStateHolder<T>(
coroutineScope: CoroutineScope,
) {
var value: T by mutableStateOf(...)
var someFlag: Boolean by mutableStateOf(false)
init {
coroutineScope.launch {
snapshotFlow { value }.drop(1).collectLatest {
someFlag = false
}
}
}
}
But I am not sure if passing a coroutineScope to my state holder like that to do the observation on init
is the way to go.
Does anyone have any other thoughts on this?Alex Styl
10/15/2024, 11:48 AMshowErrorMessage
come from?Stylianos Gakis
10/15/2024, 11:51 AMAlex Styl
10/15/2024, 11:52 AMAlex Styl
10/15/2024, 11:54 AMAlex Styl
10/15/2024, 11:56 AMStylianos Gakis
10/15/2024, 11:56 AMvalue: T
) and to have a boolean storing information on if we want to show the error message on this input or not.
This would practically be used in some forms, where I do want to know if the value is "invalid", but I do not want to show the fact that it has an error yet, until the "Continue" button is pressed. At that point the flag would be turned to true to show the error.
Then I want to automatically be able to change the input, and have the error message hide, no matter if the input is still invalid, since they are actively trying to fix it. Hence the snaphshot flow, clearing that flag after any change on the input itself.Stylianos Gakis
10/15/2024, 11:57 AMyou can do it with derived state of if u need to combine multiple states togetherI would not be able to use derivedStateOf because I do want to update that flag manually as well, and not have it only be a derived value of other things.
and have someone else update it.Yes, this is what I am leaning towards as well, perhaps with an accompanying
rememberSomeStateHolder()
which wires things up for me.Oleksandr Balan
10/15/2024, 12:01 PM@Stable
private class SomeStateHolder<T>() {
var value: T by mutableStateOf(...)
private set
var someFlag: Boolean by mutableStateOf(false)
fun setValue(value: T) {
this.value = value
someFlag = false
}
}
Stylianos Gakis
10/15/2024, 12:02 PMAlex Vanyo
10/15/2024, 6:08 PMvalue
and someFlag
together in the same object:
@Stable
class InvalidCondition<T>(
val value: T,
) {
var someFlag: Boolean by mutableStateOf(false)
private set
fun showInvalid() {
someFlag = true
}
}
@Stable
class SomeStateHolder<T> {
var condition: InvalidCondition<T> by mutableStateOf(...)
private set
fun setValue(value: T) {
condition = InvalidCondition(value) // updating condition changes some flag since it is a new object
}
}
So you can capture the idea of âflag tied to a specific version of the inputâ by the lifetime of the object representing that version of the inputStylianos Gakis
10/16/2024, 12:40 PMvar value: T by mutableStateOf(initialValue)
private set
fun setValue(newValue: T) {
someFlag = false
value = newValue
}
fails with compilation error:
Platform declaration clash: The following declarations have the same JVM signature (setValue(Ljava/lang/Object;)V):
fun `<set-value>`(`<set-?>`: T): Unit defined in com...
fun setValue(newValue: T): Unit defined in com...
So this works, but you need a more unique name đ
For your suggestion Alex, then I will end up holding my InvalidCondition
state object inside a MutableState
, but then it internally will also contain the someFlag
also in a MutableState
. Will that be what I actually want here? Feels like the wrong thing to do in a wayAlex Vanyo
10/16/2024, 5:11 PMStylianos Gakis
10/16/2024, 5:13 PM