Hello. I was unable to find a shortcut for `.withS...
# splitties
m
Hello. I was unable to find a shortcut for
.withStyledAttributes(attr) { getResourceId(it, 0) }
. Should I create a PR? None of the files below look suitable, should I create a new one? https://github.com/LouisCAD/Splitties/tree/8782cbb5d039a553ed2c5cf795eb53a86146ce91/modules/resources/src/androidMain/kotlin/splitties/resources
l
Hello, I got this need a while ago and I made several changes on the develop branch to make that work better. Please, open an issue so I remember to add a shorthand for that. I'll do my best to get this into a release this month. If you want, you can directly build it on top of the current
develop
branch and submit a PR.
m
Thanks,
resolveThemeAttribute
seems to be what I need.
l
IIRC, yes, because a TypedArray can contain several data, but we only care about the resourceId in our case. You would replace it with something else?
m
Short way to explain:
TypedArray.get*
accepts a
@StyleableRes
while styleable res values like
R.styleable.ViewClass_attrName
are just a 0-based int indices.
Long way. Given an array
int[] attrs { a, b, c }
,
obtainStyledAttrubites
will return a
TypedArray
which is effectively a
SparseIntArray
from
attrs.indices
to values. It is 'sparse' when some attributes are not specified and there's index-to-value-mapping. Hence, there are to way to read styled attributes:
Copy code
// get values specifying default ones for absent attributes
ta = obtain([a, b, c])
this.a = ta.get*(0, default);
this.b = ta.get*(1, default);
this.c = ta.get*(2, default);
and
Copy code
// get values only for present attributes
for (i in 0..ta.indexCount) {
    idx = ta.getIndex(i)
    when (idx) {
        0 -> this.a = ta.get*(0, whatever)
        1 -> this.b = ta.get*(1, dontMind)
        2 -> this.c = ta.get*(2, unused)
    }
}
Ooh, google , please, deprecate these cursed XMLs, especially styles.