I am using sealed class with Android List Adaptor in which 2 different objects are merged into one single list. Last item in the list is just a add more layout(Like Header). I am using DiffUtils and noticed that even i cast newItem to Object1 it doesn't throw any class cast exception. For example : 1st there are 7 items(6 Object1 + 1 Add More) in Adaptor's currentList then i deleted 1 of the object let that be object with index 6.Now when diffUtil check for areItemsSame() then it doesn't throw an exception. As per my thinking oldItem at 6 will be Object1 since that item is deleted that means now the item at 6 index is Add More and now casting it to Object1 should throw a ClassCastException
My diff util code
override fun areContentsTheSame(oldItem: DataItem, newItem: DataItem): Boolean {
if (oldItem is DataItem.ItemToDoCat)
Log.v("Recycler","Old :${(oldItem).toDoCategory.category} ,New ${(newItem as DataItem.ItemToDoCat).toDoCategory.category}")
return when (oldItem) {
//Since i am only checking for oldItem, casting newItem should throw an Exception as per my example
is DataItem.ItemToDoCat -> (oldItem).toDoCategory == (newItem as DataItem.ItemToDoCat).toDoCategory
is DataItem.ItemAddPlaceHolder -> oldItem == newItem
}
}
Here is the log before adding test
2020-05-28 17:14:42.226 32588-1009/com.example.todomaster V/Recycler: Old :Default ,New Default
2020-05-28 17:14:42.226 32588-1009/com.example.todomaster V/Recycler: Old :Personal ,New Personal
2020-05-28 17:14:42.226 32588-1009/com.example.todomaster V/Recycler: Old :Work ,New Work
Log after adding test
2020-05-28 17:44:58.643 10717-10779/com.example.todomaster V/Recycler: Old :Default ,New Default
2020-05-28 17:44:58.644 10717-10779/com.example.todomaster V/Recycler: Old :Personal ,New Personal
2020-05-28 17:44:58.644 10717-10779/com.example.todomaster V/Recycler: Old :Work ,New Work
Log after deleting test
2020-05-28 17:51:33.116 10717-11005/com.example.todomaster V/Recycler: Old :Default ,New Default
2020-05-28 17:51:33.116 10717-11005/com.example.todomaster V/Recycler: Old :Personal ,New Personal
2020-05-28 17:51:33.116 10717-11005/com.example.todomaster V/Recycler: Old :Work ,New Work
As you can see Log doesn't even log oldList's test object after deleting which means oldList doesn't had an instance with test Object