-
As the following codes, how can observe changes and update like the phoneNumber for exemple from my SignUpUi composable ? class SignUpComponent(
componentContext: ComponentContext
):SignUp, ComponentContext by componentContext {
private val stateEntity = instanceKeeper.getOrCreate {
StateEntity(
savedState = stateKeeper.consume(
"save_state_value", SerializableContainer.serializer())
)
}
init {
stateKeeper.register(
key = "save_state_value",
strategy = SerializableContainer.serializer(),
supplier = stateEntity::saveState
)
}
}
interface SignUp {
}
class StateEntity(
savedState: SerializableContainer?
): InstanceKeeper.Instance {
var state: State = savedState?.consume(strategy = State.serializer()) ?: State()
private set
fun saveState() : SerializableContainer = SerializableContainer(
value = state, strategy = State.serializer()
)
override fun onDestroy() {}
@Serializable
data class State(
val phoneNumber: Long? = null,
)
}
@Composable
fun SignUpUi(
component: SignUp
){
// ui codes...
} |
Beta Was this translation helpful? Give feedback.
Answered by
arkivanov
Jan 7, 2024
Replies: 1 comment 3 replies
-
You can use coroutines or Reaktive for observable things. E.g. with coroutines this could like like this: class StateEntity(
savedState: SerializableContainer?
): InstanceKeeper.Instance {
private val _state = MutableStateFlow(savedState?.consume(strategy = State.serializer()) ?: State())
val state: StateFlow<State> = _state
fun saveState() : SerializableContainer = SerializableContainer(
value = _state.value, strategy = State.serializer()
)
private fun foo() {
_state.update { it.copy(phoneNumber = ...) }
}
override fun onDestroy() {}
@Serializable
data class State(
val phoneNumber: Long? = null,
)
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interfaces for components are optional. They can be useful if you want to create Compose/SwiftUI previews by writing a fake implementation of your component with fake data. Or if you want to write test doubles for navigation tests.
Please see #69.