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
| package com.example.launchwhendemo.ui.main | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.view.View | |
| import android.widget.Button | |
| import androidx.fragment.app.Fragment | |
| import androidx.lifecycle.ViewModel | |
| import androidx.lifecycle.ViewModelProvider | |
| import androidx.lifecycle.viewModelScope |
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
| package com.example.launchwhendemo.ui.main | |
| import android.os.Bundle | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.fragment.app.Fragment | |
| import androidx.lifecycle.* | |
| import com.example.launchwhendemo.R | |
| import kotlinx.coroutines.channels.Channel |
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() |
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
| 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 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
| 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 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 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
| 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() |