Using room and paging library with MVVM. What i am trying to achieve is to call retrofit to get some data for every visible item in recycler view and then update the view accordingly. For now what i have achieved is to just implement the paging library and show data accordingly.
What i can do is to get the pagedlist and get data for whole list and update the view Or i can call api for every list item. We don't want the user to wait a lot to see the updated data on the visible item
class AllPetsFragment : Fragment() {
private lateinit var binding : FragmentAllPetsfragmentBinding
private lateinit var linearLayoutManager: LinearLayoutManager
private lateinit var allPetsViewModel: MainActivityViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentAllPetsfragmentBinding.inflate(inflater, container, false)
val view : View = binding.root
allPetsViewModel = ViewModelProvider(requireActivity()).get(MainActivityViewModel::class.java)
binding.btnOpenAddPet.setOnClickListener{
startActivity(Intent(activity, AddPetActivity::class.java))
}
val adapter = PetAdapter(requireContext(), ClickListener {
allPetsViewModel.updateData(it)
val action = AllPetsFragmentDirections.actionMainMap()
Navigation.findNavController(view).navigate(action)
})
linearLayoutManager = LinearLayoutManager(requireContext())
binding.petsRecycler.layoutManager = linearLayoutManager
binding.petsRecycler.adapter = adapter
subscribeUi(adapter)
return view
}
private fun subscribeUi(adapter: PetAdapter) {
allPetsViewModel.getDevicesLiveData().observe(viewLifecycleOwner, { names ->
if (names != null){
adapter.submitList(names)
}
})
}
}
class PetAdapter(val context: Context, private val cl: ClickListener) :
PagedListAdapter<Device, PetAdapter.PetViewHolder>(PersonDiffCallback()) {
override fun onBindViewHolder(holder: PetViewHolder, position: Int) {
val device = getItem(position)
if (device == null) {
holder.clear()
} else {
holder.bind(device, cl.clickListener)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PetViewHolder {
return PetViewHolder(LayoutInflater.from(context).inflate(R.layout.item_pet,
parent, false))
}
class PetViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var tvName: TextView = view.findViewById(R.id.petName)
var tvDistance: TextView = view.findViewById(R.id.totalDistance)
var txtBattery: TextView = view.findViewById(R.id.txtBattery)
var txtGeofence: TextView = view.findViewById(R.id.txtGeofence)
fun bind(device: Device, clickListener: (Device) -> Unit) {
tvName.text = device.name
itemView.setOnClickListener { clickListener(device) }
}
fun clear() {
tvName.text = null
}
}
override fun getCurrentList(): PagedList<Device>? {
return super.getCurrentList()
}
override fun onCurrentListChanged(currentList: PagedList<Device>?) {
Log.d("ListSize", currentList?.size.toString())
super.onCurrentListChanged(currentList)
}
}
class PersonDiffCallback : DiffUtil.ItemCallback<Device>() {
override fun areItemsTheSame(oldItem: Device, newItem: Device): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Device, newItem: Device): Boolean {
return oldItem == newItem;
}
}