all: move Markdown rendering to common module

This commit is contained in:
Harsh Shandilya 2021-10-25 01:15:56 +05:30
parent 36618690e3
commit b3588e97e7
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
6 changed files with 13 additions and 14 deletions

View file

@ -17,6 +17,7 @@ kotlin {
api(compose.material)
api(projects.database)
api(projects.model)
implementation(libs.multiplatform.markdown)
}
}
sourceSets["androidMain"].apply {

View file

@ -17,6 +17,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.mikepenz.markdown.Markdown
import dev.msfjarvis.claw.common.posts.PostDetails
import dev.msfjarvis.claw.common.posts.SubmitterName
import dev.msfjarvis.claw.common.posts.toDbModel
@ -26,6 +27,7 @@ import dev.msfjarvis.claw.model.LobstersPostDetails
@Composable
fun CommentsHeader(
postDetails: LobstersPostDetails,
htmlToMarkdown: (html: String) -> String,
) {
Surface {
Column(
@ -35,6 +37,7 @@ fun CommentsHeader(
PostDetails(
post = postDetails.toDbModel(),
)
Markdown(htmlToMarkdown(postDetails.description))
}
}
}
@ -42,7 +45,7 @@ fun CommentsHeader(
@Composable
fun CommentEntry(
comment: Comment,
renderMarkdown: @Composable (comment: String, modifier: Modifier) -> Unit,
htmlToMarkdown: (html: String) -> String,
) {
val indentLevel = comment.indentLevel.toInt() - 1
@ -55,7 +58,7 @@ fun CommentEntry(
avatarUrl = "https://lobste.rs/${comment.user.avatarUrl}",
contentDescription = "Submitted by ${comment.user.username}",
)
renderMarkdown(comment.comment, Modifier.padding(top = 8.dp))
Markdown(htmlToMarkdown(comment.comment), Modifier.padding(top = 8.dp))
}
}
}

View file

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