This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

In this article I have introduced RV(in this article refers to RecyclerView) a convenient class ListAdapter, but what does it have to do with declarative? In this article we’ll take a look at the details of code and get a feel for declarative programming in code.

Yes, the operational part of the RV in version 1 to version 2 code is actually an imperative to declarative transformation.

details

In the second version of the code, I first changed all the attributes of the data class Todo to val.

data class Todo(val id:int,val content:String,val done = false)
Copy the code

This means that you can modify any attribute of a Todo element in TodoList without having to rebuild a new object. Fortunately, Kotlin’s data class has a very handy function called copy. For example, if you want to change the completed state, you can do so with this code.

val todo2 = todo1.copy(done = true)
Copy the code

In addition, the imperative code is the mutable list passed in through the Adapter constructor, and all operations are modifications to this mutable list, with modifications to the data followed by a UI refresh to the RV. You can make mandatory changes to the data along with changes to the UI, which makes it easy to add coupling.

In a declarative form, all you want to do is pass the new modified data to the submitList function. The difference is calculated by DiffUtil.itemCallback, and the ListAdapter performs the specific behavior based on the difference. In fact, immutable lists and immutable properties also serve this process.

object diffCallback : DiffUtil.ItemCallback<Todo>() {
        override fun areItemsTheSame(oldItem: Todo, newItem: Todo): Boolean
            = oldItem.id == newItem.id

        override fun areContentsTheSame(oldItem: Todo, newItem: Todo): Boolean 
            = oldItem.content == newItem.content
              && oldItem.done == newItem.done
}
Copy the code

Now to explain diffCallback: The areItemsTheSame function compares whether items in the same location are consistent. If their contents are inconsistent, moves or additions or deletions will be triggered. The areContentsTheSame function compares whether items in the same location are consistent.

conclusion

In the declarative, any change in the RV comes from two pieces of data. To be precise, it comes from the difference between the two data points. You just have to deal with the differences you want, and the rest will be handled by a powerful framework.

That’s what a lot of people say, imperative is when you tell the program what it should do, declarative is when you tell the program what you want.

You might say that modifying an item is a waste of space by creating an almost identical object. I’ll shoot you in the head and ask you, do you want a project that is easy to maintain iteratively, or do you want a project that looks painful but saves resources?