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
Make use of reified type parameters for View DSL to allow factories installation #103
Comments
The |
Part of the prototype: import android.content.Context
import android.support.annotation.IdRes
import android.support.annotation.StyleRes
import android.view.View
import splitties.viewdsl.core.NO_THEME
import splitties.viewdsl.core.Ui
import splitties.viewdsl.core.wrapCtxIfNeeded
inline fun <reified V : View> Context.v2(
@IdRes id: Int = View.NO_ID,
@StyleRes theme: Int = NO_THEME,
initView: V.() -> Unit = {}
): V = V::class.java.getConstructor(Context::class.java).newInstance(wrapCtxIfNeeded(theme)).also {
it.id = id
}.apply(initView)
inline fun <reified V : View> View.v2(
@IdRes id: Int = View.NO_ID,
@StyleRes theme: Int = NO_THEME,
initView: V.() -> Unit = {}
) = context.v2(id, theme, initView)
inline fun <reified V : View> Ui.v2(
@IdRes id: Int = android.view.View.NO_ID,
@StyleRes theme: Int = NO_THEME,
initView: V.() -> Unit = {}
) = ctx.v2(id, theme, initView) |
Maybe add a second optional parameter for style. This could be used for different button styles, maybe also for text appearances and more. There would be a need to define a behavior when the style is not recognized BTW. Also, how to avoid errors where a style is applied to the wrong type while allowing extensibility? Maybe it could also be used with interface types? How would it work with interface types? |
The view factories could be put in the In fact, that's how the |
Any help on the API design is welcomeDiscussion is open |
We can avoid reflection (that is normally used in layout inflater) by having the factories use the real constructors directly instead. |
Released in 2.0.0-alpha6 (and also alpha5) |
Here's some inspiration: https://helw.net/2018/08/06/appcompat-view-inflation/
Here's how it could look:
v<TextView>(…) { … }
, and it'd check the type and delegate instantiation to installed factories. There could also be a way to scope View types delegates (not the same as Kotlin property delegates).The text was updated successfully, but these errors were encountered: