Skip to content

Instantly share code, notes, and snippets.

@ngallazzi
Created January 8, 2021 15:43
Show Gist options
  • Save ngallazzi/246cd98688bf5095f3c31afc4095bbc8 to your computer and use it in GitHub Desktop.
Save ngallazzi/246cd98688bf5095f3c31afc4095bbc8 to your computer and use it in GitHub Desktop.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
booksAdapter = BookAdapter(requireContext(), object : BookAdapter.ActionClickListener {
override fun bookmark(book: BookWithStatus) {
booksViewModel.bookmark(book)
}
override fun unbookmark(book: BookWithStatus) {
booksViewModel.unbookmark(book)
}
})
booksViewModel.getBooks("Robert C. Martin")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
booksViewModel.books.observe(viewLifecycleOwner, {
booksAdapter.submitUpdate(it)
})
booksViewModel.dataLoading.observe(viewLifecycleOwner, { loading ->
when (loading) {
true -> LayoutUtils.crossFade(pbLoading, rvBooks)
false -> LayoutUtils.crossFade(rvBooks, pbLoading)
}
})
rvBooks.apply {
layoutManager =
GridLayoutManager(requireContext(), COLUMNS_COUNT)
adapter = booksAdapter
}
booksViewModel.error.observe(viewLifecycleOwner, {
Toast.makeText(
requireContext(),
getString(R.string.an_error_has_occurred, it),
Toast.LENGTH_SHORT
).show()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment