Last active
May 15, 2020 23:00
-
-
Save cdmunoz/f767b656045aec3dc62791bb0f775cdb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class HomeFragment : Fragment() { | |
| companion object { | |
| private val TAG = HomeFragment::class.java.name | |
| } | |
| private lateinit var binding: FragmentHomeBinding | |
| private var photosViewModel: PhotosViewModel? = null | |
| override fun onCreateView(inflater: LayoutInflater, | |
| container: ViewGroup?, | |
| savedInstanceState: Bundle?): View? { | |
| binding = FragmentHomeBindingImpl.inflate(inflater) | |
| binding.lifecycleOwner = this@HomeFragment | |
| return binding.root | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| //stuff here | |
| } | |
| private fun initBindings() { | |
| //stuff here | |
| } | |
| private fun initViewModels() { | |
| if (null == photosViewModel) { | |
| photosViewModel = ViewModelProvider(this@HomeFragment, | |
| ViewModelFactory(RetrofitService.createService(ApiService::class.java))).get( | |
| PhotosViewModel::class.java) | |
| photosViewModel?.loadData() | |
| } | |
| } | |
| private fun initObservers() { | |
| photosViewModel?.getPhotos()?.observe(viewLifecycleOwner, Observer { result -> | |
| when (result) { | |
| is Result.Success -> { | |
| EspressoIdlingResource.increment() | |
| renderList(result.data) | |
| EspressoIdlingResource.decrement() | |
| binding.homePhotosProgressContainer.visibility = View.GONE | |
| binding.homePhotosList.visibility = View.VISIBLE | |
| } | |
| is Result.InProgress -> { | |
| binding.homePhotosProgressContainer.visibility = View.VISIBLE | |
| binding.homePhotosList.visibility = View.GONE | |
| } | |
| is Result.Error -> { | |
| binding.homePhotosProgressContainer.visibility = View.GONE | |
| Toast.makeText(activity, result.exception.message, Toast.LENGTH_LONG).show() | |
| } | |
| } | |
| }) | |
| } | |
| private fun fetchMoreData() { | |
| photosViewModel?.loadDataNextPage() | |
| } | |
| private fun renderList(photos: ArrayList<Photo>) { | |
| if (photos.isNotEmpty()) { | |
| //when screen starts | |
| if (photosViewModel?.getCurrentPage() == 1 || binding.homePhotosList.adapter?.itemCount == 0) { | |
| setRecyclerData(photos) | |
| } else { //when load more | |
| if (binding.homePhotosList.adapter == null) { //after load more | |
| setRecyclerData(photos) | |
| binding.homePhotosList.adapter?.notifyDataSetChanged() | |
| } | |
| } | |
| //load state of rv | |
| if (photosViewModel?.listState != null) { | |
| binding.homePhotosList.layoutManager?.onRestoreInstanceState(photosViewModel?.listState) | |
| photosViewModel?.listState = null | |
| } | |
| } else { | |
| showSnackBarMessage() | |
| } | |
| } | |
| override fun onDestroyView() { | |
| photosViewModel?.listState = binding.homePhotosList.layoutManager?.onSaveInstanceState() | |
| super.onDestroyView() | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ViewModelProviders.of() is deprecated, then updated to ViewModelProvider()