Anvil fails silently when you set a property on a view that won't accept it
- Lingua principale
- Java
- Stelle
- 1.4k
- Fork
- 89
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Hi!
I've spent a while debugging this issue and I think I found the root cause.
### The problem
Setting a property on a view that doesn't accept that property fails silently.
### Example code
```
drawerLayout {
textView {
text("content")
}
textView {
text("drawer")
layoutGravity(LEFT)
}
}
```
the second child of `drawerLayout` will not have its `layout_gravity` set to `LEFT` because in `BaseDSL.java` in `set`,
```
case "layoutGravity":
if (v.getLayoutParams() instanceof LinearLayout.LayoutParams && value instanceof Integer) {
((LinearLayout.LayoutParams) v.getLayoutParams()).gravity = (int) value;
return true;
} else if (v.getLayoutParams() instanceof FrameLayout.LayoutParams && value instanceof Integer) {
((FrameLayout.LayoutParams) v.getLayoutParams()).gravity = (int) value;
return true;
}
break;
```
`v.getLayoutParams()` doesn't return layout params belonging to a linear layout nor a frame layout (they belong to a drawer layout).
### The fix
If the program reaches one of these control statements but does not pass the tests laid out in the if statements, the user should be notified (by throwing an exception, logging a warning, or something)
### Related but tangential grumblings
#### Relevant to Anvil
There are definitely more things than `LinearLayout` and `FrameLayout` that can have `layout_gravity`. Those things include `TextView` as eg. above and the root of my issue, `DrawerLayout` which if it doesn't have a second child with `layout_gravity` will not work properly.
#### Not relevant to Anvil but I'm frustrated and I'll say them anyway
Getting to the bottom of this issue has been frustrating and taken more time than I like, but I mostly blame the Android framework for pushing their constraints (ordering of child views, setting layout gravity) onto consumers of the framework. I should not have to set those things in order to make a drawer. See Leaky Abstraction.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.