diff --git a/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Float.kt b/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Float.kt new file mode 100644 index 000000000..3fea975f0 --- /dev/null +++ b/core/common/src/main/java/dev/anilbeesetti/nextplayer/core/common/extensions/Float.kt @@ -0,0 +1,9 @@ +package dev.anilbeesetti.nextplayer.core.common.extensions + +import kotlin.math.pow +import kotlin.math.roundToInt + +fun Float.round(decimalPlaces: Int): Float { + return (this * 10.0.pow(decimalPlaces.toDouble())) + .roundToInt() / 10.0.pow(decimalPlaces.toDouble()).toFloat() +} \ No newline at end of file diff --git a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/dialogs/PlaybackSpeedControlsDialogFragment.kt b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/dialogs/PlaybackSpeedControlsDialogFragment.kt index 5af46df48..999065817 100644 --- a/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/dialogs/PlaybackSpeedControlsDialogFragment.kt +++ b/feature/player/src/main/java/dev/anilbeesetti/nextplayer/feature/player/dialogs/PlaybackSpeedControlsDialogFragment.kt @@ -4,8 +4,9 @@ import android.app.Dialog import android.os.Bundle import androidx.fragment.app.DialogFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder -import dev.anilbeesetti.nextplayer.core.ui.R as coreUiR +import dev.anilbeesetti.nextplayer.core.common.extensions.round import dev.anilbeesetti.nextplayer.feature.player.databinding.PlaybackSpeedBinding +import dev.anilbeesetti.nextplayer.core.ui.R as coreUiR class PlaybackSpeedControlsDialogFragment( private val currentSpeed: Float, @@ -20,9 +21,9 @@ class PlaybackSpeedControlsDialogFragment( return activity?.let { activity -> binding.apply { speedText.text = currentSpeed.toString() - speed.value = currentSpeed + speed.value = currentSpeed.round(2) speed.addOnChangeListener { _, _, _ -> - val newSpeed = String.format("%.1f", speed.value).toFloat() + val newSpeed = speed.value.round(2) onChange(newSpeed) speedText.text = newSpeed.toString() } @@ -31,12 +32,12 @@ class PlaybackSpeedControlsDialogFragment( } incSpeed.setOnClickListener { if (speed.value < 4.0f) { - speed.value += 0.1f + speed.value = (speed.value + 0.1f).round(2) } } decSpeed.setOnClickListener { if (speed.value > 0.2f) { - speed.value -= 0.1f + speed.value = (speed.value - 0.1f).round(2) } } }