common: extract htmlToMarkdown passing style to a composition local

This commit is contained in:
Harsh Shandilya 2021-11-15 23:05:47 +05:30
parent b3fda724ad
commit 4622162d09
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
6 changed files with 50 additions and 13 deletions

View file

@ -23,8 +23,8 @@ import dev.msfjarvis.claw.model.LobstersPostDetails
@Composable
fun CommentsHeader(
postDetails: LobstersPostDetails,
htmlToMarkdown: (html: String) -> String,
) {
val htmlConverter = LocalHTMLConverter.current
Surface {
Column(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 4.dp).fillMaxWidth(),
@ -33,7 +33,7 @@ fun CommentsHeader(
PostDetails(
post = postDetails.toDbModel(),
)
MaterialRichText { Markdown(htmlToMarkdown(postDetails.description)) }
MaterialRichText { Markdown(htmlConverter.convertHTMLToMarkdown(postDetails.description)) }
}
}
}
@ -41,8 +41,8 @@ fun CommentsHeader(
@Composable
fun CommentEntry(
comment: Comment,
htmlToMarkdown: (html: String) -> String,
) {
val htmlConverter = LocalHTMLConverter.current
Divider(color = Color.Gray.copy(0.4f))
Row(modifier = Modifier.wrapContentHeight()) {
Column(modifier = Modifier.padding(start = 12.dp, end = 8.dp, top = 4.dp, bottom = 4.dp)) {
@ -52,7 +52,7 @@ fun CommentEntry(
contentDescription = "User avatar for ${comment.user.username}",
)
MaterialRichText(modifier = Modifier.padding(top = 8.dp)) {
Markdown(htmlToMarkdown(comment.comment))
Markdown(htmlConverter.convertHTMLToMarkdown(comment.comment))
}
}
}

View file

@ -28,15 +28,14 @@ import dev.msfjarvis.lobsters.ui.comments.NetworkState
@Composable
private fun CommentsPageInternal(
details: LobstersPostDetails,
htmlToMarkdown: (html: String) -> String,
bottomPadding: Dp,
) {
LazyColumn(Modifier.padding(bottom = bottomPadding)) {
item { CommentsHeader(postDetails = details, htmlToMarkdown = htmlToMarkdown) }
item { CommentsHeader(postDetails = details) }
item { Spacer(modifier = Modifier.height(8.dp)) }
items(details.comments) { item -> CommentEntry(item, htmlToMarkdown) }
items(details.comments) { item -> CommentEntry(item) }
item { Divider(color = Color.Gray.copy(0.4f)) }
}
@ -47,7 +46,6 @@ private fun CommentsPageInternal(
fun CommentsPage(
postId: String,
getDetails: suspend (String) -> LobstersPostDetails,
htmlToMarkdown: (html: String) -> String,
paddingValues: PaddingValues,
) {
var postDetails: NetworkState by remember { mutableStateOf(NetworkState.Loading) }
@ -58,7 +56,6 @@ fun CommentsPage(
is NetworkState.Success<*> -> {
CommentsPageInternal(
(postDetails as NetworkState.Success<LobstersPostDetails>).data,
htmlToMarkdown,
paddingValues.calculateBottomPadding(),
)
}

View file

@ -0,0 +1,10 @@
package dev.msfjarvis.claw.common.comments
import androidx.compose.runtime.staticCompositionLocalOf
/** Defines a contract to convert strings of HTML to Markdown. */
fun interface HTMLConverter {
fun convertHTMLToMarkdown(html: String): String
}
val LocalHTMLConverter = staticCompositionLocalOf<HTMLConverter> { error("To be provided") }