From 55c7f6b8961bd60fcec2662b86e288e60d5f866d Mon Sep 17 00:00:00 2001
From: FooIbar <118464521+FooIbar@users.noreply.github.com>
Date: Wed, 14 Dec 2022 23:43:12 +0800
Subject: [PATCH] Support tag suggestion for all languages
---
.../hippo/ehviewer/client/EhTagDatabase.java | 28 ++++++++-----------
.../hippo/ehviewer/ui/fragment/EhFragment.kt | 2 +-
.../ehviewer/ui/scene/GalleryDetailScene.java | 2 +-
.../hippo/ehviewer/ui/scene/SearchBarScene.kt | 2 +-
app/src/main/res/values-zh-rCN/arrays.xml | 26 -----------------
app/src/main/res/values-zh-rCN/bools.xml | 23 +++++++++++++++
app/src/main/res/values-zh-rHK/arrays.xml | 26 -----------------
app/src/main/res/values-zh-rHK/bools.xml | 23 +++++++++++++++
app/src/main/res/values-zh-rTW/arrays.xml | 26 -----------------
app/src/main/res/values-zh-rTW/bools.xml | 23 +++++++++++++++
app/src/main/res/values/arrays.xml | 5 +++-
app/src/main/res/values/bools.xml | 23 +++++++++++++++
12 files changed, 111 insertions(+), 98 deletions(-)
delete mode 100644 app/src/main/res/values-zh-rCN/arrays.xml
create mode 100644 app/src/main/res/values-zh-rCN/bools.xml
delete mode 100644 app/src/main/res/values-zh-rHK/arrays.xml
create mode 100644 app/src/main/res/values-zh-rHK/bools.xml
delete mode 100644 app/src/main/res/values-zh-rTW/arrays.xml
create mode 100644 app/src/main/res/values-zh-rTW/bools.xml
create mode 100644 app/src/main/res/values/bools.xml
diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhTagDatabase.java b/app/src/main/java/com/hippo/ehviewer/client/EhTagDatabase.java
index 8bc741c67..87065311e 100644
--- a/app/src/main/java/com/hippo/ehviewer/client/EhTagDatabase.java
+++ b/app/src/main/java/com/hippo/ehviewer/client/EhTagDatabase.java
@@ -25,6 +25,7 @@ import androidx.core.util.Pair;
import com.hippo.ehviewer.AppConfig;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.R;
+import com.hippo.ehviewer.Settings;
import com.hippo.util.ExceptionUtils;
import com.hippo.util.IoThreadPoolExecutor;
import com.hippo.yorozuya.FileUtils;
@@ -100,13 +101,8 @@ public class EhTagDatabase {
}
@Nullable
- public static EhTagDatabase getInstance(Context context) {
- if (isPossible(context)) {
- return instance;
- } else {
- instance = null;
- return null;
- }
+ public static EhTagDatabase getInstance() {
+ return instance;
}
@Nullable
@@ -123,8 +119,8 @@ public class EhTagDatabase {
}
}
- public static boolean isPossible(Context context) {
- return getMetadata(context) != null;
+ public static boolean isTranslatable(Context context) {
+ return context.getResources().getBoolean(R.bool.tag_translatable);
}
@Nullable
@@ -196,9 +192,6 @@ public class EhTagDatabase {
return false;
}
ResponseBody body = response.body();
- if (body == null) {
- return false;
- }
try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
IOUtils.copy(is, os);
@@ -402,9 +395,12 @@ public class EhTagDatabase {
begin = start + end;
- byte[] hintBytes = new byte[end - middle - 1];
- System.arraycopy(tags, start + middle + 1, hintBytes, 0, end - middle - 1);
- String hint = new String(hintBytes, StandardCharsets.UTF_8);
+ String hint = null;
+ if (Settings.getShowTagTranslations()) {
+ byte[] hintBytes = new byte[end - middle - 1];
+ System.arraycopy(tags, start + middle + 1, hintBytes, 0, end - middle - 1);
+ hint = new String(hintBytes, StandardCharsets.UTF_8);
+ }
byte[] tagBytes = new byte[middle];
System.arraycopy(tags, start + 1, tagBytes, 0, middle);
String tag = new String(tagBytes, StandardCharsets.UTF_8);
@@ -431,6 +427,6 @@ public class EhTagDatabase {
}
private boolean containsIgnoreSpace(String text, String key) {
- return text.replace(" ", "").contains(key.replace(" ", ""));
+ return text != null && text.replace(" ", "").contains(key.replace(" ", ""));
}
}
diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/EhFragment.kt b/app/src/main/java/com/hippo/ehviewer/ui/fragment/EhFragment.kt
index 97e052b30..5cb4f1584 100644
--- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/EhFragment.kt
+++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/EhFragment.kt
@@ -47,7 +47,7 @@ class EhFragment : BasePreferenceFragment() {
thumbSize!!.onPreferenceChangeListener = this
showTagTranslations!!.onPreferenceChangeListener = this
blackDarkTheme!!.onPreferenceChangeListener = this
- if (!EhTagDatabase.isPossible(requireActivity())) {
+ if (!EhTagDatabase.isTranslatable(requireActivity())) {
preferenceScreen.removePreference(showTagTranslations)
preferenceScreen.removePreference(tagTranslationsSource!!)
}
diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java
index ade0f8747..8c4712b85 100755
--- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java
+++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java
@@ -927,7 +927,7 @@ public class GalleryDetailScene extends CollapsingToolbarScene implements View.O
mNoTags.setVisibility(View.GONE);
}
- EhTagDatabase ehTags = Settings.getShowTagTranslations() ? EhTagDatabase.getInstance(context) : null;
+ EhTagDatabase ehTags = Settings.getShowTagTranslations() ? EhTagDatabase.getInstance() : null;
int colorTag = ResourcesKt.resolveColor(getTheme(), R.attr.tagBackgroundColor);
int colorName = ResourcesKt.resolveColor(getTheme(), R.attr.tagGroupBackgroundColor);
for (GalleryTagGroup tg : tagGroups) {
diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt
index 96d14562c..67d17d027 100644
--- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt
+++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/SearchBarScene.kt
@@ -283,7 +283,7 @@ abstract class SearchBarScene : ToolbarScene() {
providerSuggestions(text)?.let { suggestions.addAll(it) }
}
mSearchDatabase.getSuggestions(text, 128).forEach { suggestions.add(KeywordSuggestion(it)) }
- val ehTagDatabase = EhTagDatabase.getInstance(context)
+ val ehTagDatabase = EhTagDatabase.getInstance()
if (!TextUtils.isEmpty(text) && ehTagDatabase != null && !text.endsWith(" ")) {
val s = text.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (s.isNotEmpty()) {
diff --git a/app/src/main/res/values-zh-rCN/arrays.xml b/app/src/main/res/values-zh-rCN/arrays.xml
deleted file mode 100644
index 621124fc6..000000000
--- a/app/src/main/res/values-zh-rCN/arrays.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-
-
-
- - tag-translations-zh-rCN.sha1
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN.sha1
- - tag-translations-zh-rCN
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN
-
-
-
diff --git a/app/src/main/res/values-zh-rCN/bools.xml b/app/src/main/res/values-zh-rCN/bools.xml
new file mode 100644
index 000000000..ab59f02a2
--- /dev/null
+++ b/app/src/main/res/values-zh-rCN/bools.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ true
+
+
diff --git a/app/src/main/res/values-zh-rHK/arrays.xml b/app/src/main/res/values-zh-rHK/arrays.xml
deleted file mode 100644
index 621124fc6..000000000
--- a/app/src/main/res/values-zh-rHK/arrays.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-
-
-
- - tag-translations-zh-rCN.sha1
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN.sha1
- - tag-translations-zh-rCN
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN
-
-
-
diff --git a/app/src/main/res/values-zh-rHK/bools.xml b/app/src/main/res/values-zh-rHK/bools.xml
new file mode 100644
index 000000000..ab59f02a2
--- /dev/null
+++ b/app/src/main/res/values-zh-rHK/bools.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ true
+
+
diff --git a/app/src/main/res/values-zh-rTW/arrays.xml b/app/src/main/res/values-zh-rTW/arrays.xml
deleted file mode 100644
index 621124fc6..000000000
--- a/app/src/main/res/values-zh-rTW/arrays.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-
-
-
- - tag-translations-zh-rCN.sha1
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN.sha1
- - tag-translations-zh-rCN
- - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN
-
-
-
diff --git a/app/src/main/res/values-zh-rTW/bools.xml b/app/src/main/res/values-zh-rTW/bools.xml
new file mode 100644
index 000000000..ab59f02a2
--- /dev/null
+++ b/app/src/main/res/values-zh-rTW/bools.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ true
+
+
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
index b9fb52623..f58820028 100644
--- a/app/src/main/res/values/arrays.xml
+++ b/app/src/main/res/values/arrays.xml
@@ -209,7 +209,10 @@
- - @null
+ - tag-translations-zh-rCN.sha1
+ - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN.sha1
+ - tag-translations-zh-rCN
+ - https://raw.githubusercontent.com/Mapaler/EhViewer/tag-translations/tag-translations/tag-translations-zh-rCN
diff --git a/app/src/main/res/values/bools.xml b/app/src/main/res/values/bools.xml
new file mode 100644
index 000000000..85ab1551d
--- /dev/null
+++ b/app/src/main/res/values/bools.xml
@@ -0,0 +1,23 @@
+
+
+
+
+ false
+
+