Skip to content

Instantly share code, notes, and snippets.

@Syex
Last active October 9, 2022 13:55
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/9dca79a66f2d5c8cbe4ef03776d20947 to your computer and use it in GitHub Desktop.
Save Syex/9dca79a66f2d5c8cbe4ef03776d20947 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 var userView: UserView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
userView = UserView.inflate(inflater, container, false)
return userView!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.events
.onEach {
// we delegate render events to our UserView
userView?.handle(it)
}
.launchIn(viewLifecycleOwner.lifecycle.coroutineScope)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment