Created
January 22, 2023 09:29
-
-
Save devrath/8d21ee02ddf6642fead0dff5e9efcb50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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