chao
05/25/2021, 5:26 PM@Composable
function with return type? I am trying to evaluate the feasibility to write JVM unit tests for them:
@Composable
fun spanStyleRange(textAttribute: TextAttribute): AnnotatedString.Range<SpanStyle> {
val spanStyle: SpanStyle = when (textAttribute.type) {
TextAttributeType.HASHTAG ->
SpanStyle(color = MaterialTheme.colors.primary, fontWeight = FontWeight.Bold)
else -> SpanStyle()
}
return AnnotatedString.Range(
item = spanStyle,
start = textAttribute.start,
end = textAttribute.start + textAttribute.length
)
}
In thie example, spanStyleRange
need to be annotated with @Composable
because it is using MaterialTheme.colors
Daniel
05/25/2021, 5:43 PMremember
) should follow standard naming conventions.
https://github.com/androidx/androidx/blob/androidx-main/compose/docs/compose-api-guidelines.md#naming-composable-functions-that-return-valueschao
05/25/2021, 5:55 PMDaniel
05/25/2021, 6:01 PMDaniel
05/25/2021, 6:01 PMDaniel
05/25/2021, 6:02 PMchao
05/25/2021, 6:24 PMAdam Powell
05/25/2021, 6:33 PM@Composable
overload that accepts a MaterialColors
as a parameter and does the heavy lifting. That way you can more easily use and test the non-composable version with all of the logic, but the composable version without the parameter can be used as a convenience.Adam Powell
05/25/2021, 6:35 PMAdam Powell
05/25/2021, 6:36 PMchao
05/25/2021, 6:45 PMchao
05/25/2021, 6:57 PMImageBitmap.Companion.imageResource
which is a top-level extension. It feels quite awkward to extract a non-@Composable
function to accept (@DrawableRes id) -> Unit
as a parameter.