From 4c060689fad760b3abe5065c24d5edf8bb28472b Mon Sep 17 00:00:00 2001 From: Helium314 Date: Sat, 15 Feb 2025 20:11:27 +0100 Subject: [PATCH] shorter name for some functions for custom layouts --- .../internal/keyboard_parser/LayoutParser.kt | 2 +- .../main/java/helium314/keyboard/latin/App.kt | 7 +++-- .../latin/settings/LanguageSettingsDialog.kt | 8 +++--- .../keyboard/latin/utils/LayoutUtilsCustom.kt | 27 +++++++++---------- .../keyboard/latin/utils/SubtypeSettings.kt | 2 +- .../settings/dialogs/LayoutEditDialog.kt | 14 +++++----- .../settings/dialogs/LayoutPickerDialog.kt | 8 +++--- .../preferences/BackupRestorePreference.kt | 2 +- 8 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LayoutParser.kt b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LayoutParser.kt index cd3197ff1..3164cc698 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LayoutParser.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/internal/keyboard_parser/LayoutParser.kt @@ -101,7 +101,7 @@ object LayoutParser { private fun getLayoutFileContent(layoutType: LayoutType, layoutName: String, context: Context): String { if (LayoutUtilsCustom.isCustomLayout(layoutName)) - LayoutUtilsCustom.getCustomLayoutFiles(layoutType, context) + LayoutUtilsCustom.getLayoutFiles(layoutType, context) .firstOrNull { it.name.startsWith(layoutName) }?.let { return it.readText() } return LayoutUtils.getContent(layoutType, layoutName, context) } diff --git a/app/src/main/java/helium314/keyboard/latin/App.kt b/app/src/main/java/helium314/keyboard/latin/App.kt index d9065a384..f4b9222e2 100644 --- a/app/src/main/java/helium314/keyboard/latin/App.kt +++ b/app/src/main/java/helium314/keyboard/latin/App.kt @@ -19,7 +19,6 @@ import helium314.keyboard.latin.utils.DictionaryInfoUtils import helium314.keyboard.latin.utils.LayoutType import helium314.keyboard.latin.utils.LayoutType.Companion.folder import helium314.keyboard.latin.utils.LayoutUtilsCustom -import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.ScriptUtils.SCRIPT_LATIN import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ToolbarKey @@ -151,7 +150,7 @@ fun checkVersionUpgrade(context: Context) { Settings.writePrefAdditionalSubtypes(prefs, newSubtypeStrings.joinToString(";")) } // rename other custom layouts - LayoutUtilsCustom.onCustomLayoutFileListChanged() + LayoutUtilsCustom.onLayoutFileChanged() File(DeviceProtectedUtils.getFilesDir(context), "layouts").listFiles()?.forEach { val newFile = getCustomLayoutFile(it.name.substringBeforeLast(".") + ".", context) if (newFile.name == it.name) return@forEach @@ -355,7 +354,7 @@ fun checkVersionUpgrade(context: Context) { } if (oldVersion <= 2304) { // rename layout files for latin scripts, and adjust layouts stored in prefs accordingly - LayoutUtilsCustom.getCustomLayoutFiles(LayoutType.MAIN, context).forEach { + LayoutUtilsCustom.getLayoutFiles(LayoutType.MAIN, context).forEach { val locale = it.name.substringAfter("custom.").substringBefore(".").constructLocale() if (locale.script() != SCRIPT_LATIN) return@forEach // change language tag to SCRIPT_LATIN, but @@ -375,7 +374,7 @@ fun checkVersionUpgrade(context: Context) { } } upgradeToolbarPrefs(prefs) - LayoutUtilsCustom.onCustomLayoutFileListChanged() // just to be sure + LayoutUtilsCustom.onLayoutFileChanged() // just to be sure prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) } } diff --git a/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt b/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt index 811342951..3daa37d13 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/LanguageSettingsDialog.kt @@ -100,7 +100,7 @@ class LanguageSettingsDialog( } private fun addSubtype(name: String) { - LayoutUtilsCustom.onCustomLayoutFileListChanged() + LayoutUtilsCustom.onLayoutFileChanged() val newSubtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(mainLocale, name, infos.first().subtype.isAsciiCapable) val newSubtypeInfo = newSubtype.toSubtypeInfo(mainLocale, context, true, infos.first().hasDictionary) // enabled by default val displayName = SubtypeLocaleUtils.getMainLayoutDisplayName(newSubtype) @@ -155,7 +155,7 @@ class LanguageSettingsDialog( .setItems(displayNames.toTypedArray()) { di, i -> di.dismiss() val fileName = context.assets.list("layouts")?.firstOrNull { it.startsWith(layouts[i]) } ?: return@setItems - LayoutUtilsCustom.loadCustomLayout(context.assets.open("layouts${File.separator}$fileName").reader().readText(), + LayoutUtilsCustom.loadLayout(context.assets.open("layouts${File.separator}$fileName").reader().readText(), displayNames[i], mainLocale.toLanguageTag(), context) { addSubtype(it) } } .setNegativeButton(android.R.string.cancel, null) @@ -163,7 +163,7 @@ class LanguageSettingsDialog( } override fun onNewLayoutFile(uri: Uri?) { - LayoutUtilsCustom.loadCustomLayout(uri, mainLocale.toLanguageTag(), context) { addSubtype(it) } + LayoutUtilsCustom.loadLayout(uri, mainLocale.toLanguageTag(), context) { addSubtype(it) } } private fun addSubtypeToView(subtype: SubtypeInfo) { @@ -174,7 +174,7 @@ class LanguageSettingsDialog( ?: subtype.subtype.displayName(context) if (LayoutUtilsCustom.isCustomLayout(layoutSetName)) { row.findViewById(R.id.language_details).setText(R.string.edit_layout) - row.findViewById(R.id.language_text).setOnClickListener { LayoutUtilsCustom.editCustomLayout(layoutSetName, context) } + row.findViewById(R.id.language_text).setOnClickListener { LayoutUtilsCustom.editLayout(layoutSetName, context) } } else { row.findViewById(R.id.language_details).isGone = true } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/LayoutUtilsCustom.kt b/app/src/main/java/helium314/keyboard/latin/utils/LayoutUtilsCustom.kt index 5d0d38bea..5888d568d 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/LayoutUtilsCustom.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/LayoutUtilsCustom.kt @@ -29,7 +29,7 @@ import java.util.EnumMap import java.util.Locale object LayoutUtilsCustom { - fun loadCustomLayout(uri: Uri?, languageTag: String, context: Context, onAdded: (String) -> Unit) { + fun loadLayout(uri: Uri?, languageTag: String, context: Context, onAdded: (String) -> Unit) { if (uri == null) return infoDialog(context, context.getString(R.string.layout_error, "layout file not found")) val layoutContent: String @@ -50,10 +50,10 @@ object LayoutUtilsCustom { name = it.getString(idx).substringBeforeLast(".") } } - loadCustomLayout(layoutContent, name, languageTag, context, onAdded) + loadLayout(layoutContent, name, languageTag, context, onAdded) } - fun loadCustomLayout(layoutContent: String, layoutName: String, languageTag: String, context: Context, onAdded: (String) -> Unit) { + fun loadLayout(layoutContent: String, layoutName: String, languageTag: String, context: Context, onAdded: (String) -> Unit) { var name = layoutName if (!checkLayout(layoutContent, context)) return infoDialog(context, context.getString(R.string.layout_error, "invalid layout file, ${Log.getLog(10).lastOrNull { it.tag == TAG }?.message}")) @@ -72,7 +72,7 @@ object LayoutUtilsCustom { .setPositiveButton(android.R.string.ok) { _, _ -> // name must be encoded to avoid issues with validity of subtype extra string or file name name = "$CUSTOM_LAYOUT_PREFIX${languageTag}.${encodeBase36(name)}." - val file = getCustomLayoutFile(name, LayoutType.MAIN, context) + val file = getLayoutFile(name, LayoutType.MAIN, context) if (file.exists()) file.delete() file.parentFile?.mkdir() @@ -151,7 +151,7 @@ object LayoutUtilsCustom { return true } - fun getCustomLayoutFiles(layoutType: LayoutType, context: Context, locale: Locale? = null): List { + fun getLayoutFiles(layoutType: LayoutType, context: Context, locale: Locale? = null): List { val layouts = customLayoutMap.getOrPut(layoutType) { File(DeviceProtectedUtils.getFilesDir(context), layoutType.folder).listFiles()?.toList() ?: emptyList() } @@ -162,7 +162,7 @@ object LayoutUtilsCustom { return layouts.filter { it.name.startsWith(CUSTOM_LAYOUT_PREFIX + locale.toLanguageTag() + ".") } } - fun onCustomLayoutFileListChanged() { + fun onLayoutFileChanged() { customLayoutMap.clear() } @@ -175,7 +175,7 @@ object LayoutUtilsCustom { layoutName } - fun getCustomLayoutName(displayName: String) = CUSTOM_LAYOUT_PREFIX + encodeBase36(displayName) + "." + fun getLayoutName(displayName: String) = CUSTOM_LAYOUT_PREFIX + encodeBase36(displayName) + "." fun getMainLayoutName(displayName: String, locale: Locale) = if (locale.script() == ScriptUtils.SCRIPT_LATIN) @@ -184,14 +184,14 @@ object LayoutUtilsCustom { fun isCustomLayout(layoutName: String) = layoutName.startsWith(CUSTOM_LAYOUT_PREFIX) - fun getCustomLayoutFile(layoutName: String, layoutType: LayoutType, context: Context): File { + fun getLayoutFile(layoutName: String, layoutType: LayoutType, context: Context): File { val file = File(DeviceProtectedUtils.getFilesDir(context), layoutType.folder + layoutName) file.parentFile?.mkdirs() return file } - fun editCustomLayout(layoutName: String, context: Context, startContent: String? = null, displayName: CharSequence? = null) { - val file = getCustomLayoutFile(layoutName, LayoutType.MAIN, context) + fun editLayout(layoutName: String, context: Context, startContent: String? = null, displayName: CharSequence? = null) { + val file = getLayoutFile(layoutName, LayoutType.MAIN, context) val editText = EditText(context).apply { setText(startContent ?: file.readText()) } @@ -201,12 +201,12 @@ object LayoutUtilsCustom { .setPositiveButton(R.string.save) { _, _ -> val content = editText.text.toString() if (!checkLayout(content, context)) { - editCustomLayout(layoutName, context, content) + editLayout(layoutName, context, content) infoDialog(context, context.getString(R.string.layout_error, Log.getLog(10).lastOrNull { it.tag == TAG }?.message)) } else { file.parentFile?.mkdir() file.writeText(content) - onCustomLayoutFileListChanged() + onLayoutFileChanged() KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context) } } @@ -216,7 +216,7 @@ object LayoutUtilsCustom { builder.setNeutralButton(R.string.delete) { _, _ -> confirmDialog(context, context.getString(R.string.delete_layout, displayName), context.getString(R.string.delete)) { file.delete() - onCustomLayoutFileListChanged() + onLayoutFileChanged() KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context) } } @@ -230,5 +230,4 @@ object LayoutUtilsCustom { const val CUSTOM_LAYOUT_PREFIX = "custom." private const val TAG = "LayoutUtilsCustom" private val customLayoutMap = EnumMap>(LayoutType::class.java) - } diff --git a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt index 6a6a88d41..17aeb048a 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/SubtypeSettings.kt @@ -232,7 +232,7 @@ private fun loadResourceSubtypes(resources: Resources) { private fun removeInvalidCustomSubtypes(context: Context) { // todo: new layout structure! val prefs = context.prefs() val additionalSubtypes = prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!.split(";") - val customSubtypeFiles by lazy { LayoutUtilsCustom.getCustomLayoutFiles(LayoutType.MAIN, context).map { it.name } } + val customSubtypeFiles by lazy { LayoutUtilsCustom.getLayoutFiles(LayoutType.MAIN, context).map { it.name } } val subtypesToRemove = mutableListOf() additionalSubtypes.forEach { val name = it.substringAfter(":").substringBefore(":") diff --git a/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutEditDialog.kt b/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutEditDialog.kt index aee69c83d..6f77a47ca 100644 --- a/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutEditDialog.kt +++ b/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutEditDialog.kt @@ -50,8 +50,8 @@ fun LayoutEditDialog( } val nameValid = displayNameValue.text.isNotBlank() && ( - (startIsCustom && LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text) == initialLayoutName) - || isNameValid(LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text)) + (startIsCustom && LayoutUtilsCustom.getLayoutName(displayNameValue.text) == initialLayoutName) + || isNameValid(LayoutUtilsCustom.getLayoutName(displayNameValue.text)) ) TextInputDialog( @@ -60,15 +60,15 @@ fun LayoutEditDialog( onDismissRequest() }, onConfirmed = { - val newLayoutName = LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text) + val newLayoutName = LayoutUtilsCustom.getLayoutName(displayNameValue.text) if (startIsCustom && initialLayoutName != newLayoutName) - LayoutUtilsCustom.getCustomLayoutFile(initialLayoutName, layoutType, ctx).delete() - LayoutUtilsCustom.getCustomLayoutFile(newLayoutName, layoutType, ctx).writeText(it) - LayoutUtilsCustom.onCustomLayoutFileListChanged() + LayoutUtilsCustom.getLayoutFile(initialLayoutName, layoutType, ctx).delete() + LayoutUtilsCustom.getLayoutFile(newLayoutName, layoutType, ctx).writeText(it) + LayoutUtilsCustom.onLayoutFileChanged() keyboardNeedsReload = true }, confirmButtonText = stringResource(R.string.save), - initialText = startContent ?: LayoutUtilsCustom.getCustomLayoutFile(initialLayoutName, layoutType, ctx).readText(), + initialText = startContent ?: LayoutUtilsCustom.getLayoutFile(initialLayoutName, layoutType, ctx).readText(), singleLine = false, title = { TextField( diff --git a/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutPickerDialog.kt b/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutPickerDialog.kt index 949557414..81767081f 100644 --- a/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutPickerDialog.kt +++ b/app/src/main/java/helium314/keyboard/settings/dialogs/LayoutPickerDialog.kt @@ -68,7 +68,7 @@ fun LayoutPickerDialog( val currentLayout = Settings.readDefaultLayoutName(layoutType, prefs) val internalLayouts = LayoutUtils.getAvailableLayouts(layoutType, ctx) // todo: getCustomLayoutFiles does not work nicely for main layout, but currently this dialog is not used for them - val customLayouts = LayoutUtilsCustom.getCustomLayoutFiles(layoutType, ctx).map { it.name }.sorted() + val customLayouts = LayoutUtilsCustom.getLayoutFiles(layoutType, ctx).map { it.name }.sorted() val layouts = internalLayouts + customLayouts + "" val state = rememberLazyListState() @@ -125,8 +125,8 @@ fun LayoutPickerDialog( prefs.edit().remove(Settings.PREF_LAYOUT_PREFIX + layoutType.name).apply() keyboardNeedsReload = true } - LayoutUtilsCustom.getCustomLayoutFiles(layoutType, ctx).firstOrNull { it.name == deletedLayout }?.delete() - LayoutUtilsCustom.onCustomLayoutFileListChanged() + LayoutUtilsCustom.getLayoutFiles(layoutType, ctx).firstOrNull { it.name == deletedLayout }?.delete() + LayoutUtilsCustom.onLayoutFileChanged() }, layoutType = layoutType, layoutName = item, @@ -167,7 +167,7 @@ private fun AddLayoutRow(onNewLayout: (String) -> Unit, userLayouts: Collection< singleLine = true ) IconButton( - enabled = textValue.text.isNotEmpty() && LayoutUtilsCustom.getCustomLayoutName(textValue.text) !in userLayouts, + enabled = textValue.text.isNotEmpty() && LayoutUtilsCustom.getLayoutName(textValue.text) !in userLayouts, onClick = { onNewLayout(textValue.text) } ) { Icon(painterResource(R.drawable.ic_edit), null) } } diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt index 27dd2ca84..1eaa8cc34 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt @@ -180,7 +180,7 @@ fun BackupRestorePreference(setting: Setting) { reloadEnabledSubtypes(ctx) val newDictBroadcast = Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION) ctx.getActivity()?.sendBroadcast(newDictBroadcast) - LayoutUtilsCustom.onCustomLayoutFileListChanged() + LayoutUtilsCustom.onLayoutFileChanged() (ctx.getActivity() as? SettingsActivity)?.prefChanged?.value = 210 // for settings reload keyboardNeedsReload = true }