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
| Saved as "mlog" with Kotlin statement applicability. (Obviously change to suit your own needs.) | |
| android.util.Log.d("LOGLOGLOG", "$className$ $METHOD_NAME$() called with: " + $args$) | |
| Params: | |
| classname - kotlinClassName() | |
| METHOD_NAME - kotlinFunctionName() | |
| args - groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", functionParameters()) |
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 MainViewModel : ViewModel() { | |
| sealed class Event { | |
| object NavigateToSettings: Event() | |
| data class ShowSnackBar(val text: String): Event() | |
| data class ShowToast(val text: String): Event() | |
| } | |
| private val eventChannel = Channel<Event>(Channel.BUFFERED) | |
| val eventsFlow = eventChannel.receiveAsFlow() |
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 MainViewModel : ViewModel() { | |
| sealed class Event { | |
| object NavigateToSettings: Event() | |
| data class ShowSnackBar(val text: String): Event() | |
| data class ShowToast(val text: String): Event() | |
| } | |
| private val eventChannel = Channel<Event>(Channel.BUFFERED) | |
| val eventsFlow = eventChannel.receiveAsFlow() |
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
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| viewModel.eventsFlow | |
| .onEach { | |
| when (it) { | |
| is MainViewModel.Event.NavigateToSettings -> {} | |
| is MainViewModel.Event.ShowSnackBar -> {} | |
| is MainViewModel.Event.ShowToast -> {} | |
| } | |
| } |
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
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| // get your view model here | |
| lifecycleScope.launchWhenStarted { | |
| viewModel.eventsFlow | |
| .collect { | |
| when (it) { | |
| MainViewModel.Event.NavigateToSettings -> {} | |
| is MainViewModel.Event.ShowSnackBar -> {} |
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
| override fun onStart() { | |
| super.onStart() | |
| disposable = viewModel.eventsFlow | |
| .asObservable() // converting to Rx for the example | |
| .subscribe { | |
| when (it) { | |
| MainViewModel.Event.NavigateToSettings -> {} | |
| is MainViewModel.Event.ShowSnackBar -> {} | |
| is MainViewModel.Event.ShowToast -> {} | |
| } |
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
| override fun onStart() { | |
| super.onStart() | |
| job = viewModel.eventsFlow | |
| .onEach { | |
| when (it) { | |
| MainViewModel.Event.NavigateToSettings -> {} | |
| is MainViewModel.Event.ShowSnackBar -> {} | |
| is MainViewModel.Event.ShowToast -> {} | |
| } | |
| } |
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 FlowObserver<T> ( | |
| lifecycleOwner: LifecycleOwner, | |
| private val flow: Flow<T>, | |
| private val collector: suspend (T) -> Unit | |
| ) { | |
| private var job: Job? = null | |
| init { | |
| lifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { |
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
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| viewModel.eventsFlow | |
| .onEach { | |
| when (it) { | |
| MainViewModel.Event.NavigateToSettings -> {} | |
| is MainViewModel.Event.ShowSnackBar -> {} | |
| is MainViewModel.Event.ShowToast -> {} | |
| } |
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
| private val eventChannel = Channel<Event>(Channel.BUFFERED) | |
| val eventsFlow = eventChannel.receiveAsFlow() |
OlderNewer