Skip to content

Commit

Permalink
Add function to format settings trailing item
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanaia committed Jul 2, 2024
1 parent 33c87e7 commit 6fb8d03
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.joaomanaia.game2048.core.util

import kotlin.text.format

actual fun formatSettingTrailingNumber(value: Float): String {
return "%.2f".format(value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.joaomanaia.game2048.core.util

expect fun formatSettingTrailingNumber(value: Float): String
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.navigation.NavController
import com.joaomanaia.game2048.core.presentation.theme.DarkThemeConfig
import com.joaomanaia.game2048.core.presentation.theme.TileColorsGenerator
import com.joaomanaia.game2048.core.presentation.theme.spacing
import com.joaomanaia.game2048.core.util.formatSettingTrailingNumber
import com.joaomanaia.game2048.presentation.color_settings.components.BaseColorChooser
import com.joaomanaia.game2048.presentation.color_settings.components.DarkThemeDialogPicker
import com.joaomanaia.game2048.presentation.components.BackIconButton
Expand Down Expand Up @@ -247,9 +248,7 @@ private fun SettingsItemSlider(
value: Float,
icon: @Composable (() -> Unit)? = null,
onValueChange: (Float) -> Unit,
// TODO: create a format function
formatTrailingText: (Float) -> String = { it.toString() }
// formatTrailingText: (Float) -> String = { "%.2f".format(it) }
formatTrailingText: (Float) -> String = { formatSettingTrailingNumber(it) }
) {
ListItem(
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.joaomanaia.game2048.core.util

actual fun formatSettingTrailingNumber(value: Float): String {
val integerPart = value.toInt()
val decimalPart = ((value - integerPart) * 100).toInt()

return if (decimalPart < 10) {
"$integerPart.0$decimalPart"
} else {
"$integerPart.$decimalPart"
}
}

0 comments on commit 6fb8d03

Please sign in to comment.