Refactor tag namespace to use value class
This commit is contained in:
1 parent
39b16b6650
commit
47be2754e5
6 files changed
+60
-37
No files matched your search
@@ -21,6 +21,7 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import com.hippo.ehviewer.EhApplication.Companion.ktorClient
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.StatusCodeException
|
||||
@@ -106,7 +107,7 @@ object EhTagDatabase : CoroutineScope {
|
||||
val prefix = keyword.dropLast(mKeyword.length)
|
||||
val namespace = mKeyword.substringBefore(':')
|
||||
val tagKeyword = mKeyword.drop(namespace.length + 1)
|
||||
val namespacePrefix = namespaceToPrefix(namespace) ?: namespace
|
||||
val namespacePrefix = TagNamespace(namespace).toPrefix() ?: namespace
|
||||
val tags = tagGroups[namespacePrefix.takeIf { tagKeyword.isNotEmpty() && it != NAMESPACE_PREFIX }]
|
||||
tags?.let {
|
||||
internalSuggestFlow(it, tagKeyword, translate, exactly).collect { (hint, tag) ->
|
||||
@@ -130,24 +131,6 @@ object EhTagDatabase : CoroutineScope {
|
||||
private fun String.containsIgnoreSpace(other: String, ignoreCase: Boolean = true): Boolean =
|
||||
removeSpace().contains(other.removeSpace(), ignoreCase)
|
||||
|
||||
private val NAMESPACE_TO_PREFIX = HashMap<String, String>().also {
|
||||
it["artist"] = "a"
|
||||
it["cosplayer"] = "cos"
|
||||
it["character"] = "c"
|
||||
it["female"] = "f"
|
||||
it["group"] = "g"
|
||||
it["language"] = "l"
|
||||
it["male"] = "m"
|
||||
it["mixed"] = "x"
|
||||
it["other"] = "o"
|
||||
it["parody"] = "p"
|
||||
it["reclass"] = "r"
|
||||
}
|
||||
|
||||
fun namespaceToPrefix(namespace: String): String? {
|
||||
return NAMESPACE_TO_PREFIX[namespace]
|
||||
}
|
||||
|
||||
private fun getMetadata(context: Context): Array<String>? {
|
||||
return context.resources.getStringArray(R.array.tag_translation_metadata)
|
||||
.takeIf { it.size == 4 }
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
@JvmInline
|
||||
value class TagNamespace(val value: String) {
|
||||
fun toPrefix(): String? = prefixMap[this]
|
||||
|
||||
companion object {
|
||||
val Artist = TagNamespace("artist")
|
||||
val Cosplayer = TagNamespace("cosplayer")
|
||||
val Character = TagNamespace("character")
|
||||
val Female = TagNamespace("female")
|
||||
val Group = TagNamespace("group")
|
||||
val Language = TagNamespace("language")
|
||||
val Male = TagNamespace("male")
|
||||
val Mixed = TagNamespace("mixed")
|
||||
val Other = TagNamespace("other")
|
||||
val Parody = TagNamespace("parody")
|
||||
val Reclass = TagNamespace("reclass")
|
||||
private val prefixMap = mapOf(
|
||||
Artist to "a",
|
||||
Cosplayer to "cos",
|
||||
Character to "c",
|
||||
Female to "f",
|
||||
Group to "g",
|
||||
Language to "l",
|
||||
Male to "m",
|
||||
Mixed to "x",
|
||||
Other to "o",
|
||||
Parody to "p",
|
||||
Reclass to "r",
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import com.hippo.ehviewer.client.EhTagDatabase
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.unifile.openInputStream
|
||||
import com.hippo.unifile.openOutputStream
|
||||
@@ -34,15 +34,18 @@ fun GalleryInfo.getComicInfo(): ComicInfo {
|
||||
val characters = mutableListOf<String>()
|
||||
val parodies = mutableListOf<String>()
|
||||
val tags = mutableListOf<String>()
|
||||
simpleTags?.forEach {
|
||||
val (namespace, tag) = it.split(':', limit = 2)
|
||||
when (namespace) {
|
||||
"artist", "cosplayer" -> artists.add(tag)
|
||||
"group" -> groups.add(tag)
|
||||
"character" -> characters.add(tag)
|
||||
"parody" -> if (tag != "original") parodies.add(tag)
|
||||
"language", "reclass" -> Unit
|
||||
else -> tags.add("${EhTagDatabase.namespaceToPrefix(namespace)}:$tag")
|
||||
simpleTags?.forEach { tagString ->
|
||||
val (namespace, tag) = tagString.split(':', limit = 2)
|
||||
with(TagNamespace) {
|
||||
when (val ns = TagNamespace(namespace)) {
|
||||
Artist, Cosplayer -> artists.add(tag)
|
||||
Group -> groups.add(tag)
|
||||
Character -> characters.add(tag)
|
||||
Parody -> if (tag != "original") parodies.add(tag)
|
||||
Other -> tags.add(tag)
|
||||
Female, Male, Mixed -> ns.toPrefix()?.let { tags.add("$it:$tag") }
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
return ComicInfo(
|
||||
|
||||
@@ -20,6 +20,7 @@ import androidx.compose.ui.unit.dp
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhTagDatabase
|
||||
import com.hippo.ehviewer.client.data.GalleryTagGroup
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.ehviewer.ui.tools.includeFontPadding
|
||||
|
||||
@Composable
|
||||
@@ -41,7 +42,7 @@ fun GalleryTags(
|
||||
text = tagGroup.groupName.translate(),
|
||||
isGroup = true,
|
||||
)
|
||||
val prefix = EhTagDatabase.namespaceToPrefix(tagGroup.groupName)
|
||||
val prefix = TagNamespace(tagGroup.groupName).toPrefix()
|
||||
FlowRow {
|
||||
tagGroup.forEach {
|
||||
val weak = it.startsWith('_')
|
||||
|
||||
@@ -98,6 +98,7 @@ import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
|
||||
import com.hippo.ehviewer.client.data.GalleryTagGroup
|
||||
import com.hippo.ehviewer.client.data.ListUrlBuilder
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.ehviewer.client.data.asGalleryDetail
|
||||
import com.hippo.ehviewer.client.data.findBaseInfo
|
||||
import com.hippo.ehviewer.client.exception.EhException
|
||||
@@ -200,10 +201,11 @@ private fun getRatingText(rating: Float): Int {
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<GalleryTagGroup>.getArtist(): String? {
|
||||
private fun List<GalleryTagGroup>.getArtistTag(): String? {
|
||||
val namespace = TagNamespace.Artist.value
|
||||
for (tagGroup in this) {
|
||||
if ("artist" == tagGroup.groupName && tagGroup.size > 0) {
|
||||
return tagGroup[0].removePrefix("_")
|
||||
if (tagGroup.groupName == namespace && tagGroup.size > 0) {
|
||||
return "$namespace:${tagGroup[0].removePrefix("_")}"
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -547,7 +549,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
|
||||
text = stringResource(id = R.string.similar_gallery),
|
||||
onClick = {
|
||||
val keyword = EhUtils.extractTitle(galleryDetail.title)
|
||||
val artist = galleryDetail.tags.getArtist()
|
||||
val artistTag = galleryDetail.tags.getArtistTag()
|
||||
if (null != keyword) {
|
||||
navigate(
|
||||
ListUrlBuilder(
|
||||
@@ -555,11 +557,11 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
|
||||
mKeyword = "\"" + keyword + "\"",
|
||||
).asDst(),
|
||||
)
|
||||
} else if (artist != null) {
|
||||
} else if (artistTag != null) {
|
||||
navigate(
|
||||
ListUrlBuilder(
|
||||
mode = ListUrlBuilder.MODE_TAG,
|
||||
mKeyword = "artist:$artist",
|
||||
mKeyword = artistTag,
|
||||
).asDst(),
|
||||
)
|
||||
} else if (null != galleryDetail.uploader) {
|
||||
|
||||
@@ -63,6 +63,7 @@ import com.hippo.ehviewer.EhApplication.Companion.searchDatabase
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhTagDatabase
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.ehviewer.dao.Search
|
||||
import com.hippo.ehviewer.dao.SearchDao
|
||||
import com.hippo.ehviewer.ui.LocalNavDrawerState
|
||||
@@ -323,7 +324,7 @@ fun wrapTagKeyword(keyword: String, translate: Boolean = false): String {
|
||||
val tag = keyword.substringAfter(':')
|
||||
val prefix = keyword.dropLast(tag.length + 1)
|
||||
if (translate) {
|
||||
val namespacePrefix = EhTagDatabase.namespaceToPrefix(prefix)
|
||||
val namespacePrefix = TagNamespace(prefix).toPrefix()
|
||||
val newPrefix = EhTagDatabase.getTranslation(tag = prefix) ?: prefix
|
||||
val newTag = EhTagDatabase.getTranslation(namespacePrefix, tag) ?: tag
|
||||
"$newPrefix:$newTag"
|
||||
|
||||
Reference in new issue
Block a user