Skip to content

Instantly share code, notes, and snippets.

@DivS-15
Created May 22, 2022 11:11
Show Gist options
  • Save DivS-15/acdf8f8e81b0036a9b68083b72193e50 to your computer and use it in GitHub Desktop.
Save DivS-15/acdf8f8e81b0036a9b68083b72193e50 to your computer and use it in GitHub Desktop.
@AndroidEntryPoint
class VideosFragment : Fragment(R.layout.fragment_popular_videos_list) {
private lateinit var _binding: FragmentPopularVideosListBinding
private val binding get() = _binding
private val viewModel: VideoViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentPopularVideosListBinding.bind(view)
val adapter = VideoAdapter()
binding.apply {
videosList.adapter = adapter.withLoadStateFooter(
footer = VideoLoadStateAdapter{adapter.retry()}
)
}
viewLifecycleOwner.lifecycleScope.launch {
adapter.loadStateFlow.collectLatest {
binding.progressBar.isVisible = it.source.append is LoadState.Loading
}
}
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.items.collectLatest {
adapter.submitData(it)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment