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

Handing model binding with payloads #1349

Open
cyberneticbytedreamer opened this issue May 13, 2023 · 0 comments
Open

Handing model binding with payloads #1349

cyberneticbytedreamer opened this issue May 13, 2023 · 0 comments

Comments

@cyberneticbytedreamer
Copy link

Hello, I'm reaching out to inquire about the correctness of my implementation for handling model binding with payloads in the Epoxy library. I fully understand, in such use-case, using the concept of payloads may not provide significant efficiency benefits. However, I'm exploring this library and I want just understand fundamental concepts, including working with payloads.

And the most important question for me, should I call super.bind(view, previouslyBoundModel) when payloads is empty?

@EpoxyModelClass
abstract class SampleModel : EpoxyModel<View>() {
    companion object {
        private const val FIRST_CHANGED = 0
        private const val SECOND_CHANGED = 1
        private const val THIRD_CHANGED = 2
    }

    @EpoxyAttribute
    lateinit var first: Int

    @EpoxyAttribute
    lateinit  var second: Int

    @EpoxyAttribute
    lateinit  var third: Int

    override fun getDefaultLayout() = R.layout.item_sample

    override fun bind(view: View, previouslyBoundModel: EpoxyModel<*>) {
        if (previouslyBoundModel !is SampleModel)
            return super.bind(view, previouslyBoundModel)

        val payloads = mutableListOf<Any>()

        if (first != previouslyBoundModel.first)
            payloads.add(FIRST_CHANGED)

        if (second != previouslyBoundModel.second)
            payloads.add(SECOND_CHANGED)

        if (third != previouslyBoundModel.third)
            payloads.add(THIRD_CHANGED)

        if (payloads.isNotEmpty())
            bind(view, payloads)
    }

    override fun bind(view: View, payloads: MutableList<Any>) {
        if (payloads.contains(FIRST_CHANGED))
            view.first.text = first.toString()

        if (payloads.contains(SECOND_CHANGED))
            view.second.text = second.toString()

        if (payloads.contains(THIRD_CHANGED))
            view.third.text = third.toString()
    }

    override fun bind(view: View) {
        view.first.text = first.toString()
        view.second.text = second.toString()
        view.third.text = third.toString()
    }
}
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

1 participant