Skip to content

Commit f70b7d6

Browse files
Refactor: Refactor settings font and icon sizing logic
This commit refactors the sizing logic for fonts and icons within the `SettingsFragment`. Previously, font and icon sizes were calculated based on a fixed offset and involved a custom `tuToDp` conversion function. The new implementation simplifies this by using `derivedStateOf` to dynamically calculate `titleFontSize`, `descriptionFontSize`, and `iconSize` based on the `selectedSettingsSize`. The redundant `tuToDp` composable function has been removed and replaced with a more concise `TextUnit.toDp()` extension function, cleaning up the code and improving readability.
1 parent 743e1b2 commit f70b7d6

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ import androidx.compose.ui.graphics.Color
4040
import androidx.compose.ui.platform.LocalContext
4141
import androidx.compose.ui.platform.LocalDensity
4242
import androidx.compose.ui.res.dimensionResource
43+
import androidx.compose.ui.unit.Density
4344
import androidx.compose.ui.unit.Dp
4445
import androidx.compose.ui.unit.TextUnit
4546
import androidx.compose.ui.unit.dp
46-
import androidx.compose.ui.unit.isSpecified
4747
import androidx.compose.ui.unit.sp
4848
import androidx.core.graphics.createBitmap
4949
import androidx.core.graphics.drawable.toDrawable
@@ -156,30 +156,33 @@ class SettingsFragment : BaseFragment() {
156156
}
157157

158158
setThemeMode(requireContext(), isDark, binding.settingsView)
159-
val settingsSize = (prefs.settingsSize - 3)
160-
161159
SettingsTheme(isDark) {
162-
Settings(settingsSize.sp)
160+
Settings()
163161
}
164162
}
165163
}
166164

167165
@Composable
168-
private fun Settings(fontSize: TextUnit = TextUnit.Unspecified) {
166+
private fun Settings() {
167+
var selectedSettingsSize by remember { mutableIntStateOf(prefs.settingsSize) }
169168
var toggledPrivateSpaces by remember { mutableStateOf(PrivateSpaceManager(requireContext()).isPrivateSpaceLocked()) }
170-
val fs = remember { mutableStateOf(fontSize) }
171169

172-
val titleFontSize = if (fs.value.isSpecified) {
173-
(fs.value.value * 1.5).sp
174-
} else fs.value
170+
// Derived states that recompute whenever selectedSize changes
171+
val titleFontSize by remember {
172+
derivedStateOf { (selectedSettingsSize.sp) }
173+
}
174+
175+
val descriptionFontSize by remember {
176+
derivedStateOf { (selectedSettingsSize.sp * 0.8f) }
177+
}
175178

176-
val descriptionFontSize = if (fs.value.isSpecified) {
177-
(fs.value.value * 1.2).sp
178-
} else fs.value
179+
val density = LocalDensity.current
179180

180-
val iconSize = if (fs.value.isSpecified) {
181-
tuToDp((fs.value * 0.8))
182-
} else tuToDp(fs.value)
181+
val iconSize by remember(selectedSettingsSize) {
182+
derivedStateOf {
183+
(selectedSettingsSize.sp * 1.8f).toDp(density)
184+
}
185+
}
183186

184187
val context = LocalContext.current
185188
val scrollState = rememberScrollState()
@@ -347,7 +350,6 @@ class SettingsFragment : BaseFragment() {
347350

348351
// Experimental Settings
349352
var toggledExpertOptions by remember { mutableStateOf(prefs.enableExpertOptions) }
350-
var selectedSettingsSize by remember { mutableIntStateOf(prefs.settingsSize) }
351353
var toggledForceWallpaper by remember { mutableStateOf(prefs.forceWallpaper) }
352354
var toggledSettingsLocked by remember { mutableStateOf(prefs.settingsLocked) }
353355
var toggledLockOrientation by remember { mutableStateOf(prefs.lockOrientation) }
@@ -2843,14 +2845,6 @@ class SettingsFragment : BaseFragment() {
28432845
}
28442846
}
28452847

2846-
@Composable
2847-
fun tuToDp(textUnit: TextUnit): Dp {
2848-
val density = LocalDensity.current.density
2849-
val scaledDensity = LocalDensity.current.fontScale
2850-
val dpValue = textUnit.value * (density / scaledDensity)
2851-
return dpValue.dp // Convert to Dp using the 'dp' extension
2852-
}
2853-
28542848
private fun dismissDialogs() {
28552849
dialogBuilder.backupRestoreBottomSheet?.dismiss()
28562850
dialogBuilder.saveLoadThemeBottomSheet?.dismiss()
@@ -3026,3 +3020,5 @@ class SettingsFragment : BaseFragment() {
30263020
return bitmap
30273021
}
30283022
}
3023+
3024+
private fun TextUnit.toDp(density: Density): Dp = with(density) { this@toDp.toDp() }

0 commit comments

Comments
 (0)