[Paris] No default style for view leads to IllegalStateException
See original GitHub issueHi,
this is more of a question, as the causes that lead to the exception are well documented here under the “Recycling Views” paragraph.
In the last few months we’ve been looking for an easy way to set styles/attrs to our views/models and the Paris integration just feels like a godsend!
There’s something we’re missing though.
Our typical view (annotated with @ ModelView) is a custom view which usually extends either ConstraintLayout or LinearLayout. More importantly, we never apply styles from styles.xml to it, so we don’t really have a “default” one. We just want to set different paddings each time.
The documentation says:
By default the view’s default style is used, per Paris’s default rules.
If no default style is provided, Paris is using a DEFAULT_PARIS_STYLE, which is basically an empty style generated this way:
private static final Style DEFAULT_PARIS_STYLE = new MyViewStyleApplier.StyleBuilder().addDefault().build();
The following code leads to an IllegalStateException thrown by the assertSameAttributes() method.
myRecyclerView.withModels {
myView {
id(MyView.TAG)
text(headerText)
styleBuilder { builder ->
builder.paddingBottomDp(10)
}
}
}
java.lang.IllegalStateException: MyViewModel_ model at position 0 has an invalid style:
The MyView style "EmptyStyle" is missing the following attributes:
✕ paddingBottom
It must declare the same attributes as the following styles:
a_programmatic_StyleBuilder
On the other hand, if I set a @ Style in MyView with option (isDefault = true) and then set paddings using styleBuilder, then everything works flawlessly.
So, my question is: is there any way we can just build the style using styleBuilder and avoid linking a default @ Style inside the view?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Thanks a lot !!! You’ve been very helpful and crystal-clear. Of course my intent was not to say that your approach was incorrect, but rather to understand whether I fully got the idea behind the recycling breaks.
@elihart Sure, I’ll be checking out https://github.com/airbnb/paris/pull/26 for updates! For now, I’ll define a @ Style(isDefault = true) with all the needed attributes and I’ll override values for them if I need to, similarly to what you said.
I also agree with @ngsilverman that views shouldn’t automatically inherit styles and I like the proposals, especially the 3rd one (“per style basis”).
Many thanks again, I really appreciate your support
I still don’t think that views should automatically inherit linked styles from their parents because styles might not be designed for inheritance. We could make it possible to enable that behavior though:
Or the other way around:
This could also be done on a per style basis:
And/or we could create this shortcut:
I like the
openStyles
/open
option best because it’s the minimal code change: set it on the base class and you’re set. It also lets view types decide for themselves whether their styles are meant to be inherited.