Skip to content

Commit

Permalink
Fix: Slider crash due to IllegalStateException (#515)
Browse files Browse the repository at this point in the history
* refactor: round slider value to fix illegalStateException

* lint: run KtlintFormat

* refactor: use round extension function in preferences

* refactor: remove steps in sliders in compose

* refactor: change round to 1 decimal in speed controls dialog
  • Loading branch information
anilbeesetti authored Aug 21, 2023
1 parent 3b735c7 commit 5faa63a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 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,6 +4,7 @@ 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.common.extensions.round
import dev.anilbeesetti.nextplayer.core.ui.R as coreUiR
import dev.anilbeesetti.nextplayer.feature.player.databinding.PlaybackSpeedBinding

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(1)
speed.addOnChangeListener { _, _, _ ->
val newSpeed = String.format("%.1f", speed.value).toFloat()
val newSpeed = speed.value.round(1)
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(1)
}
}
decSpeed.setOnClickListener {
if (speed.value > 0.2f) {
speed.value -= 0.1f
speed.value = (speed.value - 0.1f).round(1)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.anilbeesetti.nextplayer.core.common.extensions.round
import dev.anilbeesetti.nextplayer.core.model.DoubleTapGesture
import dev.anilbeesetti.nextplayer.core.model.FastSeek
import dev.anilbeesetti.nextplayer.core.model.Resume
Expand All @@ -41,7 +42,6 @@ import dev.anilbeesetti.nextplayer.core.ui.designsystem.NextIcons
import dev.anilbeesetti.nextplayer.settings.composables.OptionsDialog
import dev.anilbeesetti.nextplayer.settings.composables.PreferenceSubtitle
import dev.anilbeesetti.nextplayer.settings.extensions.name
import java.lang.Exception
import java.util.Locale

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -259,11 +259,8 @@ fun PlayerPreferencesScreen(
)
Slider(
value = defaultPlaybackSpeed,
onValueChange = {
defaultPlaybackSpeed = String.format("%.1f", it).toFloat()
},
valueRange = 0.2f..4.0f,
steps = 37
onValueChange = { defaultPlaybackSpeed = it.round(1) },
valueRange = 0.2f..4.0f
)
}
)
Expand Down Expand Up @@ -293,8 +290,7 @@ fun PlayerPreferencesScreen(
Slider(
value = controllerAutoHideSec.toFloat(),
onValueChange = { controllerAutoHideSec = it.toInt() },
valueRange = 1.0f..60.0f,
steps = 60
valueRange = 1.0f..60.0f
)
}
)
Expand Down Expand Up @@ -324,8 +320,7 @@ fun PlayerPreferencesScreen(
Slider(
value = seekIncrement.toFloat(),
onValueChange = { seekIncrement = it.toInt() },
valueRange = 1.0f..60.0f,
steps = 60
valueRange = 1.0f..60.0f
)
}
)
Expand Down Expand Up @@ -437,7 +432,9 @@ fun LazyListScope.autoplaySetting(
isChecked = isChecked,
onClick = onClick
)
} fun LazyListScope.rememberBrightnessSetting(
}

fun LazyListScope.rememberBrightnessSetting(
isChecked: Boolean,
onClick: () -> Unit
) = item {
Expand Down

0 comments on commit 5faa63a

Please sign in to comment.