2025-02-15 10:52:56 +01:00
|
|
|
package helium314.keyboard.latin.utils
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import helium314.keyboard.latin.settings.Defaults.default
|
|
|
|
import helium314.keyboard.latin.utils.LayoutType.Companion.folder
|
|
|
|
import helium314.keyboard.latin.utils.ScriptUtils.script
|
|
|
|
import java.util.Locale
|
|
|
|
|
|
|
|
// for layouts provided by the app
|
|
|
|
object LayoutUtils {
|
|
|
|
fun getAvailableLayouts(layoutType: LayoutType, context: Context, locale: Locale? = null): Collection<String> {
|
|
|
|
if (layoutType != LayoutType.MAIN)
|
|
|
|
return context.assets.list(layoutType.folder)?.map { it.substringBefore(".") }.orEmpty()
|
|
|
|
if (locale == null)
|
|
|
|
return getAllAvailableSubtypes().mapTo(HashSet()) { it.mainLayoutName()?.substringBefore("+") ?: "qwerty" }
|
|
|
|
if (locale.script() == ScriptUtils.SCRIPT_LATIN)
|
2025-02-15 18:31:25 +01:00
|
|
|
return getAllAvailableSubtypes().filter { it.isAsciiCapable && !LayoutUtilsCustom.isCustomLayout(it.mainLayoutName() ?: "qwerty") }
|
2025-02-15 10:52:56 +01:00
|
|
|
.mapTo(HashSet()) { it.mainLayoutName()?.substringBefore("+") ?: "qwerty" }
|
|
|
|
return getSubtypesForLocale(locale).mapNotNullTo(HashSet()) { it.mainLayoutName() }
|
|
|
|
}
|
|
|
|
|
2025-02-15 18:31:25 +01:00
|
|
|
fun getLMainLayoutsForLocales(locales: List<Locale>, context: Context): Collection<String> =
|
|
|
|
locales.flatMapTo(HashSet()) { getAvailableLayouts(LayoutType.MAIN, context, it) }.sorted()
|
|
|
|
|
2025-02-15 10:52:56 +01:00
|
|
|
fun getContent(layoutType: LayoutType, layoutName: String, context: Context): String {
|
|
|
|
val layouts = context.assets.list(layoutType.folder)!!
|
|
|
|
layouts.firstOrNull { it.startsWith("$layoutName.") }
|
|
|
|
?.let { return context.assets.open(layoutType.folder + it).reader().readText() }
|
|
|
|
val fallback = layouts.first { it.startsWith(layoutType.default) } // must exist!
|
|
|
|
return context.assets.open(layoutType.folder + fallback).reader().readText()
|
|
|
|
}
|
|
|
|
}
|