Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable items property in AbsDelegationAdapter after 4.3.2. #110

Open
denisstarodubov opened this issue Aug 31, 2022 · 2 comments
Open

Comments

@denisstarodubov
Copy link

denisstarodubov commented Aug 31, 2022

Hi!
Release 4.3.2 added the @nullable annotation for the items property in the AbsDelegationAdapter class. In this regard, all the adapters in our project has been broken. All of them were built according to the following pattern:

internal class TestAdapter : ListDelegationAdapter<List<TestItem>>() {

    init {
        items = listOf()
        delegatesManager
            .addDelegate(TestDelegate())
    }

    fun setData(list: List<TestItem>) {
        val oldData = items
        items = list
        dispatchUpdates(oldData)
    }

    private fun dispatchUpdates(oldData: List<TestItem>) {
        Diffutil
            .calculateDiff(TestDiffCallback(items, oldData), false)
            .dispatchUpdatesTo(this)
    }
}

This approach has been used in several different projects that I have worked on.
After the update, this approach stopped working, since now we are operating on a nullable items list:

Screenshot 2022-08-31 at 14 42 23

I have not found an example in Readme how to use ListDelegationAdapter + DiffUtil when list is nullable.

It is possible to check for null each time or write requireNotNull(items) / items!!, but in this case a lot of adapters has to be rewritten.

Is there any correct way to work with ListDelegationAdapter + DiffUtil?

@sockeqwe
Copy link
Owner

sockeqwe commented Sep 1, 2022

Hello,
I feel your pain but the items list can defacto be null (so this was a flaw in previous versions of this library). We could discuss if it makes sense (and technically be possible) to make it late init instead of nullable.

But in your concrete example, isn't it enough to just dont run DiffUtils if oldItems is null.

fun setData(list: List<TestItem>) {
        val oldData = items
        items = list
        dispatchUpdates(oldData)
    }

    private fun dispatchUpdates(oldData: List<TestItem>?) { //OldData is nullable

        if (oldData ==null)
           this.notidyDatasetChanged()
       else
        Diffutil
            .calculateDiff(TestDiffCallback(items, oldData), false)
            .dispatchUpdatesTo(this)
    }

@denisstarodubov
Copy link
Author

Thank you for your answer!

I think it would be nice if you made items lateinit or somehow non null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants