Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Last active May 19, 2019 14:47
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 cesarferreira/380d2c960f6da4a34fdabf3e74d3c3e0 to your computer and use it in GitHub Desktop.
Save cesarferreira/380d2c960f6da4a34fdabf3e74d3c3e0 to your computer and use it in GitHub Desktop.
class FriendsFragment : BaseFragment() {
override fun layoutId(): Int = R.layout.fragment_friends
private lateinit var friendsViewModel: FriendsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appComponent.inject(this)
friendsViewModel = viewModel(viewModelFactory) {
observe(state, ::onFriendsStateChange)
}
}
private fun onFriendsStateChange(state: FriendsState?) {
when (val friendsState = state!!) {
is FriendsState.Loading -> showLoading()
is FriendsState.Empty -> showEmptyState()
is FriendsState.Success -> renderList(friendsState.users)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
friendsViewModel.loadData()
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment