airbnb / paris

Define and apply styles to Android views programmatically

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question/Opinion] Creating Reusable Components with Paris/Epoxy

idrisadetunmbi opened this issue · comments

This is not an issue with the library, I am just wondering what the best way is in creating reusable components using Paris/Epoxy. I created a simple TextView custom view as defined below:

@Styleable
@ModelView
class CustomText @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : MaterialTextView(context, attributeSet, defStyleAttr) {

    @ModelProp
    fun setValue(text: String?) {
        this.text = text
    }

    @ModelProp
    fun setValue(@StringRes textRes: Int) {
        this.text = context.getText(textRes)
    }

    companion object {
        @com.airbnb.paris.annotations.Style
        val DEFAULT_STYLE = R.style.AppTheme_Text
    }
}

The goal is to be able to reuse the model generated by this wherever a text is required in the app and override the default style by using the model's styleBuilder/style properties as required.

I have tried this across about 3 screens, but so far, it is not working as expected. The styles defined across different instances are getting mixed up .e.g. an instance that does not define a drawableLeft in its style override shows it (while it is defined in another instance).

@ngsilverman, please can you advise if this might not be right way to create such reusable components or if it is, what I might be doing wrong with the approach. Thank you.

@idrisadetunmbi it sounds like you're running into the issue described here: https://github.com/airbnb/paris/wiki/View-Recycling

The solution is to make sure all your styles define the same set of attributes. For example if one sets a drawableLeft and others should not show drawables, they should explicitly set the attribute to null.

@ngsilverman , thanks for responding quickly. Just to clarify further, in your experience, does it work to define a base style that new styles inherit from rather than explicitly defining all the attributes for each new style? I am currently doing this and still running into the issue and wanted to know if it is not the right way to do it.

@idrisadetunmbi , did you ever figure out a slick way to manage your styles?

define a base style that new styles inherit from rather than explicitly defining all the attributes for each new style? I am currently doing this and still running into the issue and wanted to know if it is not the right way to do it

I'm currently doing the same thing with a base style. Did you stick with this system?

@idrisadetunmbi yup using base styles should be possible. Are you sure all the styles used for a given view type are setting the same attributes? Could you share your style code?