Skip to content

Commit

Permalink
Merge pull request #38 from kongwoojin/feature/use-custom-html-view
Browse files Browse the repository at this point in the history
Create custom HTML view
  • Loading branch information
kongwoojin authored May 3, 2024
2 parents b6a6904 + f9374fa commit 6a3fff0
Show file tree
Hide file tree
Showing 12 changed files with 830 additions and 79 deletions.
10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dependencies {
kapt(libs.androidx.room.compiler)

implementation(libs.coil)
implementation(libs.coil.compose)
implementation(libs.okhttp)
implementation(libs.okhttp.logging.interceptor)
implementation(libs.retrofit)
Expand Down
20 changes: 0 additions & 20 deletions app/release/output-metadata.json

This file was deleted.

40 changes: 24 additions & 16 deletions app/src/main/java/com/kongjak/koreatechboard/ui/article/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.kongjak.koreatechboard.ui.components.FileText
import com.kongjak.koreatechboard.ui.components.HtmlText
import com.kongjak.koreatechboard.ui.components.HtmlView
import com.kongjak.koreatechboard.ui.components.WebView
import com.kongjak.koreatechboard.ui.theme.articleSubText
import com.kongjak.koreatechboard.ui.theme.articleTitle
Expand Down Expand Up @@ -101,21 +105,25 @@ fun ArticleScreen(
}
}

if (it.content.contains("table")) {
WebView(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
html = it.content
)
} else {
HtmlText(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
html = it.content
)
}
HtmlView(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp),
html = it.content,
image = { url, description ->
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(url)
.crossfade(true)
.build(),
contentDescription = description,
contentScale = ContentScale.FillWidth
)
},
webView = { html ->
WebView(html = html)
}
)

FileText(
modifier = Modifier.padding(16.dp),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.kongjak.koreatechboard.ui.components

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit

@Composable
fun CustomClickableText(
text: AnnotatedString,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
minLines: Int = 1,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
onClick: (Int) -> Unit
) {
val textColor = color.takeOrElse {
style.color.takeOrElse {
LocalContentColor.current
}
}

val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }
val pressIndicator = Modifier.pointerInput(onClick) {
detectTapGestures { pos ->
layoutResult.value?.let { layoutResult ->
onClick(layoutResult.getOffsetForPosition(pos))
}
}
}

SelectionContainer {
BasicText(
text = text,
modifier = modifier.then(pressIndicator),
style = style.merge(
color = textColor,
fontSize = fontSize,
fontWeight = fontWeight,
textAlign = textAlign ?: TextAlign.Unspecified,
lineHeight = lineHeight,
fontFamily = fontFamily,
textDecoration = textDecoration,
fontStyle = fontStyle,
letterSpacing = letterSpacing
),
onTextLayout = {
layoutResult.value = it
onTextLayout(it)
},
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
minLines = minLines,
inlineContent = inlineContent
)
}
}

This file was deleted.

Loading

0 comments on commit 6a3fff0

Please sign in to comment.