tad
04/17/2021, 8:25 PMrememberSaveable
with StateFlow<T>.collectAsState()
? I'd like to keep the state save/restore machinery in the composition if possible.Adam Powell
04/17/2021, 8:32 PMStateFlow
of the data already, why save it with rememberSaveable
?tad
04/17/2021, 8:33 PMStateFlow
in a ViewModel
to save/restore across configuration changestad
04/17/2021, 8:51 PMclass StateMachine<State : Any, Event : Any>(
scope: CoroutineScope,
initialState: () -> State,
transition: State.(Event) -> State,
) {
private val events = MutableSharedFlow<Event>()
private val mutableState = MutableStateFlow<State>(initialState())
val state = mutableState.asStateFlow()
init {
scope.launch {
events
.combine(state) { event, state -> state.transition(event) }
.collect { state -> mutableState.value = state }
}
}
suspend fun send(event: Event) {
events.emit(event)
}
}
sealed class MyState {
object Idle : MyState()
object Loading : MyState()
object Error : MyState()
}
sealed class MyEvent {
object Click : MyEvent()
object Success : MyEvent()
object Failure : MyEvent()
}
val transitionFun: State.(Event) -> State = { event ->
when (this) {
is Idle, is Error -> when (event) {
is Click -> Loading
else -> state
}
is Loading -> when (event) {
is Success -> Idle
is Failure -> Error
else -> state
}
}
}
@Composable
fun MyScreen() {
val scope = rememberCoroutineScope()
val stateMachine = remember {
StateMachine(
scope = scope,
initialState = { MyState.Idle },
transition = transitionFun
)
}
val state = stateMachine.state.collectAsState()
MyContent(
state,
onClick = {
scope.launch {
// `transition` could launch a coroutine to perform requests
// and emit success/failure events.
stateMachine.send(Click)
}
}
)
}
tad
04/17/2021, 8:51 PMMark Murphy
04/17/2021, 11:17 PMremember()
thoughIan Lake
04/18/2021, 2:26 AMremember()
in your @Composable
does not help with configuration changes - your entire activity and all composables are destroyed with the default handling of configuration changesnatario1
04/18/2021, 8:43 AMMark Murphy
04/18/2021, 12:12 PMDaniele B
04/18/2021, 1:25 PMAdam Powell
04/18/2021, 3:09 PMAdam Powell
04/18/2021, 3:11 PMremember {}
because there's no live activity recreation to handle, only state restoration from a cold process launch, and rememberSaveable
handles the latter case.louiscad
04/18/2021, 3:12 PMAdam Powell
04/18/2021, 3:13 PMAdam Powell
04/18/2021, 3:14 PMAdam Powell
04/18/2021, 3:16 PM@ComposableActivity
marker for @Composable
functions to generate the whole activity subclass and its manifest entry outside of agp itselflouiscad
04/18/2021, 3:17 PMAdam Powell
04/18/2021, 3:18 PMlouiscad
04/18/2021, 3:19 PMAdam Powell
04/18/2021, 3:25 PM@Composable fun
) in a compilation unit intended to execute at runtime from declaring its nature as an entry point in a dsl in a compilation unit intended to run at build time to generate the manifest. Annotation processing carries different expectations as to when it can affect the code/app and can represent that split in the same file.Adam Powell
04/18/2021, 3:26 PMlouiscad
04/18/2021, 3:27 PMAdam Powell
04/18/2021, 3:34 PMAdam Powell
04/18/2021, 3:37 PMrememberSaveable
performs serialization where live object references are lost. There's a deliberate lossy compression that happens there; live operations in progress that might be associated there are dropped. It's much harder to get into trouble with a stray lambda capture that escapes the scope of a composable that created it.Adam Powell
04/18/2021, 3:38 PMnatario1
04/18/2021, 3:38 PMAdam Powell
04/18/2021, 3:41 PMandroid:configChanges
manifest entry; compose can handle all of them. The small list here will also work for the things that are probably motivating the question: https://developer.android.com/guide/topics/resources/runtime-changes#HandlingTheChange - compose obsoletes the big scary warning about doing this 🙂Adam Powell
04/18/2021, 3:43 PMandroid:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"
louiscad
04/18/2021, 5:25 PMtad
04/18/2021, 5:33 PMrememberSaveable
in order to save/restore across process death?tad
04/18/2021, 5:35 PMrememberSaveable
on the StateMachine itself?Ian Lake
04/20/2021, 3:18 AMSavedStateHandle
, but that's not going to give you anything over rememberSaveable
tad
04/20/2021, 4:12 AMSavedStateHandle
. I am still trying to understand how to use rememberSaveable
with Flow.collectAsState
, however.zalewski.se
01/31/2022, 11:02 PMconfigChanges
list for manifest still up to date or maybe there is even some better way to do it nowadays?Adam Powell
02/01/2022, 2:43 AMlouiscad
02/01/2022, 5:00 AM