Skip to content

Instantly share code, notes, and snippets.

@Syex
Last active October 9, 2022 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Syex/977413f43d15646583dd743f50852a1e to your computer and use it in GitHub Desktop.
Save Syex/977413f43d15646583dd743f50852a1e to your computer and use it in GitHub Desktop.
data class Event(val username: String)
class UserViewModel : ViewModel() {
val events = MutableSharedFlow<Event>
}
class UserFragment : Fragment() {
private val viewModel by viewModels<UserViewModel>()
private lateinit var binding: UserBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.events
.onEach {
// we access Android views in our Fragment directly
binding.usernameView.text = it.username
}
.launchIn(viewLifecycleOwner.lifecycle.coroutineScope)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment