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

Pattern for data binding #822

Closed
AllanWang opened this issue Sep 15, 2019 · 6 comments
Closed

Pattern for data binding #822

AllanWang opened this issue Sep 15, 2019 · 6 comments

Comments

@AllanWang
Copy link
Contributor

Not an issue, but just wanted to post this to help with others using data binding. If it proves to be useful, it could also be added as another submodule.

The general contents are here, but the idea is that with data binding, we can get the view ids from the layout id, and we no longer need to make a custom ViewHolder. This also avoids the need for non generic adapters.

ViewHolders however are useful for cases like event hooks, where we need to distinguish between the types. This binding pattern uses a companion object to keep track of that layout res instead.

Usage example:

class PlaceholderVhBinding(@StringRes override val data: Int) :
    BindingItem<ViewPlaceholderBinding>(data), 
    BindingLayout<ViewPlaceholderBinding> by Companion {

    override fun ViewPlaceholderBinding.bindView(holder: ViewHolder, payloads: MutableList<Any>) {
        placeholderText.setText(data)
    }

    override fun ViewPlaceholderBinding.unbindView(holder: ViewHolder) {
        placeholderText.text = null
    }

    companion object : BindingLayout<ViewPlaceholderBinding> {

        override val layoutRes: Int
            get() = R.layout.view_placeholder

    }
}

Binding and unbinding signatures extend the data binding, so view access is easy, and the model can be accessed from data. So long as it is a data class, equality and hash are also defined, so diffs work out of the box.

Id type defaults to layout res, though that is a person design decision.

@mikepenz
Copy link
Owner

mikepenz commented Oct 2, 2019

@AllanWang have you had a look at ViewBinding as it may make this obsolete too?

@AllanWang
Copy link
Contributor Author

This pattern is actually pretty generic, so long as there is a way to get the binding from the layout res. I probably won't be looking at ViewBinding until it makes it to AS Beta, but there is a chance it will still work. If all they expose is the inflate method, I would be pretty disappointed

@AllanWang
Copy link
Contributor Author

This can probably be closed, as it's really here in case people search for it and want information.

I'm testing integration with ViewBinding here:

https://github.com/AllanWang/KAU/blob/viewbinding/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt

Essentially, it seems like we can get the view information from the binding instead of a custom viewholder, and if we implement equality through the use of data classes for models, we get diffs out of the box too. All that is left is to use a single IItem model. It is annoying that you have to provide the binding yourself, whereas with data binding there is a generic loader that can take in the layoutRes you provide.

In this case, the layoutRes is a bit redundant, but is still used to validate event hooks.

@mikepenz
Copy link
Owner

Perhaps you would be interested in doing a sample activity in the sample app to showcase this?

if we somehow can do that more generic or so and pack it, then we could also set-up a new additional extension :)

@AllanWang
Copy link
Contributor Author

I'm in the process of testing now. It could be even more generic by supporting any class with a getView function, but I think at a certain point it defeats the purpose

mikepenz added a commit that referenced this issue Jan 30, 2020
…usage with viewbinding / databinding

- update to AS 4.0.0-alpha09 for viewbinding / databinding
- add compileOnly dependency for viewBinding interface
- use iconItem from viewBinding in the sample app
- FIX / REGARDING #822
@mikepenz
Copy link
Owner

Please have a look at the new data binding module :) perhaps this suites all the needs

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

No branches or pull requests

2 participants