Skip to content

Commit

Permalink
feat: display volume as percentage setting
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Sep 8, 2024
1 parent efbfddc commit 30f8e34
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PlayerPreferences(
"default_speed_presets",
setOf("0.25", "0.5", "0.75", "1.0", "1.25", "1.5", "1.75", "2.0", "2.5", "3.0", "3.5", "4.0")
)
val displayVolumeAsPercentage = preferenceStore.getBoolean("display_volume_as_percentage", true)
val savePositionOnQuit = preferenceStore.getBoolean("save_position", true)

val automaticallyEnterPip = preferenceStore.getBoolean("automatic_pip")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ class PlayerActivity : AppCompatActivity() {
if (it < viewModel.maxVolume) viewModel.changeMPVVolumeTo(100)
}
}
viewModel.changeBrightnessTo(
viewModel.currentBrightness.update {
Settings.System.getFloat(contentResolver, Settings.System.SCREEN_BRIGHTNESS)
.normalize(0f, 255f, 0f, 1f),
)
.normalize(0f, 255f, 0f, 1f)
}
}

private fun setupIntents(intent: Intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class PlayerViewModel(
fun changeBrightnessTo(
brightness: Float,
) {
isBrightnessSliderShown.update { true }
currentBrightness.update { brightness.coerceIn(0f, 1f) }
activity.window.attributes = activity.window.attributes.apply {
screenBrightness = brightness.coerceIn(0f, 1f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ fun GestureHandler(modifier: Modifier = Modifier) {
}
val changeBrightness: () -> Unit = {
if (startingY == 0f) startingY = change.position.y
calculateNewValue(originalBrightness, startingY, change.position.y, brightnessGestureSens)
viewModel.changeBrightnessTo(
calculateNewValue(originalBrightness, startingY, change.position.y, brightnessGestureSens)
)
}
when {
volumeGesture && brightnessGesture -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ fun PlayerControls(
},
) {
val boostCap by audioPreferences.volumeBoostCap.collectAsState()
val displayVolumeAsPercentage by playerPreferences.displayVolumeAsPercentage.collectAsState()
VolumeSlider(
volume,
mpvVolume = mpvVolume,
range = 0..viewModel.maxVolume,
boostRange = if (boostCap > 0) 0..audioPreferences.volumeBoostCap.get() else null
boostRange = if (boostCap > 0) 0..audioPreferences.volumeBoostCap.get() else null,
displayAsPercentage = displayVolumeAsPercentage
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import live.mehiz.mpvkt.R
import live.mehiz.mpvkt.ui.theme.spacing
import kotlin.math.abs
import kotlin.math.roundToInt

fun percentage(value: Float, range: ClosedFloatingPointRange<Float>): Float {
return ((value - range.start) / (range.endInclusive - range.start)).coerceIn(0f, 1f)
Expand Down Expand Up @@ -154,37 +157,69 @@ fun VolumeSlider(
range: ClosedRange<Int>,
boostRange: ClosedRange<Int>?,
modifier: Modifier = Modifier,
displayAsPercentage: Boolean = false,
) {
val percentage = (percentage(volume, range) * 100).roundToInt()
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.smaller),
) {
val boostVolume = mpvVolume - 100
Text(
when (mpvVolume - 100) {
0 -> "$volume"
in 0..1000 -> "$volume + $boostVolume"
in -100..-1 -> "$volume - ${abs(boostVolume)}"
else -> "$volume ($mpvVolume)"
},
getVolumeSliderText(volume, mpvVolume, boostVolume, percentage, displayAsPercentage),
style = MaterialTheme.typography.bodySmall,
)
VerticalSlider(
volume,
range,
if (displayAsPercentage) percentage else volume,
if (displayAsPercentage) 0..100 else range,
overflowValue = boostVolume,
overflowRange = boostRange,
)
Icon(
when (percentage(volume, range)) {
0f -> Icons.AutoMirrored.Default.VolumeOff
in 0f..0.3f -> Icons.AutoMirrored.Default.VolumeMute
in 0.3f..0.6f -> Icons.AutoMirrored.Default.VolumeDown
in 0.6f..1f -> Icons.AutoMirrored.Default.VolumeUp
when (percentage) {
0 -> Icons.AutoMirrored.Default.VolumeOff
in 0..30 -> Icons.AutoMirrored.Default.VolumeMute
in 30..60 -> Icons.AutoMirrored.Default.VolumeDown
in 60..100 -> Icons.AutoMirrored.Default.VolumeUp
else -> Icons.AutoMirrored.Default.VolumeOff
},
null,
)
}
}

val getVolumeSliderText: @Composable (Int, Int, Int, Int, Boolean) -> String =
{ volume, mpvVolume, boostVolume, percentage, displayAsPercentage ->
when (mpvVolume - 100) {
0 -> if (displayAsPercentage) {
stringResource(R.string.value_percentage_int, percentage)
} else {
"$volume"
}

in 0..1000 -> {
if (displayAsPercentage) {
stringResource(R.string.volume_slider_percentage_positive_boost, percentage, boostVolume)
} else {
stringResource(R.string.volume_slider_steps_positive_boost, volume, boostVolume)
}
}

in -100..-1 -> {
if (displayAsPercentage) {
stringResource(R.string.volume_slider_percentage_negative_boost, percentage, abs(boostVolume))
} else {
stringResource(R.string.volume_slider_steps_negative_boost, volume, abs(boostVolume))
}
}

else -> {
if (displayAsPercentage) {
stringResource(R.string.volume_slider_percentage_other, percentage, boostVolume)
} else {
stringResource(R.string.volume_slider_steps_other, volume, boostVolume)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ object PlayerPreferencesScreen : Screen() {
onValueChange = preferences.allowGesturesInPanels::set,
title = { Text(text = stringResource(id = R.string.pref_player_controls_allow_gestures_in_panels)) },
)
val displayVolumeAsPercentage by preferences.displayVolumeAsPercentage.collectAsState()
SwitchPreference(
value = displayVolumeAsPercentage,
onValueChange = preferences.displayVolumeAsPercentage::set,
title = { Text(stringResource(R.string.pref_player_display_volume_as_percentage)) },
)
val showChaptersButton by preferences.showChaptersButton.collectAsState()
SwitchPreference(
value = showChaptersButton,
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<string name="pref_player_precise_seeking_summary">Seek to exact positions instead of keyframes (slow)</string>
<string name="pref_player_automatically_enter_pip">Automatically switch to PiP</string>
<string name="pref_player_close_after_eof">Close after the end of playback</string>
<string name="pref_player_display_volume_as_percentage">Display volume as percentage</string>

<string name="pref_player_gestures">Gestures</string>
<string name="pref_player_gestures_seek">Horizontal seek gestures</string>
Expand Down Expand Up @@ -190,6 +191,16 @@

<string name="toast_sleep_timer_ended">Sleep timer ended</string>

<string name="volume_slider_percentage_positive_boost" translatable="false">%d%% + %d</string>
<string name="volume_slider_percentage_negative_boost" translatable="false">%d%% - %d</string>
<string name="volume_slider_percentage_other" translatable="false">%d%% (%d)</string>
<string name="volume_slider_steps_positive_boost" translatable="false">%d + %d</string>
<string name="volume_slider_steps_negative_boost" translatable="false">%d - %d</string>
<string name="volume_slider_steps_other" translatable="false">%d (%d)</string>

<string name="value_percentage_int" translatable="false">%d%%</string>
<string name="value_percentage_float" translatable="false">%.2f\%%</string>

<string name="github_repo_url" translatable="false">https://github.com/abdallahmehiz/mpvKt</string>

<plurals name="plural_items">
Expand Down

0 comments on commit 30f8e34

Please sign in to comment.