Skip to content

Commit

Permalink
refactor: Adjust brightness and volume gestures' sensitivity
Browse files Browse the repository at this point in the history
remove seek gesture's seek amount that gets displayed, for now.
  • Loading branch information
abdallahmehiz committed Jul 14, 2024
1 parent 160943e commit 0a7c56c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ class PlayerViewModel(
newVolume,
0,
)
println(activity.audioManager.getStreamVolume(AudioManager.STREAM_MUSIC))
currentVolume.update { newVolume }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ fun GestureHandler(
} else {
0
}
println(seekAmount)
viewModel.seekBy(if (isSeekingForwards) doubleTapToSeekDuration else -doubleTapToSeekDuration)
viewModel.showSeekBar()
}
Expand Down Expand Up @@ -158,18 +157,14 @@ fun GestureHandler(
},
onDragEnd = {
viewModel.gestureSeekAmount.update { 0 }
viewModel.hideSeekBar()
viewModel.unpause()
},
) { change, dragAmount ->
if (position >= duration && dragAmount > 0) return@detectHorizontalDragGestures
if (position <= 0f && dragAmount < 0) return@detectHorizontalDragGestures
viewModel.showSeekBar()
val seekBy = (
(dragAmount * 150f / size.width).coerceIn(
0f - position,
duration - position,
)
).toInt()
val seekBy = ((dragAmount * 150f / size.width).coerceIn(0f - position, duration - position)).toInt()
viewModel.seekBy(seekBy)
viewModel.gestureSeekAmount.update { (position - startingPosition).toInt() }
}
Expand All @@ -179,29 +174,39 @@ fun GestureHandler(
var startingY = 0f
var originalVolume = currentVolume
var originalBrightness = currentBrightness
val brightnessGestureSens = 0.001f
val volumeGestureSens = 0.03f
detectVerticalDragGestures(
onDragEnd = { startingY = 0f },
onDragStart = {
startingY = it.y
originalVolume = currentVolume
originalBrightness = currentBrightness
}
},
) { change, amount ->
when {
volumeGesture && brightnessGesture -> {
if (change.position.x < size.width / 2) {
viewModel.changeBrightnessTo(originalBrightness + ((startingY - change.position.y) * 0.005f))
viewModel.changeBrightnessTo(
originalBrightness + ((startingY - change.position.y) * brightnessGestureSens),
)
} else {
viewModel.changeVolumeTo(originalVolume + ((startingY - change.position.y) * 0.1f).toInt())
viewModel.changeVolumeTo(
originalVolume + ((startingY - change.position.y) * volumeGestureSens).toInt(),
)
}
}

brightnessGesture -> {
viewModel.changeBrightnessTo(originalBrightness + ((startingY - change.position.y) * 0.005f))
viewModel.changeBrightnessTo(
originalBrightness + ((startingY - change.position.y) * brightnessGestureSens),
)
}
// it's not always true, AS is drunk
volumeGesture -> {
viewModel.changeVolumeTo(originalVolume + ((startingY - change.position.y) * 0.1f).toInt())
viewModel.changeVolumeTo(
originalVolume + ((startingY - change.position.y) * volumeGestureSens).toInt(),
)
}

else -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -40,14 +39,10 @@ import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import `is`.xyz.mpv.Utils
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -81,14 +76,8 @@ class PlayerControls(private val viewModel: PlayerViewModel) {
val controlsShown by viewModel.controlsShown.collectAsState()
val areControlsLocked by viewModel.areControlsLocked.collectAsState()
val seekBarShown by viewModel.seekBarShown.collectAsState()
val gestureSeekAmount by viewModel.gestureSeekAmount.collectAsState()
val isLoading by viewModel.isLoading.collectAsState()
val duration by viewModel.duration.collectAsState()
LaunchedEffect(gestureSeekAmount) {
if (gestureSeekAmount != 0) return@LaunchedEffect
delay(3000)
viewModel.hideSeekBar()
}
val position by viewModel.pos.collectAsState()
val paused by viewModel.paused.collectAsState()
var isSeeking by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -126,12 +115,13 @@ class PlayerControls(private val viewModel: PlayerViewModel) {
val (bottomRightControls, bottomLeftControls) = createRefs()
val playerPauseButton = createRef()
val seekbar = createRef()
val (seekValue, playerUpdates) = createRefs()
val (playerUpdates) = createRefs()

val isBrightnessSliderShown by viewModel.isBrightnessSliderShown.collectAsState()
val isVolumeSliderShown by viewModel.isVolumeSliderShown.collectAsState()
val brightness by viewModel.currentBrightness.collectAsState()
val volume by viewModel.currentVolume.collectAsState()

LaunchedEffect(volume, brightness) {
delay(1000)
if (isVolumeSliderShown) viewModel.isVolumeSliderShown.update { false }
Expand Down Expand Up @@ -203,24 +193,6 @@ class PlayerControls(private val viewModel: PlayerViewModel) {
onClick = { viewModel.unlockControls() },
)
}
AnimatedVisibility(
visible = gestureSeekAmount != 0 && !areControlsLocked,
enter = fadeIn(),
exit = fadeOut(),
modifier = Modifier.constrainAs(seekValue) {
start.linkTo(parent.absoluteLeft)
end.linkTo(parent.absoluteRight)
bottom.linkTo(seekbar.top)
},
) {
Text(
stringResource(R.string.player_gesture_seek_text, gestureSeekAmount, Utils.prettyTime(position.toInt())),
style = MaterialTheme.typography.headlineMedium.copy(shadow = Shadow(blurRadius = 5f)),
color = Color.White,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
)
}
AnimatedVisibility(
visible = (controlsShown && !areControlsLocked) || isLoading,
enter = fadeIn(),
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<string name="player_sheets_stats_page_chip">Page %d</string>
<string name="player_seek_n_seconds">%d seconds</string>
<string name="player_speed">x%.2f</string>
<string name="player_gesture_seek_text">[%d]\n%s</string>

<string name="player_aspect_fit">Fit Screen</string>
<string name="player_aspect_crop">Crop</string>
Expand Down

0 comments on commit 0a7c56c

Please sign in to comment.