How to add checkbox when using Android Paging Library & Room

312 views Asked by At

I'm using Android Paging Library + Room Library.

@Entity
data class Customer {
  @PrimaryKey(autoGererate = true) 
  val id: Long,
  val name: String
}

@Dao
interface CustomerDao {
 @Query("SELECT * FROM Customer")
 fun get(): DataSource.Factory<Int, Customer>
}

I'd like to checkbox in PagedListAdapter when user click customer item. And I'd like to provide all item check function in fragment. The checked item is stored in viewModel.

But, I don't want to use getItem(int: Position) and notifyItemChanged function like below.

// in fragment
viewModel.selected.observe(lifecyclerOwner, Observer {
  adapter.selectedCustomers = it
})

// in adapter
 var selectedCustomers: List<Customer>()
 set(value) {
    for (i in until itemCount) {
      getItem(i)?.id?.let {
         if (selected item id is changed) {
             notifyItemChanged(i)
         }
      }
   }
 }

That's because getItem call mPagedList.loadAround(index) in AsyncPagedListDiffer

Can I change Customer to CheckableCustomer and use DiffUtil.ItemCallback instead of notifyDataSetChanged?

  data class CheckableCustomer {
    val customer: Customer,
    val isChecked: Boolean
  }

How to change DataSource.Factory<Int, Customer> from Room db's dao to CheckableCustomer?

I want change Customer to CheckableCustomer and use PagedListAdapter<CheckableCustomer, VH>(callback)

Or, is there any good solution using room database datasource & checkbox?

0

There are 0 answers