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

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

Open
idrisadetunmbi opened this issue Apr 15, 2020 · 4 comments
Open

Comments

@idrisadetunmbi
Copy link

idrisadetunmbi commented Apr 15, 2020

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.

@ngsilverman
Copy link
Collaborator

@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.

@idrisadetunmbi
Copy link
Author

@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.

@langsmith
Copy link

@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?

@ngsilverman
Copy link
Collaborator

@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?

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

3 participants