Skip to content

Commit

Permalink
Bug fix, use content (parser html to text) and as fallback contentText
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiebeler committed Feb 9, 2025
1 parent 7c3b4bc commit 3bb7063
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ data class PostContextDto(
@SerialName("descendants") val descendants: List<PostDto>,
) : DtoInterface<PostContext> {
override fun toModel(): PostContext {
val descendants = descendants.map { post -> post.toModel() }
return PostContext(ancestors = ancestors.map { post -> post.toModel() },
descendants = descendants.map { post -> post.toModel() })
descendants = descendants)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ data class PostDto(
account = reblog.account.toModel(),
tags = reblog.tags?.map { it.toModel() }.orEmpty(),
favouritesCount = reblog.favouritesCount ?: 0,
content = reblog.contentText?.let { it.takeIf { it.isNotEmpty() } }
?: reblog.content?.let { htmlToText(it) } ?: "",
content = reblog.content?.let { htmlToText(it) } ?: reblog.contentText?.let { it.takeIf { it.isNotEmpty() } } ?: "",
replyCount = reblog.replyCount ?: 0,
createdAt = reblog.createdAt ?: "",
url = reblog.url ?: "",
Expand All @@ -83,8 +82,7 @@ data class PostDto(
account = account.toModel(),
tags = tags?.map { it.toModel() }.orEmpty(),
favouritesCount = favouritesCount ?: 0,
content = contentText?.let { it.takeIf { it.isNotEmpty() } }
?: content?.let { htmlToText(it) } ?: "",
content = content?.let { htmlToText(it) } ?: contentText?.let { it.takeIf { it.isNotEmpty() } } ?: "",
replyCount = replyCount ?: 0,
createdAt = createdAt ?: "",
url = url ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.daniebeler.pfpixelix.data.remote.dto


import com.daniebeler.pfpixelix.domain.model.Reply
import com.daniebeler.pfpixelix.utils.HtmlToText.htmlToText
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
Expand Down Expand Up @@ -51,7 +52,7 @@ data class ReplyDto(
override fun toModel(): Reply {
return Reply(
id = id,
content = contentText,
content = content?.let { htmlToText(it) } ?: contentText,
mentions = mentions.map { accountDto -> accountDto.toModel() },
account = account.toModel(),
createdAt = createdAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
package com.daniebeler.pfpixelix.domain.usecase

import co.touchlab.kermit.Logger
import com.daniebeler.pfpixelix.common.Resource
import com.daniebeler.pfpixelix.domain.model.PostContext
import com.daniebeler.pfpixelix.domain.repository.CountryRepository
import com.fleeksoft.ksoup.Ksoup
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import me.tatarka.inject.annotations.Inject

@Inject
class GetRepliesUseCase(
private val repository: CountryRepository
) {
operator fun invoke(postId: String): Flow<Resource<PostContext>> = flow{
repository.getReplies(postId).collect { result ->
if (result is Resource.Success && result.data != null) {
var postContext: PostContext = result.data
postContext = postContext.copy(descendants = postContext.descendants.map {reply ->
val updatedReply = reply.copy(content = htmlToText(reply.content))
updatedReply
})
emit(Resource.Success(postContext))
} else {
emit(result)
}
}
}

private fun htmlToText(html: String): String {
val text = Ksoup.parse(html).text()
Logger.d("htmlToText") { text }
return text
operator fun invoke(postId: String): Flow<Resource<PostContext>> {
return repository.getReplies(postId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ object HtmlToText {
val cleanedText = text.lines().joinToString("\n") { it.trimStart() } // Trim leading spaces

Logger.d("htmlToText") { cleanedText }
return cleanedText.trim()
val finsihedText = cleanedText.trim()
return finsihedText
}

}

0 comments on commit 3bb7063

Please sign in to comment.