Skip to content

Commit 54361a4

Browse files
Refactor: Use FontManager for font loading in SettingsComposable
The `FontText` composable in `SettingsComposable.kt` has been refactored to use the existing `FontManager` for loading the currently selected font. Previously, `FontText` duplicated the logic for mapping font preferences to `FontFamily` objects. This change centralizes font handling by calling `FontManager.getTypeface()` and converting the resulting `Typeface` into a Compose `FontFamily`. This refactoring simplifies the `FontText` composable, removes redundant code, and ensures a consistent font-loading mechanism across the application. Additionally, some minor code cleanup and comment removal were performed within the composable.
1 parent d9b50e3 commit 54361a4

File tree

1 file changed

+22
-44
lines changed

1 file changed

+22
-44
lines changed

app/src/main/java/com/github/codeworkscreativehub/mlauncher/ui/compose/SettingsComposable.kt

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import androidx.compose.ui.text.SpanStyle
4949
import androidx.compose.ui.text.TextLayoutResult
5050
import androidx.compose.ui.text.TextStyle
5151
import androidx.compose.ui.text.buildAnnotatedString
52-
import androidx.compose.ui.text.font.Font
5352
import androidx.compose.ui.text.font.FontFamily
5453
import androidx.compose.ui.text.font.FontWeight
5554
import androidx.compose.ui.text.style.TextAlign
@@ -61,8 +60,7 @@ import androidx.compose.ui.unit.sp
6160
import androidx.compose.ui.viewinterop.AndroidView
6261
import androidx.core.net.toUri
6362
import com.github.codeworkscreativehub.mlauncher.R
64-
import com.github.codeworkscreativehub.mlauncher.data.Constants
65-
import com.github.codeworkscreativehub.mlauncher.data.Prefs
63+
import com.github.codeworkscreativehub.mlauncher.helper.FontManager
6664
import com.github.codeworkscreativehub.mlauncher.services.HapticFeedbackService
6765
import com.github.codeworkscreativehub.mlauncher.style.SettingsTheme
6866
import com.github.creativecodecat.components.views.FontAppCompatTextView
@@ -550,64 +548,44 @@ object SettingsComposable {
550548
onTextLayout: ((TextLayoutResult) -> Unit)? = null
551549
) {
552550
val context = LocalContext.current
553-
val prefs = Prefs(context)
554-
555-
// Map saved preference to Compose FontFamily
556-
val fontFamily: FontFamily = remember(prefs.fontFamily) {
557-
when (prefs.fontFamily) {
558-
Constants.FontFamily.System -> FontFamily.Default
559-
Constants.FontFamily.Roboto -> FontFamily(Font(R.font.roboto))
560-
Constants.FontFamily.Bitter -> FontFamily(Font(R.font.bitter))
561-
Constants.FontFamily.Doto -> FontFamily(Font(R.font.doto))
562-
Constants.FontFamily.FiraCode -> FontFamily(Font(R.font.fira_code))
563-
Constants.FontFamily.Hack -> FontFamily(Font(R.font.hack))
564-
Constants.FontFamily.Lato -> FontFamily(Font(R.font.lato))
565-
Constants.FontFamily.Merriweather -> FontFamily(Font(R.font.merriweather))
566-
Constants.FontFamily.Montserrat -> FontFamily(Font(R.font.montserrat))
567-
Constants.FontFamily.Quicksand -> FontFamily(Font(R.font.quicksand))
568-
Constants.FontFamily.Raleway -> FontFamily(Font(R.font.raleway))
569-
Constants.FontFamily.SourceCodePro -> FontFamily(Font(R.font.source_code_pro))
570-
Constants.FontFamily.Custom -> FontFamily.Default
571-
}
551+
552+
// Get Typeface from FontManager (like your FontEditText)
553+
val typeface = remember { FontManager.getTypeface(context) }
554+
555+
// Convert Typeface to Compose FontFamily
556+
val fontFamily: FontFamily = remember(typeface) {
557+
typeface?.let { FontFamily(it) } ?: FontFamily.Default
572558
}
573559

574-
// Merge optional style with defaults
575560
val finalStyle = (style ?: TextStyle()).copy(
576561
fontFamily = fontFamily,
577562
fontSize = fontSize,
578563
fontWeight = fontWeight,
579564
color = color
580565
)
581566

582-
// Clickable modifier with ripple
583567
val clickableModifier = if (onClick != null) {
584568
Modifier.clickable(
585569
onClick = onClick,
586570
)
587571
} else Modifier
588572

589573
when (text) {
590-
is String -> {
591-
Text(
592-
text = text,
593-
modifier = modifier.then(clickableModifier),
594-
style = finalStyle,
595-
textAlign = textAlign,
596-
onTextLayout = onTextLayout
597-
)
598-
}
574+
is String -> Text(
575+
text = text,
576+
modifier = modifier.then(clickableModifier),
577+
style = finalStyle,
578+
textAlign = textAlign,
579+
onTextLayout = onTextLayout // nullable is fine for String
580+
)
599581

600-
is AnnotatedString -> {
601-
if (onTextLayout != null) {
602-
Text(
603-
text = text,
604-
modifier = modifier.then(clickableModifier),
605-
style = finalStyle,
606-
textAlign = textAlign,
607-
onTextLayout = onTextLayout
608-
)
609-
}
610-
}
582+
is AnnotatedString -> Text(
583+
text = text,
584+
modifier = modifier.then(clickableModifier),
585+
style = finalStyle,
586+
textAlign = textAlign,
587+
onTextLayout = onTextLayout ?: {} // must be non-null for AnnotatedString
588+
)
611589

612590
else -> throw IllegalArgumentException("FontText supports only String or AnnotatedString")
613591
}

0 commit comments

Comments
 (0)