-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Support for LayoutContainer #46
Comments
Hi, @emmano I'd love to make this library Kotlin friendly and this could be just a really nice feature to add to the project. About this issue you mention, this feature could be helpful for the renderers implementation invoking However, the usage cache you mention working on |
This is true, but since most Kotlin users are just going to use synthetic properties (similarly to how you use fun render() {
title.text = getContent().title
} That will basically break the |
It could be nice if we have this feature. My proposal is to implement this in two steps. The first step is simple. Update the documentation explaining how to implement The second step is sending the PR. We need to create the Kotlin module. Implement the feature. Configure the module to be deployed to maven central as the root module is configured. We should also add a linter and testing support for this new Kotlin module. What do you think @emmano, would you like to collaborate sending these two pull requests? |
Maybe @Serchinastico can help us with the pull request review 👍 |
Sure. The first one should be simple. The second one will take more time. I am also looking into issue #45. |
Thank you so much @emmano 😃 |
So, it looks like in order to get this to work (without library changes) the following needs to happen. Renderers will have to take a val rootView = layoutInflater.inflate(R.layout.comic_item, recycler, false)
val rendererBuilder = RendererBuilder<Comic>(ComicRenderer(rootView)) This is required since the public interface LayoutContainer {
/** Returns the root holder view. */
public val containerView: View?
} I think it makes the most sense to satisfy the dependency via a constructor argument as follows: class ComicRenderer(override val containerView: View) : Renderer<Comic>(), LayoutContainer {
override fun inflate(inflater: LayoutInflater, parent: ViewGroup?): View = containerView
override fun hookListeners(rootView: View?) = Unit
override fun setUpView(rootView: View?) = Unit
override fun render() {
comic_title.text = content.title
}
} I know it does not look great, but it will be temporary until the library officially supports |
Only the actual class that implements `LayoutContainer` can benefit from `View` caching. I think it will be left to the users of the library to correctly implement `LayoutContainer` in their `Renderers`. I can still send you a PR with an updated README that shows how to do it (from the comment above). |
Hi @emmano, I think passing the view in the constructor will break some uses of the library. Keep in mind that renderers are cloned under the hood, I'm not sure how that will work as we expect it to. Besides, with the new API change we wouldn't need the layout inflater no more and we'd be requiring renderer constructor to know how to build views. I'd go for a new abstract class KRenderer<T>: Renderer<T>(), LayoutContainer {
override lateinit var containerView: View
override fun setUpView(rootView: View?) {
containerView = rootView!!
}
} I don't know if that's worth a module, thought. We can just add a new section to the README for Kotlin users. Note: Please, do not call it |
Yeah, I did not like that either. I just wanted to keep the contract as it is.(i.e. keeping I agree that this change does not grant a module and updating the README should be enough. Implementers would do something like this: class ComicRenderer : Renderer<Comic>(), LayoutContainer {
override lateinit var containerView: View
override fun inflate(inflater: LayoutInflater, parent: ViewGroup?): View =
inflater.inflate(R.layout.comic_item, parent, false)
override fun hookListeners(rootView: View?) = Unit
override fun setUpView(rootView: View?) {
containerView = rootView!!
}
override fun render() {
comic_title.text = content.title
}
} If you agree I will work on the README file and submit a pull request. |
@emmano I think this solution should be enough and should work for everyone 😃 If you want to send a PR adding this information to the |
As it turns out, Kotlin synthetic properties cache works by default on
Activities
,Fragments
, andViews
, but it does not do so forViewHolder
. They fixed this as of Kotlin 1.1.4 viaLayoutContainer
.Consumers of this libraries have to make sure they make their
Renderers
implementLayoutContainer
if they are using synthetic properties inside of them. If they do not, theirViewHolders
are going to callfindViewById()
on every call torender()
making theirViewHolders
obsolete. It would be nice if the library itself did this maybe by adding a Kotlin specific artifact that implements this interface (implementation should be trivial). Thoughts?The text was updated successfully, but these errors were encountered: