Skip to content

Instantly share code, notes, and snippets.

@ashdavies
Last active March 26, 2020 21:37
Show Gist options
  • Save ashdavies/e4d0b674c202f9aa1a3b1fed0386ae79 to your computer and use it in GitHub Desktop.
Save ashdavies/e4d0b674c202f9aa1a3b1fed0386ae79 to your computer and use it in GitHub Desktop.
Co'tines NavDirections consumption
class Activity : AppCompatActivity {
private val navController: NavController by lazy(NONE) { findNavController(R.id.host) }
private val viewModel: CoroutineViewModel by viewModels()
fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
viewModel
.navDirections
.onEach { navController.navigate(it) }
.launchIn(lifecycleScope)
}
}
class CoroutineViewModel : ViewModel() {
private val _navDirections: Channel<NavDirections> = Channel(CONFLATED)
val navDirections: Flow<NavDirections> get() = _navDirections.receiveAsFlow()
fun onButtonClick() {
_navDirections.offer(activityNavDirection())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment