Skip to content

Commit

Permalink
refactor: round slider value to fix illegalStateException
Browse files Browse the repository at this point in the history
  • Loading branch information
anilbeesetti committed Aug 18, 2023
1 parent 3c81fec commit 2229b1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
}
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 2229b1e

Please sign in to comment.