Make GalleryTagGroup immutable & non-empty (#1798)
* Make GalleryTagGroup immutable & non-empty * Refine --------- Co-authored-by: Dude so hot <djohn@fbi.gov>
This commit is contained in:
1 parent
40d9bfa30b
commit
e843a547a5
6 files changed
+34
-49
No files matched your search
@@ -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<GalleryTagGroup>,
|
||||
val tagGroups: List<GalleryTagGroup>,
|
||||
var comments: GalleryCommentList,
|
||||
val previewPages: Int,
|
||||
val previewList: List<GalleryPreview>,
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class GalleryTagGroup(
|
||||
val groupName: String,
|
||||
private val tagList: MutableList<String> = mutableListOf(),
|
||||
) : Parcelable,
|
||||
MutableList<String> by tagList
|
||||
val name: String,
|
||||
val tags: List<String>,
|
||||
) : Parcelable
|
||||
@@ -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<GalleryTagGroup> = 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<GalleryTagGroup> = Either.catch {
|
||||
trs.mapNotNull { parseTagGroup(it) }
|
||||
taglist.child(0).child(0).children().mapNotNull(::parseSingleTagGroup)
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
emptyList()
|
||||
|
||||
@@ -42,9 +42,9 @@ fun GalleryInfo.getComicInfo(): ComicInfo {
|
||||
val otherTags = mutableListOf<String>()
|
||||
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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<GalleryTagGroup>.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
|
||||
|
||||
Reference in new issue
Block a user