Last active
October 9, 2022 13:55
-
-
Save Syex/9dca79a66f2d5c8cbe4ef03776d20947 to your computer and use it in GitHub Desktop.
This file contains 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
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