-
-
Save programmerr47/58dd445ea93bedc6eed444a3af2f1400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("Recycle") | |
@UseExperimental(ExperimentalContracts::class) | |
inline fun AttributeSet.parseAttrs(context: Context, @StyleableRes attrs: IntArray, parser: TypedArray.() -> Unit) { | |
contract { callsInPlace(parser, InvocationKind.EXACTLY_ONCE) } | |
context.obtainStyledAttributes(this, attrs).use(parser) | |
} | |
@UseExperimental(ExperimentalContracts::class) | |
inline fun TypedArray.use(block: TypedArray.() -> Unit) { | |
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } | |
try { | |
block() | |
} finally { | |
recycle() | |
} | |
} | |
fun TypedArray.getResString(ctx: Context, index: Int): String? { | |
return if (hasValue(index)) { | |
getResourceId(index)?.let { ctx.getString(it) } | |
} else { | |
null | |
} | |
} | |
fun TypedArray.getResourceId(index: Int): Int? = getResourceId(index, 0).takeIf { it > 0 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment