Skip to content

Instantly share code, notes, and snippets.

@ajailani4
Created June 3, 2021 11:23
Show Gist options
  • Save ajailani4/9746f0cf25fa88d0ece1696124543200 to your computer and use it in GitHub Desktop.
Save ajailani4/9746f0cf25fa88d0ece1696124543200 to your computer and use it in GitHub Desktop.
@AndroidEntryPoint
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private val homeViewModel: HomeViewModel by viewModels()
private lateinit var brandHomeAdapter: BrandsHomeAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentHomeBinding.inflate(
inflater, container, false
)
return binding.root
}
@ExperimentalCoroutinesApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupView()
}
@ExperimentalCoroutinesApi
private fun setupView() {
// Setup brandsAdapter and brandsRv
brandHomeAdapter = BrandsHomeAdapter({ brand ->
// navigate to PhonesFragment
}, { brandSlug, phoneSlug ->
// navigate to PhoneSpecsFragments
})
brandHomeAdapter.addLoadStateListener { loadState ->
if (loadState.refresh is LoadState.Loading) {
binding.apply {
progressBar.visibility = View.VISIBLE
brandsRv.visibility = View.GONE
}
} else {
binding.apply {
progressBar.visibility = View.GONE
brandsRv.visibility = View.VISIBLE
}
}
}
binding.brandsRv.apply {
layoutManager = LinearLayoutManager(context)
adapter = brandHomeAdapter.withLoadStateFooter(
footer = FooterAdapter()
)
isNestedScrollingEnabled = false
}
// Get brands list and show it
lifecycleScope.launch {
homeViewModel.getBrandsHome().collect { brands ->
brandHomeAdapter.submitData(brands)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment