Skip to content

Refactor de OnSwipeTouchListener y spin de la ruleta hacia la derecha #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import kotlin.math.abs

open class OnSwipeTouchListener(context: Context) : View.OnTouchListener {

Expand Down Expand Up @@ -35,32 +36,25 @@ open class OnSwipeTouchListener(context: Context) : View.OnTouchListener {
velocityX: Float,
velocityY: Float
): Boolean {
var result = false
try {
val diffY = e2.y - e1!!.y
val diffX = e2.x - e1.x
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight()
} else {
onSwipeLeft()
}
result = true
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom()
} else {
onSwipeTop()
}
result = true
val diffXAbs = abs(diffX)
val diffYAbs = abs(diffY)
if (diffXAbs > diffYAbs && diffXAbs > SWIPE_THRESHOLD && abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) onSwipeRight()
else onSwipeLeft()
return true
}
if (diffYAbs > SWIPE_THRESHOLD && abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) onSwipeBottom()
else onSwipeTop()
return true
}
} catch (exception: Exception) {
exception.printStackTrace()
}

return result
return false
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class LuckFragment : Fragment() {

@SuppressLint("ClickableViewAccessibility")
private fun initListeners() {
// binding.ivRoulette.setOnClickListener { spinRoulette() }

binding.ivRoulette.setOnTouchListener(object : OnSwipeTouchListener(requireContext()){

Expand All @@ -74,15 +73,14 @@ class LuckFragment : Fragment() {
}

override fun onSwipeLeft() {
spinRoulette()
spinRoulette(true)
}
})
}

private fun spinRoulette() {
private fun spinRoulette(isLeft: Boolean = false) {
val random = Random()
val degrees = random.nextInt(1440) + 360

val degrees = (random.nextInt(1440) + 360) * (if (isLeft) -1 else 1)
val animator =
ObjectAnimator.ofFloat(binding.ivRoulette, View.ROTATION, 0f, degrees.toFloat())
animator.duration = 2000
Expand Down