Fixed MusicSeekSkipTouchListener
This commit is contained in:
parent
5d0b5b91b0
commit
b17013f5bb
1 changed files with 53 additions and 37 deletions
|
@ -1,13 +1,14 @@
|
|||
package code.name.monkey.retromusic.fragments
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.GestureDetector
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
||||
import kotlinx.coroutines.*
|
||||
import kotlin.math.abs
|
||||
|
||||
/**
|
||||
* @param activity, Activity
|
||||
|
@ -16,48 +17,63 @@ import kotlinx.coroutines.*
|
|||
class MusicSeekSkipTouchListener(val activity: FragmentActivity, val next: Boolean) :
|
||||
View.OnTouchListener {
|
||||
|
||||
var job: Job? = null
|
||||
var counter = 0
|
||||
var wasSeeking = false
|
||||
private var job: Job? = null
|
||||
private var counter = 0
|
||||
private var wasSeeking = false
|
||||
|
||||
private val gestureDetector = GestureDetector(activity, object :
|
||||
GestureDetector.SimpleOnGestureListener() {
|
||||
override fun onDown(e: MotionEvent?): Boolean {
|
||||
job = activity.lifecycleScope.launch(Dispatchers.Default) {
|
||||
counter = 0
|
||||
while (isActive) {
|
||||
delay(500)
|
||||
wasSeeking = true
|
||||
var seekingDuration = MusicPlayerRemote.songProgressMillis
|
||||
if (next) {
|
||||
seekingDuration += 5000 * (counter.floorDiv(2) + 1)
|
||||
} else {
|
||||
seekingDuration -= 5000 * (counter.floorDiv(2) + 1)
|
||||
}
|
||||
MusicPlayerRemote.seekTo(seekingDuration)
|
||||
counter += 1
|
||||
}
|
||||
}
|
||||
return super.onDown(e)
|
||||
}
|
||||
})
|
||||
private var startX = 0f
|
||||
private var startY = 0f
|
||||
|
||||
private val scaledTouchSlop = ViewConfiguration.get(activity).scaledTouchSlop
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
|
||||
val action = event?.actionMasked
|
||||
if (action == MotionEvent.ACTION_UP) {
|
||||
job?.cancel()
|
||||
if (v?.isPressed == true && !wasSeeking) {
|
||||
if (next) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
} else {
|
||||
MusicPlayerRemote.back()
|
||||
when (event?.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
startX = event.x
|
||||
startY = event.y
|
||||
job = activity.lifecycleScope.launch(Dispatchers.Default) {
|
||||
counter = 0
|
||||
while (isActive) {
|
||||
delay(500)
|
||||
wasSeeking = true
|
||||
var seekingDuration = MusicPlayerRemote.songProgressMillis
|
||||
if (next) {
|
||||
seekingDuration += 5000 * (counter.floorDiv(2) + 1)
|
||||
} else {
|
||||
seekingDuration -= 5000 * (counter.floorDiv(2) + 1)
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
MusicPlayerRemote.seekTo(seekingDuration)
|
||||
}
|
||||
counter += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
wasSeeking = false
|
||||
} else if (action == MotionEvent.ACTION_CANCEL) {
|
||||
job?.cancel()
|
||||
MotionEvent.ACTION_UP -> {
|
||||
job?.cancel()
|
||||
val endX = event.x
|
||||
val endY = event.y
|
||||
if (!wasSeeking && isAClick(startX, endX, startY, endY)) {
|
||||
if (next) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
} else {
|
||||
MusicPlayerRemote.back()
|
||||
}
|
||||
}
|
||||
|
||||
wasSeeking = false
|
||||
}
|
||||
MotionEvent.ACTION_CANCEL -> {
|
||||
job?.cancel()
|
||||
}
|
||||
}
|
||||
return gestureDetector.onTouchEvent(event)
|
||||
return false
|
||||
}
|
||||
|
||||
private fun isAClick(startX: Float, endX: Float, startY: Float, endY: Float): Boolean {
|
||||
val differenceX = abs(startX - endX)
|
||||
val differenceY = abs(startY - endY)
|
||||
return !(differenceX > scaledTouchSlop || differenceY > scaledTouchSlop)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue