Skip to content

Instantly share code, notes, and snippets.

@devrath
Created January 22, 2023 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/8d21ee02ddf6642fead0dff5e9efcb50 to your computer and use it in GitHub Desktop.
Save devrath/8d21ee02ddf6642fead0dff5e9efcb50 to your computer and use it in GitHub Desktop.
class LaunchedEffectActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CurrentScreen()
}
}
@Composable
private fun CurrentScreen(
vm : LaunchedEffectVm = LaunchedEffectVm()
){
LaunchedEffect(key1 = true){
vm.sharedFlow.collect{
when(it){
is LaunchedEffectVm.LaunchedEffectEvents.DisplayErrorMessage -> TODO()
is LaunchedEffectVm.LaunchedEffectEvents.ShowNotification -> {
Toast.makeText(this@LaunchedEffectActivity,"Value->"+it.count,Toast.LENGTH_LONG).show()
}
}
}
}
}
}
class LaunchedEffectVm : ViewModel() {
private val _sharedFlow = MutableSharedFlow<LaunchedEffectEvents>()
val sharedFlow = _sharedFlow.asSharedFlow()
init {
viewModelScope.launch {
_sharedFlow.emit(LaunchedEffectEvents.ShowNotification(1))
}
}
sealed class LaunchedEffectEvents {
data class DisplayErrorMessage(val message : String):LaunchedEffectEvents()
data class ShowNotification(val count : Int):LaunchedEffectEvents()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment