Last active
March 26, 2020 21:37
-
-
Save ashdavies/e4d0b674c202f9aa1a3b1fed0386ae79 to your computer and use it in GitHub Desktop.
Co'tines NavDirections consumption
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 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) | |
} | |
} |
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 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