Improves badge display

This commit is contained in:
Vitor Pamplona
2024-08-12 11:18:22 -04:00
parent 2cface9ac9
commit 75efd2b372

View File

@@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
@@ -40,6 +41,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@@ -51,6 +53,7 @@ import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.get import androidx.core.graphics.get
@@ -64,6 +67,7 @@ import com.vitorpamplona.amethyst.ui.note.ClickableUserPicture
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.Size35dp import com.vitorpamplona.amethyst.ui.theme.Size35dp
import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonRow
import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink import com.vitorpamplona.amethyst.ui.theme.mediumImportanceLink
import com.vitorpamplona.quartz.events.BadgeAwardEvent import com.vitorpamplona.quartz.events.BadgeAwardEvent
import com.vitorpamplona.quartz.events.BadgeDefinitionEvent import com.vitorpamplona.quartz.events.BadgeDefinitionEvent
@@ -72,13 +76,14 @@ import kotlinx.coroutines.launch
@Composable @Composable
fun BadgeDisplay(baseNote: Note) { fun BadgeDisplay(baseNote: Note) {
val observingNote by baseNote.live().metadata.observeAsState()
val badgeData = observingNote?.note?.event as? BadgeDefinitionEvent ?: return
val image = badgeData.image()
val name = badgeData.name()
val description = badgeData.description()
val background = MaterialTheme.colorScheme.background val background = MaterialTheme.colorScheme.background
val badgeData = baseNote.event as? BadgeDefinitionEvent ?: return
val image = remember { badgeData.thumb()?.ifBlank { null } ?: badgeData.image() }
val name = remember { badgeData.name() }
val description = remember { badgeData.description() }
var backgroundFromImage by remember { mutableStateOf(Pair(background, background)) } var backgroundFromImage by remember { mutableStateOf(Pair(background, background)) }
var imageResult by remember { mutableStateOf<SuccessResult?>(null) } var imageResult by remember { mutableStateOf<SuccessResult?>(null) }
@@ -103,26 +108,32 @@ fun BadgeDisplay(baseNote: Note) {
} }
} }
Row( RenderBadge(
modifier = image,
Modifier name,
.padding(10.dp) backgroundFromImage.first,
.clip(shape = CutCornerShape(20, 20, 20, 20)) backgroundFromImage.second,
.border( description,
5.dp,
MaterialTheme.colorScheme.mediumImportanceLink,
CutCornerShape(20),
).background(backgroundFromImage.first),
) { ) {
if (imageResult == null) {
imageResult = it.result
}
}
}
@Preview
@Composable
private fun RenderBadgePreview() {
val background = MaterialTheme.colorScheme.background
ThemeComparisonRow {
RenderBadge( RenderBadge(
image, image = "http://test.com",
name, name = "Name",
backgroundFromImage.second, backgroundForRow = background,
description, backgroundFromImage = Color.LightGray,
description = "This badge is awarded to the dedicated individuals who actively contributed by writing events to the relay during the crucial testing phase leading up to the first beta release of Grain.",
) { ) {
if (imageResult == null) {
imageResult = it.result
}
} }
} }
} }
@@ -131,51 +142,65 @@ fun BadgeDisplay(baseNote: Note) {
private fun RenderBadge( private fun RenderBadge(
image: String?, image: String?,
name: String?, name: String?,
backgroundForRow: Color,
backgroundFromImage: Color, backgroundFromImage: Color,
description: String?, description: String?,
onSuccess: (AsyncImagePainter.State.Success) -> Unit, onSuccess: (AsyncImagePainter.State.Success) -> Unit,
) { ) {
Column { Row(
image.let { modifier =
AsyncImage( Modifier
model = it, .padding(10.dp)
contentDescription = .clip(shape = CutCornerShape(20, 20, 20, 20))
stringRes( .aspectRatio(0.8f)
R.string.badge_award_image_for, .border(
name ?: "", 5.dp,
), MaterialTheme.colorScheme.mediumImportanceLink,
contentScale = ContentScale.FillWidth, CutCornerShape(20),
modifier = Modifier.fillMaxWidth(), ).background(backgroundForRow),
onSuccess = onSuccess, ) {
) Column {
} image?.let {
AsyncImage(
model = it,
contentDescription =
stringRes(
R.string.badge_award_image_for,
name ?: "",
),
modifier = Modifier.weight(1f),
contentScale = ContentScale.FillWidth,
onSuccess = onSuccess,
)
}
name?.let { name?.let {
Text( Text(
text = it, text = it,
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = modifier =
Modifier Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(start = 10.dp, end = 10.dp), .padding(start = 10.dp, end = 10.dp),
color = backgroundFromImage, color = backgroundFromImage,
) )
} }
description?.let { description?.let {
Text( Text(
text = it, text = it,
style = MaterialTheme.typography.bodySmall, style = MaterialTheme.typography.bodySmall,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = modifier =
Modifier Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(start = 10.dp, end = 10.dp, bottom = 10.dp), .padding(start = 20.dp, end = 20.dp, bottom = 10.dp),
color = Color.Gray, color = Color.Gray,
maxLines = 3, maxLines = 3,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
) )
}
} }
} }
} }