mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-06-09 16:17:44 +00:00
cache subtype display names for improved performance
This commit is contained in:
parent
40de17c148
commit
eec197c32c
3 changed files with 18 additions and 3 deletions
|
@ -26,6 +26,7 @@ import helium314.keyboard.latin.utils.InputTypeUtils;
|
|||
import helium314.keyboard.latin.utils.Log;
|
||||
import helium314.keyboard.latin.utils.ResourceUtils;
|
||||
import helium314.keyboard.latin.utils.ScriptUtils;
|
||||
import helium314.keyboard.latin.utils.SubtypeLocaleUtils;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.HashMap;
|
||||
|
@ -95,6 +96,7 @@ public final class KeyboardLayoutSet {
|
|||
public static void onSystemLocaleChanged() {
|
||||
clearKeyboardCache();
|
||||
LocaleKeyboardInfosKt.clearCache();
|
||||
SubtypeLocaleUtils.clearDisplayNameCache();
|
||||
}
|
||||
|
||||
public static void onKeyboardThemeChanged() {
|
||||
|
|
|
@ -12,6 +12,7 @@ import android.view.inputmethod.InputMethodSubtype;
|
|||
|
||||
import helium314.keyboard.compat.ConfigurationCompatKt;
|
||||
import helium314.keyboard.latin.R;
|
||||
import helium314.keyboard.latin.common.Constants;
|
||||
import helium314.keyboard.latin.common.LocaleUtils;
|
||||
import helium314.keyboard.latin.common.StringUtils;
|
||||
|
||||
|
@ -56,6 +57,7 @@ public final class SubtypeLocaleUtils {
|
|||
private static final HashMap<String, Integer> sExceptionalLocaleToNameIdsMap = new HashMap<>();
|
||||
// Exceptional locale to subtype name with layout resource id map.
|
||||
private static final HashMap<String, Integer> sExceptionalLocaleToWithLayoutNameIdsMap = new HashMap<>();
|
||||
private static final HashMap<Integer, String> sResourceSubtypeDisplayNames = new HashMap<>();
|
||||
private static final String SUBTYPE_NAME_RESOURCE_PREFIX = "string/subtype_";
|
||||
private static final String SUBTYPE_NAME_RESOURCE_GENERIC_PREFIX = "string/subtype_generic_";
|
||||
private static final String SUBTYPE_NAME_RESOURCE_WITH_LAYOUT_PREFIX = "string/subtype_with_layout_";
|
||||
|
@ -239,8 +241,20 @@ public final class SubtypeLocaleUtils {
|
|||
|
||||
@NonNull
|
||||
public static String getSubtypeDisplayNameInSystemLocale(@NonNull final InputMethodSubtype subtype) {
|
||||
final String cached = sResourceSubtypeDisplayNames.get(subtype.hashCode());
|
||||
if (cached != null) return cached;
|
||||
|
||||
final Locale displayLocale = ConfigurationCompatKt.locale(sResources.getConfiguration());
|
||||
return getSubtypeDisplayNameInternal(subtype, displayLocale);
|
||||
final String displayName = getSubtypeDisplayNameInternal(subtype, displayLocale);
|
||||
|
||||
if (!subtype.containsExtraValueKey(Constants.Subtype.ExtraValue.IS_ADDITIONAL_SUBTYPE)) {
|
||||
sResourceSubtypeDisplayNames.put(subtype.hashCode(), displayName);
|
||||
}
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public static void clearDisplayNameCache() {
|
||||
sResourceSubtypeDisplayNames.clear();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
|
@ -67,7 +67,6 @@ fun LanguageScreen(
|
|||
}
|
||||
},
|
||||
filteredItems = { term ->
|
||||
// todo: maybe better performance with display name cache?
|
||||
sortedSubtypes.filter {
|
||||
it.displayName(ctx).replace("(", "")
|
||||
.splitOnWhitespace().any { it.startsWith(term, true) }
|
||||
|
@ -116,7 +115,7 @@ fun LanguageScreen(
|
|||
}
|
||||
}
|
||||
|
||||
// todo: sorting is slow, need to cache displayName (overall or just in getSortedSubtypes), and then it should be fine
|
||||
// sorting by display name is still slow, even with the cache... but probably good enough
|
||||
private fun getSortedSubtypes(context: Context): List<InputMethodSubtype> {
|
||||
val systemLocales = SubtypeSettings.getSystemLocales()
|
||||
val enabledSubtypes = SubtypeSettings.getEnabledSubtypes(context.prefs(), true)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue