Skip to content

Commit 8550dce

Browse files
authored
[Jetcaster] Handling of HTML Elements in PodcastSummary on Foldable Devices (#1339)
There are cases where the Summary Text does not contain HTML elements. In these instances, it is considered that the performance overhead is negligible, therefore no additional processing was implemented | before | after | | ------- | -----| | ![HTML-TAG](https://github.com/android/compose-samples/assets/38935359/83b5df55-702b-4b8b-994f-aaecc0f4ec23)|![html-refactor](https://github.com/android/compose-samples/assets/38935359/a1efd824-497f-467b-891b-56bd62b46025)|
2 parents e2adb20 + 5a82b73 commit 8550dce

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

Jetcaster/app/src/main/java/com/example/jetcaster/ui/player/PlayerScreen.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import androidx.compose.foundation.layout.systemBarsPadding
4141
import androidx.compose.foundation.layout.windowInsetsPadding
4242
import androidx.compose.foundation.layout.wrapContentSize
4343
import androidx.compose.foundation.rememberScrollState
44+
import androidx.compose.foundation.text.selection.SelectionContainer
4445
import androidx.compose.foundation.verticalScroll
4546
import androidx.compose.material.icons.Icons
4647
import androidx.compose.material.icons.automirrored.filled.ArrowBack
@@ -55,6 +56,7 @@ import androidx.compose.material.icons.rounded.PlayCircleFilled
5556
import androidx.compose.material3.CircularProgressIndicator
5657
import androidx.compose.material3.Icon
5758
import androidx.compose.material3.IconButton
59+
import androidx.compose.material3.LocalContentColor
5860
import androidx.compose.material3.MaterialTheme
5961
import androidx.compose.material3.Slider
6062
import androidx.compose.material3.Surface
@@ -73,11 +75,13 @@ import androidx.compose.ui.semantics.Role
7375
import androidx.compose.ui.semantics.role
7476
import androidx.compose.ui.semantics.semantics
7577
import androidx.compose.ui.text.TextStyle
78+
import androidx.compose.ui.text.buildAnnotatedString
7679
import androidx.compose.ui.text.style.TextOverflow
7780
import androidx.compose.ui.tooling.preview.Devices
7881
import androidx.compose.ui.tooling.preview.Preview
7982
import androidx.compose.ui.unit.Dp
8083
import androidx.compose.ui.unit.dp
84+
import androidx.core.text.HtmlCompat
8185
import androidx.hilt.navigation.compose.hiltViewModel
8286
import androidx.window.core.layout.WindowSizeClass
8387
import androidx.window.core.layout.WindowWidthSizeClass
@@ -486,19 +490,16 @@ private fun PlayerContentBookStart(
486490
.fillMaxSize()
487491
.verticalScroll(rememberScrollState())
488492
.padding(
489-
vertical = 8.dp,
493+
vertical = 40.dp,
490494
horizontal = 16.dp
491495
),
492496
horizontalAlignment = Alignment.CenterHorizontally,
493-
verticalArrangement = Arrangement.SpaceAround
494497
) {
495-
Spacer(modifier = Modifier.height(32.dp))
496498
PodcastInformation(
497-
episode.title,
498-
episode.podcastName,
499-
episode.summary
499+
title = episode.title,
500+
name = episode.podcastName,
501+
summary = episode.summary,
500502
)
501-
Spacer(modifier = Modifier.height(32.dp))
502503
}
503504
}
504505

@@ -618,32 +619,32 @@ private fun PodcastInformation(
618619
title: String,
619620
name: String,
620621
summary: String,
622+
modifier: Modifier = Modifier,
621623
titleTextStyle: TextStyle = MaterialTheme.typography.headlineSmall,
622624
nameTextStyle: TextStyle = MaterialTheme.typography.displaySmall,
623625
) {
624626
Column(
627+
modifier = modifier.padding(horizontal = 8.dp),
628+
verticalArrangement = Arrangement.spacedBy(32.dp),
625629
horizontalAlignment = Alignment.CenterHorizontally,
626-
modifier = Modifier.padding(horizontal = 8.dp)
627630
) {
628631
Text(
629632
text = name,
630633
style = nameTextStyle,
631634
maxLines = 1,
632635
overflow = TextOverflow.Ellipsis
633636
)
634-
Spacer(modifier = Modifier.height(32.dp))
635637
Text(
636638
text = title,
637639
style = titleTextStyle,
638640
maxLines = 1,
639641
overflow = TextOverflow.Ellipsis
640642
)
641-
Spacer(modifier = Modifier.height(32.dp))
642-
Text(
643+
HtmlText(
643644
text = summary,
644645
style = MaterialTheme.typography.bodyMedium,
646+
color = LocalContentColor.current
645647
)
646-
Spacer(modifier = Modifier.weight(1f))
647648
}
648649
}
649650

@@ -776,6 +777,25 @@ private fun FullScreenLoading(modifier: Modifier = Modifier) {
776777
}
777778
}
778779

780+
@Composable
781+
private fun HtmlText(
782+
text: String,
783+
style: TextStyle,
784+
color: Color
785+
) {
786+
val annotationString = buildAnnotatedString {
787+
val htmlCompat = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_COMPACT)
788+
append(htmlCompat)
789+
}
790+
SelectionContainer {
791+
Text(
792+
text = annotationString,
793+
style = style,
794+
color = color
795+
)
796+
}
797+
}
798+
779799
@Preview
780800
@Composable
781801
fun TopAppBarPreview() {

0 commit comments

Comments
 (0)