playback: deslop stepper

Actually meaningful improvement to functionality this time.
This commit is contained in:
Alexander Capehart
2026-05-14 20:47:59 -06:00
parent 00ddba54bb
commit b78fb5a6d8
7 changed files with 289 additions and 415 deletions
@@ -807,18 +807,30 @@ constructor(
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true) pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
startPoint.reset() startPoint.reset()
val startPosArr = floatArrayOf(startPoint.posVec.x, startPoint.posVec.y)
val startTanArr = floatArrayOf(startPoint.tanVec.x, startPoint.tanVec.y)
pathMeasure.getPosTan( pathMeasure.getPosTan(
startDistance, startDistance,
floatArrayOf(startPoint.posVec.x, startPoint.posVec.y), startPosArr,
floatArrayOf(startPoint.tanVec.x, startPoint.tanVec.y), startTanArr
)
endPoint.reset()
pathMeasure.getPosTan(
startDistance,
floatArrayOf(endPoint.posVec.x, endPoint.posVec.y),
floatArrayOf(endPoint.tanVec.x, endPoint.tanVec.y),
) )
startPoint.posVec.x = startPosArr[0]
startPoint.posVec.y = startPosArr[1]
startPoint.tanVec.x = startTanArr[0]
startPoint.tanVec.y = startTanArr[1]
endPoint.reset()
val endPosArr = floatArrayOf(endPoint.posVec.x, endPoint.posVec.y)
val endTanArr = floatArrayOf(endPoint.tanVec.x, endPoint.tanVec.y)
pathMeasure.getPosTan(
endDistance,
endPosArr,
endTanArr
)
endPoint.posVec.x = endPosArr[0]
endPoint.posVec.y = endPosArr[1]
endPoint.tanVec.x = endTanArr[0]
endPoint.tanVec.y = endTanArr[1]
transform.reset() transform.reset()
transform.setTranslate(resultTranslationX, baseTranslationY) transform.setTranslate(resultTranslationX, baseTranslationY)
startPoint.translate(resultTranslationX, baseTranslationY) startPoint.translate(resultTranslationX, baseTranslationY)
@@ -41,7 +41,7 @@ import org.oxycblt.auxio.music.resolve
import org.oxycblt.auxio.music.resolveNames import org.oxycblt.auxio.music.resolveNames
import org.oxycblt.auxio.playback.state.RepeatMode import org.oxycblt.auxio.playback.state.RepeatMode
import org.oxycblt.auxio.playback.ui.StyledSeekBar import org.oxycblt.auxio.playback.ui.StyledSeekBar
import org.oxycblt.auxio.playback.ui.stepper.DisplayPortion import org.oxycblt.auxio.playback.ui.stepper.Direction
import org.oxycblt.auxio.playback.ui.stepper.PlayerFastSeekOverlay import org.oxycblt.auxio.playback.ui.stepper.PlayerFastSeekOverlay
import org.oxycblt.auxio.ui.ViewBindingFragment import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.collectImmediately import org.oxycblt.auxio.util.collectImmediately
@@ -104,10 +104,7 @@ class PlaybackPanelFragment :
binding.playbackCover.onSwipeListener = null binding.playbackCover.onSwipeListener = null
// Set up fast seek overlay // Set up fast seek overlay
binding.playbackFastSeekOverlay?.apply { binding.playbackFastSeekOverlay?.performListener = this
performListener(this@PlaybackPanelFragment)
seekSecondsSupplier { 10 } // 10 seconds per double-tap
}
binding.playbackSong.apply { binding.playbackSong.apply {
isSelected = true isSelected = true
setOnClickListener { navigateToCurrentSong() } setOnClickListener { navigateToCurrentSong() }
@@ -279,25 +276,10 @@ class PlaybackPanelFragment :
playbackModel.song.value?.let { detailModel.showAlbum(it.album) } playbackModel.song.value?.let { detailModel.showAlbum(it.album) }
} }
override fun getFastSeekDirection( override fun seek(direction: Direction) {
portion: DisplayPortion when (direction) {
): PlayerFastSeekOverlay.PerformListener.FastSeekDirection { Direction.FORWARDS -> playbackModel.stepForward()
return when (portion) { Direction.BACKWARDS -> playbackModel.stepBackwards()
DisplayPortion.LEFT,
DisplayPortion.LEFT_HALF ->
PlayerFastSeekOverlay.PerformListener.FastSeekDirection.BACKWARD
DisplayPortion.RIGHT,
DisplayPortion.RIGHT_HALF ->
PlayerFastSeekOverlay.PerformListener.FastSeekDirection.FORWARD
else -> PlayerFastSeekOverlay.PerformListener.FastSeekDirection.NONE
}
}
override fun seek(forward: Boolean) {
if (forward) {
playbackModel.stepForward()
} else {
playbackModel.stepBack()
} }
} }
} }
@@ -428,7 +428,7 @@ constructor(
} }
/** Step back by 10 seconds in the current song. */ /** Step back by 10 seconds in the current song. */
fun stepBack() { fun stepBackwards() {
L.d("Stepping back 10 seconds") L.d("Stepping back 10 seconds")
val currentPositionMs = playbackManager.progression.calculateElapsedPositionMs() val currentPositionMs = playbackManager.progression.calculateElapsedPositionMs()
val newPositionMs = (currentPositionMs - 10000).coerceAtLeast(0) val newPositionMs = (currentPositionMs - 10000).coerceAtLeast(0)
@@ -441,7 +441,7 @@ constructor(
val currentPositionMs = playbackManager.progression.calculateElapsedPositionMs() val currentPositionMs = playbackManager.progression.calculateElapsedPositionMs()
val currentSong = playbackManager.currentSong val currentSong = playbackManager.currentSong
if (currentSong != null) { if (currentSong != null) {
val newPositionMs = (currentPositionMs + 10000).coerceAtMost(currentSong.durationMs) val newPositionMs = (currentPositionMs + STEP_INCREMENT).coerceAtMost(currentSong.durationMs)
playbackManager.seekTo(newPositionMs) playbackManager.seekTo(newPositionMs)
} }
} }
@@ -626,6 +626,10 @@ constructor(
} }
_openPanel.put(panel) _openPanel.put(panel)
} }
private companion object {
private const val STEP_INCREMENT = 10000 // ms
}
} }
/** /**
@@ -19,102 +19,95 @@
package org.oxycblt.auxio.playback.ui.stepper package org.oxycblt.auxio.playback.ui.stepper
import android.content.Context import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet import android.util.AttributeSet
import android.view.GestureDetector import android.view.GestureDetector
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MotionEvent import android.view.MotionEvent
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import androidx.dynamicanimation.animation.SpringAnimation
import com.google.android.material.shape.MaterialShapeDrawable import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel import com.google.android.material.shape.ShapeAppearanceModel
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.ui.Effect
import org.oxycblt.auxio.ui.UISettings import org.oxycblt.auxio.ui.UISettings
import timber.log.Timber as L import javax.inject.Inject
enum class DisplayPortion { enum class Direction {
LEFT, FORWARDS,
MIDDLE, BACKWARDS
RIGHT,
LEFT_HALF,
RIGHT_HALF,
}
interface DoubleTapListener {
fun onDoubleTapStarted(portion: DisplayPortion)
fun onDoubleTapProgressDown(portion: DisplayPortion)
fun onDoubleTapFinished()
} }
@AndroidEntryPoint @AndroidEntryPoint
class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) : class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
ConstraintLayout(context, attrs), DoubleTapListener, GestureDetector.OnDoubleTapListener { ConstraintLayout(context, attrs), GestureDetector.OnDoubleTapListener {
private sealed interface OverlayState {
data object Invisible : OverlayState
data class Entering(val tap: SpringAnimation, val seconds: SpringAnimation) : OverlayState
data class Wait(val runnable: Runnable) : OverlayState
data class Exiting(val tap: SpringAnimation, val seconds: SpringAnimation) : OverlayState
}
@Inject lateinit var uiSettings: UISettings @Inject lateinit var uiSettings: UISettings
private var leftSecondsView: SecondsView private var leftSecondsView: SecondsView
private var rightSecondsView: SecondsView private var rightSecondsView: SecondsView
private var leftCircleClipTapView: CircleClipTapView private var leftTapView: TapView
private var rightCircleClipTapView: CircleClipTapView private var rightTapView: TapView
private var rootConstraintLayout: ConstraintLayout
private val gestureDetector: GestureDetector private val gestureDetector: GestureDetector
private val shapeAppearance: ShapeAppearanceModel
var performListener: PerformListener? = null
private val alphaSpring = Effect.FAST
private var leftOverlayState: OverlayState = OverlayState.Invisible
private var rightOverlayState: OverlayState = OverlayState.Invisible
interface PerformListener {
fun seek(direction: Direction)
}
init { init {
LayoutInflater.from(context).inflate(R.layout.player_fast_seek_overlay, this, true) LayoutInflater.from(context).inflate(R.layout.player_fast_seek_overlay, this, true)
// Set up shape appearance based on UISettings similar to CoverView // Set up shape appearance based on UISettings similar to CoverView
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.PlayerFastSeekOverlay) context.withStyledAttributes(attrs, R.styleable.PlayerFastSeekOverlay) {
val shapeAppearanceRes = val shapeAppearanceRes =
styledAttrs.getResourceId(R.styleable.PlayerFastSeekOverlay_shapeAppearance, 0) getResourceId(
R.styleable.PlayerFastSeekOverlay_shapeAppearance,
com.google.android.material.R.style
.ShapeAppearance_Material3_Corner_Medium
)
shapeAppearance = background =
if (uiSettings.roundMode) { MaterialShapeDrawable().apply {
if (shapeAppearanceRes != 0) { shapeAppearanceModel = if (uiSettings.roundMode) {
ShapeAppearanceModel.builder(context, shapeAppearanceRes, -1).build() ShapeAppearanceModel.builder(context, shapeAppearanceRes, -1).build()
} else {
ShapeAppearanceModel.builder(
context,
com.google.android.material.R.style
.ShapeAppearance_Material3_Corner_Medium,
-1,
)
.build()
}
} else { } else {
ShapeAppearanceModel.builder().build() ShapeAppearanceModel.builder().build()
} }
// Set transparent background as we only want the clipping
styledAttrs.recycle() fillColor = ColorStateList.valueOf(Color.TRANSPARENT)
}
}
leftSecondsView = findViewById(R.id.left_seconds_view) leftSecondsView = findViewById(R.id.left_seconds_view)
rightSecondsView = findViewById(R.id.right_seconds_view) rightSecondsView = findViewById(R.id.right_seconds_view)
leftCircleClipTapView = findViewById(R.id.left_circle_clip_tap_view) leftTapView = findViewById(R.id.left_circle_clip_tap_view)
rightCircleClipTapView = findViewById(R.id.right_circle_clip_tap_view) rightTapView = findViewById(R.id.right_circle_clip_tap_view)
rootConstraintLayout = findViewById(R.id.root_constraint_layout)
// Apply shape clipping to the overlay // Apply shape clipping to the overlay
clipToOutline = true clipToOutline = true
background =
MaterialShapeDrawable().apply {
shapeAppearanceModel = shapeAppearance
// Set transparent background as we only want the clipping
fillColor =
android.content.res.ColorStateList.valueOf(android.graphics.Color.TRANSPARENT)
}
// Hide overlay elements by default // Hide overlay elements by default
leftSecondsView.alpha = 0f leftSecondsView.alpha = 0f
rightSecondsView.alpha = 0f rightSecondsView.alpha = 0f
leftCircleClipTapView.alpha = 0f leftTapView.alpha = 0f
rightCircleClipTapView.alpha = 0f rightTapView.alpha = 0f
leftSecondsView.visibility = INVISIBLE
rightSecondsView.visibility = INVISIBLE
leftCircleClipTapView.visibility = INVISIBLE
rightCircleClipTapView.visibility = INVISIBLE
// Set up gesture detection // Set up gesture detection
gestureDetector = gestureDetector =
@@ -127,243 +120,170 @@ class PlayerFastSeekOverlay(context: Context, attrs: AttributeSet?) :
gestureDetector.setOnDoubleTapListener(this) gestureDetector.setOnDoubleTapListener(this)
addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ -> addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
leftCircleClipTapView.updateArcSize(view) leftTapView.updateArcSize(view)
rightCircleClipTapView.updateArcSize(view) rightTapView.updateArcSize(view)
} }
// Initialize directions for each side // Initialize directions for each side
leftSecondsView.setForwarding(false) leftSecondsView.setDirection(Direction.BACKWARDS)
rightSecondsView.setForwarding(true) rightSecondsView.setDirection(Direction.FORWARDS)
leftCircleClipTapView.updatePosition(true) // isLeft = true for left side leftTapView.setDirection(Direction.BACKWARDS) // isLeft = true for left side
rightCircleClipTapView.updatePosition(false) // isLeft = false for right side rightTapView.setDirection(Direction.FORWARDS) // isLeft = false for right side
} }
private var performListener: PerformListener? = null
fun performListener(listener: PerformListener?) = apply { performListener = listener }
private var seekSecondsSupplier: () -> Int = { 0 }
fun seekSecondsSupplier(supplier: (() -> Int)?) = apply {
seekSecondsSupplier = supplier ?: { 0 }
}
// Track state for left overlay
private var leftInitTap: Boolean = false
private var leftIsAnimatingIn: Boolean = false
private var leftFadeOutRunnable: Runnable? = null
// Track state for right overlay
private var rightInitTap: Boolean = false
private var rightIsAnimatingIn: Boolean = false
private var rightFadeOutRunnable: Runnable? = null
override fun onDoubleTapStarted(portion: DisplayPortion) {
L.d("onDoubleTapStarted called with portion = [$portion]")
val shouldForward: Boolean =
performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return
if (shouldForward) {
rightInitTap = false
rightSecondsView.stopAnimation()
} else {
leftInitTap = false
leftSecondsView.stopAnimation()
}
}
override fun onDoubleTapProgressDown(portion: DisplayPortion) {
val shouldForward: Boolean =
performListener?.getFastSeekDirection(portion)?.directionAsBoolean ?: return
L.d("onDoubleTapProgressDown called with shouldForward = [$shouldForward]")
if (shouldForward) {
handleRightOverlayTap()
} else {
handleLeftOverlayTap()
}
performListener?.seek(forward = shouldForward)
}
private fun handleLeftOverlayTap() {
// Cancel any pending fade out for left overlay
leftFadeOutRunnable?.let {
removeCallbacks(it)
leftFadeOutRunnable = null
}
if (!leftInitTap) {
// Reset seconds for new tap series
leftSecondsView.seconds = 0
leftInitTap = true
// Only fade in if we're not already visible and animating
if (!leftIsAnimatingIn && leftSecondsView.alpha < 1f) {
leftIsAnimatingIn = true
leftSecondsView.visibility = VISIBLE
leftCircleClipTapView.visibility = VISIBLE
leftSecondsView.startAnimation()
leftSecondsView
.animate()
.alpha(1f)
.setDuration(200)
.withEndAction { leftIsAnimatingIn = false }
.start()
leftCircleClipTapView.animate().alpha(1f).setDuration(200).start()
}
} else {
// For rapid taps, ensure we're fully visible without re-animating
if (leftSecondsView.alpha < 1f && !leftIsAnimatingIn) {
leftSecondsView.alpha = 1f
leftCircleClipTapView.alpha = 1f
leftSecondsView.visibility = VISIBLE
leftCircleClipTapView.visibility = VISIBLE
}
}
leftSecondsView.seconds += seekSecondsSupplier.invoke()
}
private fun handleRightOverlayTap() {
// Cancel any pending fade out for right overlay
rightFadeOutRunnable?.let {
removeCallbacks(it)
rightFadeOutRunnable = null
}
if (!rightInitTap) {
// Reset seconds for new tap series
rightSecondsView.seconds = 0
rightInitTap = true
// Only fade in if we're not already visible and animating
if (!rightIsAnimatingIn && rightSecondsView.alpha < 1f) {
rightIsAnimatingIn = true
rightSecondsView.visibility = VISIBLE
rightCircleClipTapView.visibility = VISIBLE
rightSecondsView.startAnimation()
rightSecondsView
.animate()
.alpha(1f)
.setDuration(200)
.withEndAction { rightIsAnimatingIn = false }
.start()
rightCircleClipTapView.animate().alpha(1f).setDuration(200).start()
}
} else {
// For rapid taps, ensure we're fully visible without re-animating
if (rightSecondsView.alpha < 1f && !rightIsAnimatingIn) {
rightSecondsView.alpha = 1f
rightCircleClipTapView.alpha = 1f
rightSecondsView.visibility = VISIBLE
rightCircleClipTapView.visibility = VISIBLE
}
}
rightSecondsView.seconds += seekSecondsSupplier.invoke()
}
override fun onDoubleTapFinished() {
L.d("onDoubleTapFinished called")
// Handle left overlay fade out
if (leftInitTap) {
leftInitTap = false
leftSecondsView.stopAnimation()
// Cancel any existing fade out
leftFadeOutRunnable?.let { removeCallbacks(it) }
// Schedule fade out for left overlay after a delay
leftFadeOutRunnable = Runnable {
leftIsAnimatingIn = false
leftSecondsView
.animate()
.alpha(0f)
.setDuration(200)
.withEndAction { leftSecondsView.visibility = INVISIBLE }
.start()
leftCircleClipTapView
.animate()
.alpha(0f)
.setDuration(200)
.withEndAction { leftCircleClipTapView.visibility = INVISIBLE }
.start()
leftFadeOutRunnable = null
}
postDelayed(leftFadeOutRunnable, 400)
}
// Handle right overlay fade out
if (rightInitTap) {
rightInitTap = false
rightSecondsView.stopAnimation()
// Cancel any existing fade out
rightFadeOutRunnable?.let { removeCallbacks(it) }
// Schedule fade out for right overlay after a delay
rightFadeOutRunnable = Runnable {
rightIsAnimatingIn = false
rightSecondsView
.animate()
.alpha(0f)
.setDuration(200)
.withEndAction { rightSecondsView.visibility = INVISIBLE }
.start()
rightCircleClipTapView
.animate()
.alpha(0f)
.setDuration(200)
.withEndAction { rightCircleClipTapView.visibility = INVISIBLE }
.start()
rightFadeOutRunnable = null
}
postDelayed(rightFadeOutRunnable, 400)
}
}
interface PerformListener {
/** Determines if the playback should forward/rewind or do nothing. */
fun getFastSeekDirection(portion: DisplayPortion): FastSeekDirection
fun seek(forward: Boolean)
enum class FastSeekDirection(val directionAsBoolean: Boolean?) {
NONE(null),
FORWARD(true),
BACKWARD(false),
}
}
override fun onTouchEvent(event: MotionEvent): Boolean = override fun onTouchEvent(event: MotionEvent): Boolean =
gestureDetector.onTouchEvent(event) || super.onTouchEvent(event) gestureDetector.onTouchEvent(event) || super.onTouchEvent(event)
override fun onSingleTapConfirmed(e: MotionEvent) = false override fun onSingleTapConfirmed(e: MotionEvent) = false
private fun enter(secondsView: SecondsView, tapView: TapView, overlayState: (OverlayState) -> Unit) {
// start animating the seconds icon
secondsView.startAnimation()
val secondsIn = alphaSpring.alpha(secondsView, 1.0f)
val tapIn = alphaSpring.alpha(tapView, 1.0f)
// will execute after a delay
val out = Runnable {
exit(secondsView, tapView, overlayState)
}
tapIn.addEndListener { _, cancelled, _, _ ->
if (!cancelled) {
postDelayed(out, EXIT_DELAY_MS)
overlayState(OverlayState.Wait(out))
}
}
overlayState(OverlayState.Entering(secondsIn, tapIn))
}
private fun exit(secondsView: SecondsView, tapView: TapView, overlayState: (OverlayState) -> Unit) {
val secondsOut = alphaSpring.alpha(secondsView, 0.0f)
val tapOut = alphaSpring.alpha(tapView, 0.0f)
tapOut.addEndListener { _, cancelled, _, _ ->
if (!cancelled) {
secondsView.stopAnimation()
secondsView.seconds = 0
overlayState(OverlayState.Invisible)
}
}
overlayState(OverlayState.Exiting(secondsOut, tapOut))
}
private fun tap(
tappedSecondsView: SecondsView,
tappedTapView: TapView,
tappedOverlayState: OverlayState,
setTappedOverlayState: (OverlayState) -> Unit,
idleSecondsView: SecondsView,
idleTapView: TapView,
idleOverlayState: OverlayState,
setIdleOverlayState: (OverlayState) -> Unit,
) {
// (re)activate the side that was tapped on
when (tappedOverlayState) {
is OverlayState.Invisible -> {
enter(tappedSecondsView, tappedTapView, setTappedOverlayState)
}
is OverlayState.Entering -> {
// do nothing
}
is OverlayState.Wait -> {
// refresh the exit timeout
removeCallbacks(tappedOverlayState.runnable)
postDelayed(tappedOverlayState.runnable, EXIT_DELAY_MS)
}
is OverlayState.Exiting -> {
// cancel exit and re-enter
tappedOverlayState.seconds.cancel()
tappedOverlayState.tap.cancel()
enter(tappedSecondsView, tappedTapView, setTappedOverlayState)
}
}
// clear out the idle side if it's active
when (idleOverlayState) {
is OverlayState.Invisible -> {
// nothing to clear
}
is OverlayState.Entering -> {
// cancel enter and exit immediately
idleOverlayState.seconds.cancel()
idleOverlayState.tap.cancel()
exit(idleSecondsView, idleTapView, setIdleOverlayState)
}
is OverlayState.Wait -> {
// dont wait for an exit, exit immediately
removeCallbacks(idleOverlayState.runnable)
exit(idleSecondsView, idleTapView, setIdleOverlayState)
}
is OverlayState.Exiting -> {
// nothing to clear
}
}
// update seconds from tap - reset when exit anim clears
// largely implicit shared understanding that seeking occurs by 10s
// todo: probably share this constant
tappedSecondsView.seconds += 10
}
override fun onDoubleTap(e: MotionEvent): Boolean { override fun onDoubleTap(e: MotionEvent): Boolean {
val width = width val width = width
val tapX = e.x val tapX = e.x
val portion = val direction =
when { when {
tapX < width * 0.33f -> DisplayPortion.LEFT_HALF tapX < width * 0.5 -> Direction.BACKWARDS
tapX > width * 0.67f -> DisplayPortion.RIGHT_HALF tapX > width * 0.5 -> Direction.FORWARDS
else -> DisplayPortion.MIDDLE // dont care
else -> return false
} }
if (portion != DisplayPortion.MIDDLE) { // a bit duplicated but already the generic `enter`/`exit` logic
onDoubleTapStarted(portion) // requires a good amount of params and trying to share the helpe
onDoubleTapProgressDown(portion) when (direction) {
onDoubleTapFinished() Direction.BACKWARDS -> {
return true tap(
leftSecondsView,
leftTapView,
leftOverlayState,
{ leftOverlayState = it },
rightSecondsView,
rightTapView,
rightOverlayState,
{ rightOverlayState = it }
)
}
Direction.FORWARDS -> {
// forwards, we tap on the right
tap(
rightSecondsView,
rightTapView,
rightOverlayState,
{ rightOverlayState = it },
leftSecondsView,
leftTapView,
leftOverlayState,
{ leftOverlayState = it },
)
}
} }
performListener?.seek(direction)
return false return false
} }
override fun onDoubleTapEvent(e: MotionEvent) = false override fun onDoubleTapEvent(e: MotionEvent) = false
private companion object {
private const val EXIT_DELAY_MS = 500L
}
} }
@@ -25,25 +25,14 @@ import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.core.animation.addListener import androidx.core.animation.addListener
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.SpringAnimation
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.PlayerFastSeekSecondsViewBinding import org.oxycblt.auxio.databinding.PlayerFastSeekSecondsViewBinding
import org.oxycblt.auxio.ui.Effect
class SecondsView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) { class SecondsView(context: Context, attrs: AttributeSet?) : LinearLayout(context, attrs) {
companion object {
const val ICON_ANIMATION_DURATION = 750L
}
var cycleDuration: Long = ICON_ANIMATION_DURATION
set(value) {
firstAnimator.duration = value / 5
secondAnimator.duration = value / 5
thirdAnimator.duration = value / 5
fourthAnimator.duration = value / 5
fifthAnimator.duration = value / 5
field = value
}
var seconds: Int = 0 var seconds: Int = 0
set(value) { set(value) {
binding.tvSeconds.text = binding.tvSeconds.text =
@@ -59,22 +48,34 @@ class SecondsView(context: Context, attrs: AttributeSet?) : LinearLayout(context
1F, 1F,
) != 0F ) != 0F
private val alphaSpring = Effect.FAST
private var animations: List<SpringAnimation> = listOf()
val binding = PlayerFastSeekSecondsViewBinding.inflate(LayoutInflater.from(context), this) val binding = PlayerFastSeekSecondsViewBinding.inflate(LayoutInflater.from(context), this)
init { init {
orientation = VERTICAL orientation = VERTICAL
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
reset()
} }
fun setForwarding(isForward: Boolean) { fun setDirection(direction: Direction) {
binding.triangleContainer.rotation = if (isForward) 0f else 180f // easy flip if in the other direction, dont have to change icons or anim order
binding.triangleContainer.rotation = if (direction == Direction.FORWARDS) 0f else 180f
} }
fun startAnimation() { fun startAnimation() {
stopAnimation() if (animations.any { it.isRunning }) {
// animations ongoing
// we never try to stop ongoing animations when this is
// called again, as that will likely result in the indicator always
// resetting
// TODO: might want to change this
return
}
reset()
if (animationsEnabled) { if (animationsEnabled) {
firstAnimator.start() cycle()
} else { } else {
// If no animations are enable show the arrow(s) without animation // If no animations are enable show the arrow(s) without animation
showWithoutAnimation() showWithoutAnimation()
@@ -82,12 +83,7 @@ class SecondsView(context: Context, attrs: AttributeSet?) : LinearLayout(context
} }
fun stopAnimation() { fun stopAnimation() {
firstAnimator.cancel() animations.forEach { it.cancel() }
secondAnimator.cancel()
thirdAnimator.cancel()
fourthAnimator.cancel()
fifthAnimator.cancel()
reset() reset()
} }
@@ -103,76 +99,38 @@ class SecondsView(context: Context, attrs: AttributeSet?) : LinearLayout(context
binding.icon3.alpha = 1f binding.icon3.alpha = 1f
} }
private val firstAnimator: ValueAnimator = // cycle animation for the 3 icons
CustomValueAnimator( private val cyclicAlpha = floatArrayOf(
{ 1.00f, 0.25f, 0.25f,
binding.icon1.alpha = 0f 1.00f, 1.00f, 0.25f,
binding.icon2.alpha = 0f 0.25f, 1.00f, 1.00f,
binding.icon3.alpha = 0f 0.25f, 0.25f, 1.00f,
}, 0.25f, 0.25f, 0.25f,
{ binding.icon1.alpha = it }, )
{ secondAnimator.start() },
)
private val secondAnimator: ValueAnimator = private fun cycle(at: Int = 0) {
CustomValueAnimator( animations.forEach { it.cancel() }
{ val row = at * 3
binding.icon1.alpha = 1f val one = alphaSpring.alpha(binding.icon1, cyclicAlpha[row])
binding.icon2.alpha = 0f val two = alphaSpring.alpha(binding.icon2, cyclicAlpha[row + 1])
binding.icon3.alpha = 0f val three = alphaSpring.alpha(binding.icon3, cyclicAlpha[row + 2])
}, val endListener = DynamicAnimation.OnAnimationEndListener { _, cancelled, _, _ ->
{ binding.icon2.alpha = it }, if (!cancelled) {
{ thirdAnimator.start() }, cycle((at + 1) % 5)
) }
private val thirdAnimator: ValueAnimator =
CustomValueAnimator(
{
binding.icon1.alpha = 1f
binding.icon2.alpha = 1f
binding.icon3.alpha = 0f
},
{
binding.icon1.alpha = 1f - binding.icon3.alpha
binding.icon3.alpha = it
},
{ fourthAnimator.start() },
)
private val fourthAnimator: ValueAnimator =
CustomValueAnimator(
{
binding.icon1.alpha = 0f
binding.icon2.alpha = 1f
binding.icon3.alpha = 1f
},
{ binding.icon2.alpha = 1f - it },
{ fifthAnimator.start() },
)
private val fifthAnimator: ValueAnimator =
CustomValueAnimator(
{
binding.icon1.alpha = 0f
binding.icon2.alpha = 0f
binding.icon3.alpha = 1f
},
{ binding.icon3.alpha = 1f - it },
{ firstAnimator.start() },
)
private inner class CustomValueAnimator(
start: () -> Unit,
update: (value: Float) -> Unit,
end: () -> Unit,
) : ValueAnimator() {
init {
duration = cycleDuration / 5
setFloatValues(0f, 1f)
addUpdateListener { update(it.animatedValue as Float) }
addListener(onStart = { start() }, onEnd = { end() })
} }
when {
cyclicAlpha[row] != binding.icon1.alpha -> {
one.addEndListener(endListener)
}
cyclicAlpha[row + 1] != binding.icon2.alpha -> {
two.addEndListener(endListener)
}
cyclicAlpha[row + 2] != binding.icon3.alpha -> {
three.addEndListener(endListener)
}
}
animations = listOf(one, two, three)
} }
} }
@@ -25,7 +25,10 @@ import android.graphics.Path
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
class CircleClipTapView(context: Context?, attrs: AttributeSet) : View(context, attrs) { /**
* Cricle
*/
class TapView(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var backgroundPaint = Paint() private var backgroundPaint = Paint()
@@ -33,14 +36,11 @@ class CircleClipTapView(context: Context?, attrs: AttributeSet) : View(context,
private var heightPx = 0 private var heightPx = 0
// Background // Background
private var shapePath = Path() private var shapePath = Path()
private var arcSize: Float = 80f private var arcSize: Float = 80f
private var isLeft = true private var direction = Direction.FORWARDS
init { init {
requireNotNull(context) { "Context is null." }
backgroundPaint.apply { backgroundPaint.apply {
style = Paint.Style.FILL style = Paint.Style.FILL
isAntiAlias = true isAntiAlias = true
@@ -62,11 +62,9 @@ class CircleClipTapView(context: Context?, attrs: AttributeSet) : View(context,
} }
} }
fun updatePosition(newIsLeft: Boolean) { fun setDirection(newDirection: Direction) {
if (isLeft != newIsLeft) { direction = newDirection
isLeft = newIsLeft updatePathShape()
updatePathShape()
}
} }
private fun updatePathShape() { private fun updatePathShape() {
@@ -74,8 +72,8 @@ class CircleClipTapView(context: Context?, attrs: AttributeSet) : View(context,
shapePath.reset() shapePath.reset()
val w = if (isLeft) 0f else widthPx.toFloat() val w = if (direction == Direction.BACKWARDS) 0f else widthPx.toFloat()
val f = if (isLeft) 1 else -1 val f = if (direction == Direction.BACKWARDS) 1 else -1
shapePath.moveTo(w, 0f) shapePath.moveTo(w, 0f)
shapePath.lineTo(f * (halfWidth - arcSize) + w, 0f) shapePath.lineTo(f * (halfWidth - arcSize) + w, 0f)
@@ -6,7 +6,7 @@
android:layout_height="match_parent"> android:layout_height="match_parent">
<!-- Left overlay views --> <!-- Left overlay views -->
<org.oxycblt.auxio.playback.ui.stepper.CircleClipTapView <org.oxycblt.auxio.playback.ui.stepper.TapView
android:id="@+id/left_circle_clip_tap_view" android:id="@+id/left_circle_clip_tap_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -26,7 +26,7 @@
app:layout_constraintWidth_percent="0.5" /> app:layout_constraintWidth_percent="0.5" />
<!-- Right overlay views --> <!-- Right overlay views -->
<org.oxycblt.auxio.playback.ui.stepper.CircleClipTapView <org.oxycblt.auxio.playback.ui.stepper.TapView
android:id="@+id/right_circle_clip_tap_view" android:id="@+id/right_circle_clip_tap_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"