André Tessmer
05/09/2023, 7:11 PMMarcin Wisniowski
05/09/2023, 7:40 PMmessage
String, and you can show a Snackbar when it's set, while also informing your ViewModel that you did, so it can set it back to null. Or some other mechanism that makes it only readable once.Colton Idle
05/09/2023, 7:46 PMAndré Tessmer
05/09/2023, 8:01 PMopen class Event<out T>(private val content: T) {
var consumed = false
private set
fun consume(): T? {
return if (consumed) {
null
} else {
consumed = true
content
}
}
}
private val _uiState = MutableStateFlow(SelectWallpaperUiState.initial)
val uiState = _uiState.asStateFlow()
private val _navigationState = MutableStateFlow<SelectWallpaperNavigationState>(SelectWallpaperNavigationState.None)
val navigationState = _navigationState.asStateFlow()
Marcin Wisniowski
05/09/2023, 8:13 PMPrateek Kumar
05/09/2023, 8:13 PMdewildte
05/09/2023, 8:26 PMAndré Tessmer
05/09/2023, 8:28 PMBcz if u use sharedFlow , it will work in XML but on configuration changes or if u use compose, it might create more problems then it will solve@Prateek Kumar do you mind explaining in more detail the possible problems?
navigationEvent: Event<SelectWallpaperNavigationState>
inside my stateEvent
it will also handle the one-shot
thingPrateek Kumar
05/09/2023, 8:38 PMdewildte
05/09/2023, 8:42 PMdata class FormState(val isCompleted: Boolean = false)
When the UI controller (Composable, Fragment, or Activity) observes that isCompleted
is true
then it decides to perform the back navigation.ViewModel
does not need to know about UI details like navigation.
That’s also why you might see the examples given by Google where the UI shows a SnackBar
if a
a String
on a state in a ViewModel
!= null
and then hiding the SnackBar
if it is null
.
The ViewModel layer knows nothing about a purely UI concern.
This makes things easier to test, maintain, reason about, and safer under some conditions.Colton Idle
05/09/2023, 9:45 PMPablichjenkov
05/09/2023, 10:16 PMTim Malseed
05/09/2023, 11:13 PMPablichjenkov
05/09/2023, 11:21 PMAlbert Chang
05/10/2023, 4:52 AM