Preload font variants for selected family

This commit is contained in:
Suphon Thanakornpakapong
2021-10-03 00:21:25 +07:00
parent a9fb200ef2
commit 29c05c85d8
2 changed files with 23 additions and 8 deletions

View File

@@ -53,6 +53,11 @@ class FontCache private constructor(private val context: Context) {
return loadFontAsync(font).await()
}
fun preloadFont(font: Font) {
@Suppress("DeferredResultUnused")
loadFontAsync(font)
}
@ExperimentalCoroutinesApi
fun getLoadedFont(font: Font): Typeface? {
val deferredFont = deferredFonts[font] ?: return null

View File

@@ -152,15 +152,17 @@ fun FontSelectionItem(
)
)
},
endWidget = if (selected && family.variants.size > 1) { {
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.wrapContentWidth()
) {
VariantDropdown(adapter = adapter, family = family)
endWidget = if (selected && family.variants.size > 1) {
{
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.wrapContentWidth()
) {
VariantDropdown(adapter = adapter, family = family)
}
}
} } else null,
} else null,
modifier = Modifier.clickable { adapter.onChange(family.default) },
showDivider = showDivider,
dividerIndent = 40.dp,
@@ -175,6 +177,14 @@ fun VariantDropdown(
) {
val selectedFont = adapter.state.value
var showVariants by remember { mutableStateOf(false) }
val context = LocalContext.current
DisposableEffect(family) {
val fontCache = FontCache.INSTANCE.get(context)
family.variants.forEach { fontCache.preloadFont(it.value) }
onDispose { }
}
TextButton(
onClick = { showVariants = true },
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colors.onBackground)