Skip to content

Commit

Permalink
DText parser: Remove unused type parameter from MessageData
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroBrine1st committed Jan 10, 2024
1 parent 9dcc80a commit ded82eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
30 changes: 15 additions & 15 deletions app/src/main/java/ru/herobrine1st/e621/api/BBCodeProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ val ITALIC = SpanStyle(
const val WIKI_PAGE_STRING_ANNOTATION_TAG = "WIKI_PAGE"

@Immutable
sealed interface MessageData<T : MessageData<T>> {
sealed interface MessageData {
fun isEmpty(): Boolean
}

Expand All @@ -59,7 +59,7 @@ sealed interface MessageData<T : MessageData<T>> {
@Immutable
data class MessageText(
val text: AnnotatedString,
) : MessageData<MessageText> {
) : MessageData {

constructor(text: String) : this(AnnotatedString(text))

Expand All @@ -76,14 +76,14 @@ data class MessageText(
@Immutable
data class MessageQuote(
val author: Author?,
val data: List<MessageData<*>>
) : MessageData<MessageQuote> {
val data: List<MessageData>,
) : MessageData {
override fun isEmpty(): Boolean = data.isEmpty()

@Immutable
data class Author(
val userName: String,
val userId: Int
val userId: Int,
)
}

Expand All @@ -97,10 +97,10 @@ private sealed interface DTextTag {
* @param data list of parsed [MessageData]
* @return styled list of [MessageData]
*/
fun stylize(data: List<MessageData<*>>): List<MessageData<*>>
fun stylize(data: List<MessageData>): List<MessageData>

sealed class Styled(override val name: String, private val style: SpanStyle) : DTextTag {
override fun stylize(data: List<MessageData<*>>): List<MessageData<*>> {
override fun stylize(data: List<MessageData>): List<MessageData> {
return data.map {
(it as? MessageText)?.text?.let { text ->
MessageText(AnnotatedString.Builder().apply {
Expand All @@ -119,7 +119,7 @@ private sealed interface DTextTag {
data object Quote : DTextTag {
override val name: String = "quote"

override fun stylize(data: List<MessageData<*>>): List<MessageData<*>> {
override fun stylize(data: List<MessageData>): List<MessageData> {
val collapsed = data.collapseMessageTexts().toMutableList()
val first = collapsed[0]
val author: MessageQuote.Author?
Expand All @@ -143,7 +143,7 @@ private sealed interface DTextTag {
val commaAttribute: String,
val property: String
) : DTextTag {
override fun stylize(data: List<MessageData<*>>): List<MessageData<*>> {
override fun stylize(data: List<MessageData>): List<MessageData> {
val startingTag =
"[$name" +
(if (commaAttribute.isNotBlank()) ",$commaAttribute" else "") +
Expand Down Expand Up @@ -178,7 +178,7 @@ private sealed interface DTextTag {
else -> Unknown(name, commaAttribute, property)
}

fun DTextTag?.stylizeNullable(data: List<MessageData<*>>) = this?.stylize(data) ?: data
fun DTextTag?.stylizeNullable(data: List<MessageData>) = this?.stylize(data) ?: data
}
}

Expand All @@ -197,7 +197,7 @@ val pattern =
// name said:
val quotePattern = Regex("""(?:"?([^"\n]+)"?:/user(?:s|/show)/(\d+)|(\S+)) said:\r?\n""")

fun parseBBCode(input: String): List<MessageData<*>> {
fun parseBBCode(input: String): List<MessageData> {
val (parsed, end) = parseBBCodeInternal(input, null, 0)
assert(end == input.length) {
"Parser hasn't reached end of string: expected ${input.length}, actual $end"
Expand All @@ -209,9 +209,9 @@ fun parseBBCode(input: String): List<MessageData<*>> {
private fun parseBBCodeInternal(
input: String,
currentTag: DTextTag?,
initialStart: Int
): Pair<List<MessageData<*>>, Int> {
val output = mutableListOf<MessageData<*>>()
initialStart: Int,
): Pair<List<MessageData>, Int> {
val output = mutableListOf<MessageData>()
var start = initialStart

var tagClosed = false
Expand Down Expand Up @@ -293,7 +293,7 @@ private fun parseQuoteHeader(input: String): Pair<MessageQuote.Author, Int>? {
*
* @return Optimized list of [MessageData]
*/
private fun List<MessageData<*>>.collapseMessageTexts(): List<MessageData<*>> {
private fun List<MessageData>.collapseMessageTexts(): List<MessageData> {
// FAST PATH: Short comment, maybe with one quote or picture.
if (this.size < 2) return this
.filter { (it as? MessageText)?.text?.isNotEmpty() != false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data class CommentData(
val editTime: Instant,
val score: Int,
val isHidden: Boolean,
val message: List<MessageData<*>>
val message: List<MessageData>,
) {
companion object {
fun fromE621Comment(comment: CommentBB, authorAvatarPost: PostReduced?) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ sealed interface WikiState {
@Serializable
class Success(val result: WikiPage) : WikiState {
@Transient // it is a cache
lateinit var parsed: List<MessageData<*>>
lateinit var parsed: List<MessageData>
private set

fun setParsed(v: List<MessageData<*>>) {
fun setParsed(v: List<MessageData>) {
if (::parsed.isInitialized) throw RuntimeException("This property is write-once")
parsed = v
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun RenderBB(text: String, onWikiLinkClick: ((Tag) -> Unit)? = null) {
}

@Composable
fun RenderBB(data: List<MessageData<*>>, onWikiLinkClick: ((Tag) -> Unit)? = null) {
fun RenderBB(data: List<MessageData>, onWikiLinkClick: ((Tag) -> Unit)? = null) {
Column {
data.forEach {
RenderBB(it, onWikiLinkClick = onWikiLinkClick)
Expand All @@ -80,7 +80,7 @@ fun RenderBB(data: List<MessageData<*>>, onWikiLinkClick: ((Tag) -> Unit)? = nul
}

@Composable
fun RenderBB(data: MessageData<*>, onWikiLinkClick: ((Tag) -> Unit)? = null) {
fun RenderBB(data: MessageData, onWikiLinkClick: ((Tag) -> Unit)? = null) {
when (data) {
is MessageQuote -> {
data.author?.let {
Expand Down

0 comments on commit ded82eb

Please sign in to comment.