add fling handling at showing/hiding all app icons

This commit is contained in:
tibbi 2022-08-15 22:08:40 +02:00
parent 1b42712776
commit 5ef9c643c9
3 changed files with 61 additions and 6 deletions

View File

@ -4,21 +4,26 @@ import android.animation.ObjectAnimator
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.animation.DecelerateInterpolator
import android.widget.FrameLayout
import androidx.core.view.GestureDetectorCompat
import com.simplemobiletools.commons.extensions.appLaunched
import com.simplemobiletools.commons.extensions.realScreenSize
import com.simplemobiletools.commons.extensions.statusBarHeight
import com.simplemobiletools.launcher.BuildConfig
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.fragments.AllAppsFragment
import com.simplemobiletools.launcher.interfaces.FlingListener
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : SimpleActivity() {
var mTouchDownY = -1
var mCurrentFragmentY = 0
class MainActivity : SimpleActivity(), FlingListener {
private var mTouchDownY = -1
private var mCurrentFragmentY = 0
private var mScreenHeight = 0
private var mIgnoreUpEvent = false
private lateinit var mDetector: GestureDetectorCompat
override fun onCreate(savedInstanceState: Bundle?) {
useDynamicTheme = false
@ -27,6 +32,8 @@ class MainActivity : SimpleActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID)
mDetector = GestureDetectorCompat(this, MyGestureListener(this))
window.setDecorFitsSystemWindows(false)
(all_apps_fragment as AllAppsFragment).setupFragment(this)
mScreenHeight = realScreenSize.y
@ -47,10 +54,12 @@ class MainActivity : SimpleActivity() {
}
override fun onTouchEvent(event: MotionEvent): Boolean {
mDetector.onTouchEvent(event)
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
mTouchDownY = event.y.toInt()
mCurrentFragmentY = all_apps_fragment.y.toInt()
mIgnoreUpEvent = false
}
MotionEvent.ACTION_MOVE -> {
@ -64,17 +73,57 @@ class MainActivity : SimpleActivity() {
MotionEvent.ACTION_CANCEL,
MotionEvent.ACTION_UP -> {
mTouchDownY = -1
showAllAppsFragment()
if (!mIgnoreUpEvent) {
if (all_apps_fragment.y < mScreenHeight * 0.7) {
showAllAppsFragment()
} else {
hideAllAppsFragment()
}
}
}
}
return true
}
fun startHandlingTouches(touchDownY: Int) {
mTouchDownY = touchDownY
mCurrentFragmentY = all_apps_fragment.y.toInt()
mIgnoreUpEvent = false
}
private fun showAllAppsFragment() {
ObjectAnimator.ofFloat(all_apps_fragment, "y", 0f).apply {
interpolator = DecelerateInterpolator()
start()
}
}
private fun hideAllAppsFragment() {
ObjectAnimator.ofFloat(all_apps_fragment, "y", mScreenHeight.toFloat()).apply {
interpolator = DecelerateInterpolator()
start()
}
}
private class MyGestureListener(private val flingListener: FlingListener) : GestureDetector.SimpleOnGestureListener() {
override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
if (velocityY > 0) {
flingListener.onFlingDown()
} else {
flingListener.onFlingUp()
}
return true
}
}
override fun onFlingUp() {
mIgnoreUpEvent = true
showAllAppsFragment()
}
override fun onFlingDown() {
mIgnoreUpEvent = true
hideAllAppsFragment()
}
}

View File

@ -58,8 +58,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : RelativeLa
if (touchDownY != -1) {
shouldIntercept = touchDownY - event.y < 0 && all_apps_grid.computeVerticalScrollOffset() == 0
if (shouldIntercept) {
activity?.mTouchDownY = touchDownY
activity?.mCurrentFragmentY = y.toInt()
activity?.startHandlingTouches(touchDownY)
touchDownY = -1
}
} else {

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.launcher.interfaces
interface FlingListener {
fun onFlingUp()
fun onFlingDown()
}