shorter name for some functions for custom layouts

This commit is contained in:
Helium314 2025-02-15 20:11:27 +01:00
parent bccb10ea39
commit 4c060689fa
8 changed files with 34 additions and 36 deletions

View file

@ -101,7 +101,7 @@ object LayoutParser {
private fun getLayoutFileContent(layoutType: LayoutType, layoutName: String, context: Context): String { private fun getLayoutFileContent(layoutType: LayoutType, layoutName: String, context: Context): String {
if (LayoutUtilsCustom.isCustomLayout(layoutName)) if (LayoutUtilsCustom.isCustomLayout(layoutName))
LayoutUtilsCustom.getCustomLayoutFiles(layoutType, context) LayoutUtilsCustom.getLayoutFiles(layoutType, context)
.firstOrNull { it.name.startsWith(layoutName) }?.let { return it.readText() } .firstOrNull { it.name.startsWith(layoutName) }?.let { return it.readText() }
return LayoutUtils.getContent(layoutType, layoutName, context) return LayoutUtils.getContent(layoutType, layoutName, context)
} }

View file

@ -19,7 +19,6 @@ import helium314.keyboard.latin.utils.DictionaryInfoUtils
import helium314.keyboard.latin.utils.LayoutType import helium314.keyboard.latin.utils.LayoutType
import helium314.keyboard.latin.utils.LayoutType.Companion.folder import helium314.keyboard.latin.utils.LayoutType.Companion.folder
import helium314.keyboard.latin.utils.LayoutUtilsCustom 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_LATIN
import helium314.keyboard.latin.utils.ScriptUtils.script import helium314.keyboard.latin.utils.ScriptUtils.script
import helium314.keyboard.latin.utils.ToolbarKey import helium314.keyboard.latin.utils.ToolbarKey
@ -151,7 +150,7 @@ fun checkVersionUpgrade(context: Context) {
Settings.writePrefAdditionalSubtypes(prefs, newSubtypeStrings.joinToString(";")) Settings.writePrefAdditionalSubtypes(prefs, newSubtypeStrings.joinToString(";"))
} }
// rename other custom layouts // rename other custom layouts
LayoutUtilsCustom.onCustomLayoutFileListChanged() LayoutUtilsCustom.onLayoutFileChanged()
File(DeviceProtectedUtils.getFilesDir(context), "layouts").listFiles()?.forEach { File(DeviceProtectedUtils.getFilesDir(context), "layouts").listFiles()?.forEach {
val newFile = getCustomLayoutFile(it.name.substringBeforeLast(".") + ".", context) val newFile = getCustomLayoutFile(it.name.substringBeforeLast(".") + ".", context)
if (newFile.name == it.name) return@forEach if (newFile.name == it.name) return@forEach
@ -355,7 +354,7 @@ fun checkVersionUpgrade(context: Context) {
} }
if (oldVersion <= 2304) { if (oldVersion <= 2304) {
// rename layout files for latin scripts, and adjust layouts stored in prefs accordingly // 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() val locale = it.name.substringAfter("custom.").substringBefore(".").constructLocale()
if (locale.script() != SCRIPT_LATIN) return@forEach if (locale.script() != SCRIPT_LATIN) return@forEach
// change language tag to SCRIPT_LATIN, but // change language tag to SCRIPT_LATIN, but
@ -375,7 +374,7 @@ fun checkVersionUpgrade(context: Context) {
} }
} }
upgradeToolbarPrefs(prefs) upgradeToolbarPrefs(prefs)
LayoutUtilsCustom.onCustomLayoutFileListChanged() // just to be sure LayoutUtilsCustom.onLayoutFileChanged() // just to be sure
prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) } prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) }
} }

View file

@ -100,7 +100,7 @@ class LanguageSettingsDialog(
} }
private fun addSubtype(name: String) { private fun addSubtype(name: String) {
LayoutUtilsCustom.onCustomLayoutFileListChanged() LayoutUtilsCustom.onLayoutFileChanged()
val newSubtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(mainLocale, name, infos.first().subtype.isAsciiCapable) val newSubtype = SubtypeUtilsAdditional.createEmojiCapableAdditionalSubtype(mainLocale, name, infos.first().subtype.isAsciiCapable)
val newSubtypeInfo = newSubtype.toSubtypeInfo(mainLocale, context, true, infos.first().hasDictionary) // enabled by default val newSubtypeInfo = newSubtype.toSubtypeInfo(mainLocale, context, true, infos.first().hasDictionary) // enabled by default
val displayName = SubtypeLocaleUtils.getMainLayoutDisplayName(newSubtype) val displayName = SubtypeLocaleUtils.getMainLayoutDisplayName(newSubtype)
@ -155,7 +155,7 @@ class LanguageSettingsDialog(
.setItems(displayNames.toTypedArray()) { di, i -> .setItems(displayNames.toTypedArray()) { di, i ->
di.dismiss() di.dismiss()
val fileName = context.assets.list("layouts")?.firstOrNull { it.startsWith(layouts[i]) } ?: return@setItems 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) } displayNames[i], mainLocale.toLanguageTag(), context) { addSubtype(it) }
} }
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
@ -163,7 +163,7 @@ class LanguageSettingsDialog(
} }
override fun onNewLayoutFile(uri: Uri?) { 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) { private fun addSubtypeToView(subtype: SubtypeInfo) {
@ -174,7 +174,7 @@ class LanguageSettingsDialog(
?: subtype.subtype.displayName(context) ?: subtype.subtype.displayName(context)
if (LayoutUtilsCustom.isCustomLayout(layoutSetName)) { if (LayoutUtilsCustom.isCustomLayout(layoutSetName)) {
row.findViewById<TextView>(R.id.language_details).setText(R.string.edit_layout) row.findViewById<TextView>(R.id.language_details).setText(R.string.edit_layout)
row.findViewById<View>(R.id.language_text).setOnClickListener { LayoutUtilsCustom.editCustomLayout(layoutSetName, context) } row.findViewById<View>(R.id.language_text).setOnClickListener { LayoutUtilsCustom.editLayout(layoutSetName, context) }
} else { } else {
row.findViewById<View>(R.id.language_details).isGone = true row.findViewById<View>(R.id.language_details).isGone = true
} }

View file

@ -29,7 +29,7 @@ import java.util.EnumMap
import java.util.Locale import java.util.Locale
object LayoutUtilsCustom { 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) if (uri == null)
return infoDialog(context, context.getString(R.string.layout_error, "layout file not found")) return infoDialog(context, context.getString(R.string.layout_error, "layout file not found"))
val layoutContent: String val layoutContent: String
@ -50,10 +50,10 @@ object LayoutUtilsCustom {
name = it.getString(idx).substringBeforeLast(".") 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 var name = layoutName
if (!checkLayout(layoutContent, context)) if (!checkLayout(layoutContent, context))
return infoDialog(context, context.getString(R.string.layout_error, "invalid layout file, ${Log.getLog(10).lastOrNull { it.tag == TAG }?.message}")) 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) { _, _ -> .setPositiveButton(android.R.string.ok) { _, _ ->
// name must be encoded to avoid issues with validity of subtype extra string or file name // name must be encoded to avoid issues with validity of subtype extra string or file name
name = "$CUSTOM_LAYOUT_PREFIX${languageTag}.${encodeBase36(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()) if (file.exists())
file.delete() file.delete()
file.parentFile?.mkdir() file.parentFile?.mkdir()
@ -151,7 +151,7 @@ object LayoutUtilsCustom {
return true return true
} }
fun getCustomLayoutFiles(layoutType: LayoutType, context: Context, locale: Locale? = null): List<File> { fun getLayoutFiles(layoutType: LayoutType, context: Context, locale: Locale? = null): List<File> {
val layouts = customLayoutMap.getOrPut(layoutType) { val layouts = customLayoutMap.getOrPut(layoutType) {
File(DeviceProtectedUtils.getFilesDir(context), layoutType.folder).listFiles()?.toList() ?: emptyList() 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() + ".") } return layouts.filter { it.name.startsWith(CUSTOM_LAYOUT_PREFIX + locale.toLanguageTag() + ".") }
} }
fun onCustomLayoutFileListChanged() { fun onLayoutFileChanged() {
customLayoutMap.clear() customLayoutMap.clear()
} }
@ -175,7 +175,7 @@ object LayoutUtilsCustom {
layoutName 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) = fun getMainLayoutName(displayName: String, locale: Locale) =
if (locale.script() == ScriptUtils.SCRIPT_LATIN) if (locale.script() == ScriptUtils.SCRIPT_LATIN)
@ -184,14 +184,14 @@ object LayoutUtilsCustom {
fun isCustomLayout(layoutName: String) = layoutName.startsWith(CUSTOM_LAYOUT_PREFIX) 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) val file = File(DeviceProtectedUtils.getFilesDir(context), layoutType.folder + layoutName)
file.parentFile?.mkdirs() file.parentFile?.mkdirs()
return file return file
} }
fun editCustomLayout(layoutName: String, context: Context, startContent: String? = null, displayName: CharSequence? = null) { fun editLayout(layoutName: String, context: Context, startContent: String? = null, displayName: CharSequence? = null) {
val file = getCustomLayoutFile(layoutName, LayoutType.MAIN, context) val file = getLayoutFile(layoutName, LayoutType.MAIN, context)
val editText = EditText(context).apply { val editText = EditText(context).apply {
setText(startContent ?: file.readText()) setText(startContent ?: file.readText())
} }
@ -201,12 +201,12 @@ object LayoutUtilsCustom {
.setPositiveButton(R.string.save) { _, _ -> .setPositiveButton(R.string.save) { _, _ ->
val content = editText.text.toString() val content = editText.text.toString()
if (!checkLayout(content, context)) { 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)) infoDialog(context, context.getString(R.string.layout_error, Log.getLog(10).lastOrNull { it.tag == TAG }?.message))
} else { } else {
file.parentFile?.mkdir() file.parentFile?.mkdir()
file.writeText(content) file.writeText(content)
onCustomLayoutFileListChanged() onLayoutFileChanged()
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context) KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context)
} }
} }
@ -216,7 +216,7 @@ object LayoutUtilsCustom {
builder.setNeutralButton(R.string.delete) { _, _ -> builder.setNeutralButton(R.string.delete) { _, _ ->
confirmDialog(context, context.getString(R.string.delete_layout, displayName), context.getString(R.string.delete)) { confirmDialog(context, context.getString(R.string.delete_layout, displayName), context.getString(R.string.delete)) {
file.delete() file.delete()
onCustomLayoutFileListChanged() onLayoutFileChanged()
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context) KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(context)
} }
} }
@ -230,5 +230,4 @@ object LayoutUtilsCustom {
const val CUSTOM_LAYOUT_PREFIX = "custom." const val CUSTOM_LAYOUT_PREFIX = "custom."
private const val TAG = "LayoutUtilsCustom" private const val TAG = "LayoutUtilsCustom"
private val customLayoutMap = EnumMap<LayoutType, List<File>>(LayoutType::class.java) private val customLayoutMap = EnumMap<LayoutType, List<File>>(LayoutType::class.java)
} }

View file

@ -232,7 +232,7 @@ private fun loadResourceSubtypes(resources: Resources) {
private fun removeInvalidCustomSubtypes(context: Context) { // todo: new layout structure! private fun removeInvalidCustomSubtypes(context: Context) { // todo: new layout structure!
val prefs = context.prefs() val prefs = context.prefs()
val additionalSubtypes = prefs.getString(Settings.PREF_ADDITIONAL_SUBTYPES, Defaults.PREF_ADDITIONAL_SUBTYPES)!!.split(";") 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<String>() val subtypesToRemove = mutableListOf<String>()
additionalSubtypes.forEach { additionalSubtypes.forEach {
val name = it.substringAfter(":").substringBefore(":") val name = it.substringAfter(":").substringBefore(":")

View file

@ -50,8 +50,8 @@ fun LayoutEditDialog(
} }
val nameValid = displayNameValue.text.isNotBlank() val nameValid = displayNameValue.text.isNotBlank()
&& ( && (
(startIsCustom && LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text) == initialLayoutName) (startIsCustom && LayoutUtilsCustom.getLayoutName(displayNameValue.text) == initialLayoutName)
|| isNameValid(LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text)) || isNameValid(LayoutUtilsCustom.getLayoutName(displayNameValue.text))
) )
TextInputDialog( TextInputDialog(
@ -60,15 +60,15 @@ fun LayoutEditDialog(
onDismissRequest() onDismissRequest()
}, },
onConfirmed = { onConfirmed = {
val newLayoutName = LayoutUtilsCustom.getCustomLayoutName(displayNameValue.text) val newLayoutName = LayoutUtilsCustom.getLayoutName(displayNameValue.text)
if (startIsCustom && initialLayoutName != newLayoutName) if (startIsCustom && initialLayoutName != newLayoutName)
LayoutUtilsCustom.getCustomLayoutFile(initialLayoutName, layoutType, ctx).delete() LayoutUtilsCustom.getLayoutFile(initialLayoutName, layoutType, ctx).delete()
LayoutUtilsCustom.getCustomLayoutFile(newLayoutName, layoutType, ctx).writeText(it) LayoutUtilsCustom.getLayoutFile(newLayoutName, layoutType, ctx).writeText(it)
LayoutUtilsCustom.onCustomLayoutFileListChanged() LayoutUtilsCustom.onLayoutFileChanged()
keyboardNeedsReload = true keyboardNeedsReload = true
}, },
confirmButtonText = stringResource(R.string.save), confirmButtonText = stringResource(R.string.save),
initialText = startContent ?: LayoutUtilsCustom.getCustomLayoutFile(initialLayoutName, layoutType, ctx).readText(), initialText = startContent ?: LayoutUtilsCustom.getLayoutFile(initialLayoutName, layoutType, ctx).readText(),
singleLine = false, singleLine = false,
title = { title = {
TextField( TextField(

View file

@ -68,7 +68,7 @@ fun LayoutPickerDialog(
val currentLayout = Settings.readDefaultLayoutName(layoutType, prefs) val currentLayout = Settings.readDefaultLayoutName(layoutType, prefs)
val internalLayouts = LayoutUtils.getAvailableLayouts(layoutType, ctx) val internalLayouts = LayoutUtils.getAvailableLayouts(layoutType, ctx)
// todo: getCustomLayoutFiles does not work nicely for main layout, but currently this dialog is not used for them // 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 layouts = internalLayouts + customLayouts + ""
val state = rememberLazyListState() val state = rememberLazyListState()
@ -125,8 +125,8 @@ fun LayoutPickerDialog(
prefs.edit().remove(Settings.PREF_LAYOUT_PREFIX + layoutType.name).apply() prefs.edit().remove(Settings.PREF_LAYOUT_PREFIX + layoutType.name).apply()
keyboardNeedsReload = true keyboardNeedsReload = true
} }
LayoutUtilsCustom.getCustomLayoutFiles(layoutType, ctx).firstOrNull { it.name == deletedLayout }?.delete() LayoutUtilsCustom.getLayoutFiles(layoutType, ctx).firstOrNull { it.name == deletedLayout }?.delete()
LayoutUtilsCustom.onCustomLayoutFileListChanged() LayoutUtilsCustom.onLayoutFileChanged()
}, },
layoutType = layoutType, layoutType = layoutType,
layoutName = item, layoutName = item,
@ -167,7 +167,7 @@ private fun AddLayoutRow(onNewLayout: (String) -> Unit, userLayouts: Collection<
singleLine = true singleLine = true
) )
IconButton( 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) } onClick = { onNewLayout(textValue.text) }
) { Icon(painterResource(R.drawable.ic_edit), null) } ) { Icon(painterResource(R.drawable.ic_edit), null) }
} }

View file

@ -180,7 +180,7 @@ fun BackupRestorePreference(setting: Setting) {
reloadEnabledSubtypes(ctx) reloadEnabledSubtypes(ctx)
val newDictBroadcast = Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION) val newDictBroadcast = Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION)
ctx.getActivity()?.sendBroadcast(newDictBroadcast) ctx.getActivity()?.sendBroadcast(newDictBroadcast)
LayoutUtilsCustom.onCustomLayoutFileListChanged() LayoutUtilsCustom.onLayoutFileChanged()
(ctx.getActivity() as? SettingsActivity)?.prefChanged?.value = 210 // for settings reload (ctx.getActivity() as? SettingsActivity)?.prefChanged?.value = 210 // for settings reload
keyboardNeedsReload = true keyboardNeedsReload = true
} }