diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryDetail.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryDetail.kt index 676f4d0f3..6354f5961 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryDetail.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryDetail.kt @@ -16,6 +16,7 @@ package com.hippo.ehviewer.client.data import android.os.Parcelable +import androidx.compose.ui.util.fastFlatMap import com.hippo.ehviewer.client.data.GalleryInfo.Companion.S_LANGS import kotlinx.parcelize.Parcelize @@ -51,7 +52,7 @@ data class GalleryDetail( var size: String? = null, var favoriteCount: Int = 0, var ratingCount: Int = 0, - val tags: List, + val tagGroups: List, var comments: GalleryCommentList, val previewPages: Int, val previewList: List, @@ -60,6 +61,6 @@ data class GalleryDetail( fun fillInfo() { val index = LANGUAGES.indexOf(language) if (index != -1) simpleLanguage = S_LANGS[index] - simpleTags = tags.flatten() + simpleTags = tagGroups.fastFlatMap(GalleryTagGroup::tags) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryTagGroup.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryTagGroup.kt index 77e60a8eb..32bda2efc 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryTagGroup.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/data/GalleryTagGroup.kt @@ -20,7 +20,6 @@ import kotlinx.parcelize.Parcelize @Parcelize data class GalleryTagGroup( - val groupName: String, - private val tagList: MutableList = mutableListOf(), -) : Parcelable, - MutableList by tagList + val name: String, + val tags: List, +) : Parcelable diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt index 1bed2d20b..aebcbebbb 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt @@ -51,7 +51,6 @@ import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element import org.jsoup.nodes.Node -import org.jsoup.select.Elements import org.jsoup.select.NodeTraversor import org.jsoup.select.NodeVisitor @@ -109,7 +108,7 @@ object GalleryDetailParser { PATTERN_ERROR.find(body)?.run { throw EhException(groupValues[1]) } val document = Jsoup.parse(body) val galleryDetail = GalleryDetail( - tags = parseTagGroups(document), + tagGroups = parseTagGroups(document), comments = parseComments(document), previewPages = parsePreviewPages(body), previewList = parsePreviewList(body).first, @@ -279,25 +278,21 @@ object GalleryDetailParser { } } - private fun parseTagGroup(element: Element): GalleryTagGroup? = Either.catch { - var nameSpace = element.child(0).text() - // Remove last ':' - nameSpace = nameSpace.substring(0, nameSpace.length - 1) - val group = GalleryTagGroup(nameSpace) - val tags = element.child(1).children() - tags.forEach { - var tag = it.text() - // Sometimes parody tag is followed with '|' and english translate, just remove them - val index = tag.indexOf('|') - if (index >= 0) { - tag = tag.substring(0, index).trim() - } - if (it.className() == "gtw") { - tag = "_$tag" // weak tag - } - group.add(tag) + private fun parseSingleTagGroup(element: Element): GalleryTagGroup? = Either.catch { + val nameSpace = element.child(0).text().run { + // Remove last ':' + substring(0, length - 1) } - group.takeIf { it.isNotEmpty() } + val tags = element.child(1).children().map { e -> + val text = e.text() + // Sometimes parody tag is followed with '|' and english translate, just remove them + val index = text.indexOf('|') + val tag = if (index >= 0) text.substring(0, index).trim() else text + // weak tag + if (e.className() == "gtw") "_$tag" else tag + } + check(tags.isNotEmpty()) { "TagGroup is empty!" } + GalleryTagGroup(nameSpace, tags) }.getOrElse { logcat(it) null @@ -308,15 +303,7 @@ object GalleryDetailParser { */ private fun parseTagGroups(document: Document): List = Either.catch { val taglist = document.getElementById("taglist")!! - val tagGroups = taglist.child(0).child(0).children() - parseTagGroups(tagGroups) - }.getOrElse { - logcat(it) - emptyList() - } - - private fun parseTagGroups(trs: Elements): List = Either.catch { - trs.mapNotNull { parseTagGroup(it) } + taglist.child(0).child(0).children().mapNotNull(::parseSingleTagGroup) }.getOrElse { logcat(it) emptyList() diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt index 4d7b8c98f..bc388bf2b 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt @@ -42,9 +42,9 @@ fun GalleryInfo.getComicInfo(): ComicInfo { val otherTags = mutableListOf() with(TagNamespace) { when (this@getComicInfo) { - is GalleryDetail -> tags.forEach { tagList -> - val list = tagList.filterNot { it == TAG_ORIGINAL || it.startsWith('_') } - when (val ns = TagNamespace(tagList.groupName)) { + is GalleryDetail -> tagGroups.forEach { group -> + val list = group.tags.filterNot { it == TAG_ORIGINAL || it.startsWith('_') } + when (val ns = TagNamespace(group.name)) { Artist, Cosplayer -> artists.addAll(list) Group -> groups.addAll(list) Character -> characters.addAll(list) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryTag.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryTag.kt index c74057d30..939cc4902 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryTag.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/main/GalleryTag.kt @@ -39,16 +39,16 @@ fun GalleryTags( tagGroups.forEach { tagGroup -> Row { BaseRoundText( - text = tagGroup.groupName.translate(), + text = tagGroup.name.translate(), isGroup = true, ) - val prefix = TagNamespace(tagGroup.groupName).toPrefix() + val prefix = TagNamespace(tagGroup.name).toPrefix() FlowRow { - tagGroup.forEach { + tagGroup.tags.forEach { val weak = it.startsWith('_') val real = it.removePrefix("_") val translated = real.translate(prefix) - val tag = tagGroup.groupName + ":" + real + val tag = tagGroup.name + ":" + real val hapticFeedback = LocalHapticFeedback.current BaseRoundText( text = translated, diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt index dd0560bca..4a471d622 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailContent.kt @@ -499,7 +499,7 @@ fun BelowHeader(galleryDetail: GalleryDetail) { text = stringResource(id = R.string.similar_gallery), onClick = { val keyword = EhUtils.extractTitle(galleryDetail.title) - val artistTag = galleryDetail.tags.getArtistTag() + val artistTag = galleryDetail.tagGroups.getArtistTag() if (null != keyword) { navigate( ListUrlBuilder( @@ -643,7 +643,7 @@ fun BelowHeader(galleryDetail: GalleryDetail) { } } Spacer(modifier = Modifier.size(keylineMargin)) - val tags = galleryDetail.tags + val tags = galleryDetail.tagGroups if (tags.isEmpty()) { Box( modifier = Modifier.fillMaxWidth(), @@ -723,11 +723,9 @@ private fun getRatingText(rating: Float): Int = when ((rating * 2).roundToInt()) private fun List.getArtistTag(): String? { for (tagGroup in this) { - if (tagGroup.isNotEmpty()) { - val namespace = tagGroup.groupName - if (namespace == TagNamespace.Artist.value || namespace == TagNamespace.Cosplayer.value) { - return "$namespace:${tagGroup[0].removePrefix("_")}" - } + val namespace = tagGroup.name + if (namespace == TagNamespace.Artist.value || namespace == TagNamespace.Cosplayer.value) { + return "$namespace:${tagGroup.tags[0].removePrefix("_")}" } } return null