Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active August 28, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenquadros/32753e61bcce7d1ce3af89d830b5a97c to your computer and use it in GitHub Desktop.
Save rubenquadros/32753e61bcce7d1ce3af89d830b5a97c to your computer and use it in GitHub Desktop.
Game description and show more/less toggle
@Composable
fun GameDescription(description: String) {
val maxLines = remember { mutableStateOf(4) }
val toggle = remember {
mutableStateOf(DescriptionStatus.DEFAULT)
}
Column {
// Desctiption text
Text(
modifier =
Modifier.getDetailsModifier()
.testTag("Description"),
text = description,
overflow = TextOverflow.Ellipsis,
maxLines = maxLines.value,
onTextLayout = {
if (it.lineCount == 4 &&
it.isLineEllipsized(3)
) {
// game description has overflowed maxLines
// show Show More
toggle.value =
DescriptionStatus.SHOW_MORE
} else if (it.lineCount > 4) {
// showing entire description
// show Show Less
toggle.value =
DescriptionStatus.SHOW_LESS
} else {
// game description has not overflowed maxLines
// do not show Show More at all
}
}
)
when (toggle.value) {
DescriptionStatus.SHOW_MORE -> {
// display show more
Text(
modifier =
Modifier.padding(
start = 16.dp,
end = 16.dp
)
.clickable {
maxLines.value =
Int.MAX_VALUE
},
text = "Show More",
color = Color(0xFFF50057),
textDecoration =
TextDecoration.Underline,
)
}
DescriptionStatus.SHOW_LESS -> {
// display show less
Text(
modifier =
Modifier.padding(
start = 16.dp,
end = 16.dp
)
.clickable {
maxLines.value = 4
},
text = "Show Less",
color = Color(0xFFF50057),
textDecoration =
TextDecoration.Underline,
)
}
else -> {
// show more is not displayed at all
// do not do anything
}
}
}
}
enum class DescriptionStatus {
DEFAULT,
SHOW_MORE,
SHOW_LESS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment