From 47be2754e59dacec00f4e564461c834295c7ffc5 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 4 Mar 2024 23:08:36 +0800 Subject: [PATCH] Refactor tag namespace to use value class --- .../hippo/ehviewer/client/EhTagDatabase.kt | 21 ++---------- .../ehviewer/client/data/TagNamespace.kt | 33 +++++++++++++++++++ .../com/hippo/ehviewer/spider/ComicInfo.kt | 23 +++++++------ .../com/hippo/ehviewer/ui/main/GalleryTag.kt | 3 +- .../ehviewer/ui/screen/GalleryDetailScreen.kt | 14 ++++---- .../ehviewer/ui/screen/SearchBarScreen.kt | 3 +- 6 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 app/src/main/kotlin/com/hippo/ehviewer/client/data/TagNamespace.kt diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/EhTagDatabase.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/EhTagDatabase.kt index f360b0986..9af6cdb38 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/EhTagDatabase.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/EhTagDatabase.kt @@ -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().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? { return context.resources.getStringArray(R.array.tag_translation_metadata) .takeIf { it.size == 4 } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/data/TagNamespace.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/data/TagNamespace.kt new file mode 100644 index 000000000..681829358 --- /dev/null +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/data/TagNamespace.kt @@ -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", + ) + } +} 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 1fa517d19..5f0f2bcd4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/spider/ComicInfo.kt @@ -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() val parodies = mutableListOf() val tags = mutableListOf() - 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( 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 55e4dbc1b..c74057d30 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 @@ -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('_') diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt index dfd00f9f5..fd9f80c3e 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt @@ -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.getArtist(): String? { +private fun List.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) { diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/SearchBarScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/SearchBarScreen.kt index 4daa7b903..6bf372f3c 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/SearchBarScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/SearchBarScreen.kt @@ -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"