Skip to content

Commit 8b028e5

Browse files
feat: html renderer
1 parent 3d3a732 commit 8b028e5

File tree

4 files changed

+318
-168
lines changed

4 files changed

+318
-168
lines changed

core/src/main/java/org/openedx/core/ui/ComposeCommon.kt

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.openedx.core.ui
22

3-
import android.os.Build
4-
import android.os.Build.VERSION.SDK_INT
5-
import android.webkit.WebView
63
import androidx.compose.foundation.BorderStroke
74
import androidx.compose.foundation.ExperimentalFoundationApi
85
import androidx.compose.foundation.background
@@ -20,7 +17,6 @@ import androidx.compose.foundation.layout.Spacer
2017
import androidx.compose.foundation.layout.fillMaxSize
2118
import androidx.compose.foundation.layout.fillMaxWidth
2219
import androidx.compose.foundation.layout.height
23-
import androidx.compose.foundation.layout.heightIn
2420
import androidx.compose.foundation.layout.padding
2521
import androidx.compose.foundation.layout.size
2622
import androidx.compose.foundation.layout.width
@@ -82,7 +78,6 @@ import androidx.compose.ui.graphics.painter.Painter
8278
import androidx.compose.ui.graphics.vector.ImageVector
8379
import androidx.compose.ui.graphics.vector.rememberVectorPainter
8480
import androidx.compose.ui.input.pointer.pointerInput
85-
import androidx.compose.ui.layout.ContentScale
8681
import androidx.compose.ui.platform.LocalContext
8782
import androidx.compose.ui.platform.LocalFocusManager
8883
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
@@ -106,17 +101,11 @@ import androidx.compose.ui.tooling.preview.Preview
106101
import androidx.compose.ui.unit.Dp
107102
import androidx.compose.ui.unit.TextUnit
108103
import androidx.compose.ui.unit.dp
109-
import androidx.compose.ui.viewinterop.AndroidView
110104
import androidx.compose.ui.zIndex
111-
import coil.ImageLoader
112-
import coil.compose.AsyncImage
113-
import coil.decode.GifDecoder
114-
import coil.decode.ImageDecoderDecoder
115105
import kotlinx.coroutines.launch
116106
import org.openedx.core.NoContentScreenType
117107
import org.openedx.core.R
118108
import org.openedx.core.domain.model.RegistrationField
119-
import org.openedx.core.extension.LinkedImageText
120109
import org.openedx.core.presentation.global.ErrorType
121110
import org.openedx.core.ui.theme.OpenEdXTheme
122111
import org.openedx.core.ui.theme.appColors
@@ -510,131 +499,6 @@ fun HyperlinkText(
510499
)
511500
}
512501

513-
@Composable
514-
fun HyperlinkImageText(
515-
modifier: Modifier = Modifier,
516-
title: String = "",
517-
imageText: LinkedImageText,
518-
textStyle: TextStyle = TextStyle.Default,
519-
linkTextColor: Color = MaterialTheme.appColors.primary,
520-
linkTextFontWeight: FontWeight = FontWeight.Normal,
521-
linkTextDecoration: TextDecoration = TextDecoration.None,
522-
fontSize: TextUnit = TextUnit.Unspecified,
523-
) {
524-
val fullText = imageText.text
525-
val hyperLinks = imageText.links
526-
val annotatedString = buildAnnotatedString {
527-
if (title.isNotEmpty()) {
528-
append(title)
529-
append("\n\n")
530-
}
531-
append(fullText)
532-
addStyle(
533-
style = SpanStyle(
534-
color = MaterialTheme.appColors.textPrimary,
535-
fontSize = fontSize
536-
),
537-
start = 0,
538-
end = this.length
539-
)
540-
541-
for ((key, value) in hyperLinks) {
542-
val startIndex = this.toString().indexOf(key)
543-
if (startIndex == -1) continue
544-
val endIndex = startIndex + key.length
545-
addStyle(
546-
style = SpanStyle(
547-
color = linkTextColor,
548-
fontSize = fontSize,
549-
fontWeight = linkTextFontWeight,
550-
textDecoration = linkTextDecoration
551-
),
552-
start = startIndex,
553-
end = endIndex
554-
)
555-
addStringAnnotation(
556-
tag = "URL",
557-
annotation = value,
558-
start = startIndex,
559-
end = endIndex
560-
)
561-
}
562-
if (title.isNotEmpty()) {
563-
addStyle(
564-
style = SpanStyle(
565-
color = MaterialTheme.appColors.textPrimary,
566-
fontSize = MaterialTheme.appTypography.titleLarge.fontSize,
567-
fontWeight = MaterialTheme.appTypography.titleLarge.fontWeight
568-
),
569-
start = 0,
570-
end = title.length
571-
)
572-
}
573-
for (item in imageText.headers) {
574-
val startIndex = this.toString().indexOf(item)
575-
if (startIndex == -1) continue
576-
val endIndex = startIndex + item.length
577-
addStyle(
578-
style = SpanStyle(
579-
color = MaterialTheme.appColors.textPrimary,
580-
fontSize = MaterialTheme.appTypography.titleLarge.fontSize,
581-
fontWeight = MaterialTheme.appTypography.titleLarge.fontWeight
582-
),
583-
start = startIndex,
584-
end = endIndex
585-
)
586-
}
587-
addStyle(
588-
style = SpanStyle(
589-
fontSize = fontSize
590-
),
591-
start = 0,
592-
end = this.length
593-
)
594-
}
595-
596-
val uriHandler = LocalUriHandler.current
597-
val context = LocalContext.current
598-
val imageLoader = ImageLoader.Builder(context)
599-
.components {
600-
if (SDK_INT >= Build.VERSION_CODES.P) {
601-
add(ImageDecoderDecoder.Factory())
602-
} else {
603-
add(GifDecoder.Factory())
604-
}
605-
}
606-
.build()
607-
608-
Column(Modifier.fillMaxWidth()) {
609-
BasicText(
610-
text = annotatedString,
611-
modifier = modifier.pointerInput(Unit) {
612-
detectTapGestures { offset ->
613-
val position = offset.x.toInt()
614-
annotatedString.getStringAnnotations("URL", position, position)
615-
.firstOrNull()?.let { stringAnnotation ->
616-
uriHandler.openUri(stringAnnotation.item)
617-
}
618-
}
619-
},
620-
style = textStyle
621-
)
622-
imageText.imageLinks.values.forEach {
623-
Spacer(Modifier.height(8.dp))
624-
AsyncImage(
625-
modifier = Modifier
626-
.fillMaxWidth()
627-
.heightIn(0.dp, 360.dp),
628-
contentScale = ContentScale.Fit,
629-
model = it,
630-
contentDescription = null,
631-
imageLoader = imageLoader
632-
)
633-
}
634-
Spacer(Modifier.height(16.dp))
635-
}
636-
}
637-
638502
@Composable
639503
fun SheetContent(
640504
searchValue: TextFieldValue,
@@ -1406,20 +1270,6 @@ private fun RoundTab(
14061270
}
14071271
}
14081272

1409-
@Composable
1410-
fun HtmlTextView(htmlContent: String, modifier: Modifier = Modifier) {
1411-
AndroidView(
1412-
modifier = modifier,
1413-
factory = { context ->
1414-
WebView(context).apply {
1415-
settings.javaScriptEnabled = false
1416-
settings.loadsImagesAutomatically = true
1417-
loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null)
1418-
}
1419-
},
1420-
)
1421-
}
1422-
14231273
@Preview
14241274
@Composable
14251275
private fun StaticSearchBarPreview() {

0 commit comments

Comments
 (0)