From 1b4271277687a3990adb732ff54afbd6dbc773cc Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 15 Aug 2022 21:49:34 +0200 Subject: [PATCH] adding some swiping improvements --- .../launcher/activities/MainActivity.kt | 27 ++++++++++++--- .../launcher/fragments/AllAppsFragment.kt | 33 +++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 0e08984..9e55d64 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -1,9 +1,11 @@ package com.simplemobiletools.launcher.activities +import android.animation.ObjectAnimator import android.content.res.Configuration import android.graphics.Color import android.os.Bundle import android.view.MotionEvent +import android.view.animation.DecelerateInterpolator import android.widget.FrameLayout import com.simplemobiletools.commons.extensions.appLaunched import com.simplemobiletools.commons.extensions.realScreenSize @@ -14,9 +16,9 @@ import com.simplemobiletools.launcher.fragments.AllAppsFragment import kotlinx.android.synthetic.main.activity_main.* class MainActivity : SimpleActivity() { - private var mTouchDownY = 0 + var mTouchDownY = -1 + var mCurrentFragmentY = 0 private var mScreenHeight = 0 - private var mCurrentFragmentY = 0 override fun onCreate(savedInstanceState: Bundle?) { useDynamicTheme = false @@ -52,12 +54,27 @@ class MainActivity : SimpleActivity() { } MotionEvent.ACTION_MOVE -> { - val diffY = mTouchDownY - event.y - val newY = mCurrentFragmentY - diffY - all_apps_fragment.y = Math.min(Math.max(0f, newY), mScreenHeight.toFloat()) + if (mTouchDownY != -1) { + val diffY = mTouchDownY - event.y + val newY = mCurrentFragmentY - diffY + all_apps_fragment.y = Math.min(Math.max(0f, newY), mScreenHeight.toFloat()) + } + } + + MotionEvent.ACTION_CANCEL, + MotionEvent.ACTION_UP -> { + mTouchDownY = -1 + showAllAppsFragment() } } return true } + + private fun showAllAppsFragment() { + ObjectAnimator.ofFloat(all_apps_fragment, "y", 0f).apply { + interpolator = DecelerateInterpolator() + start() + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index 184cdeb..e7e31db 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -8,6 +8,7 @@ import android.content.pm.PackageManager import android.graphics.drawable.Drawable import android.os.Process import android.util.AttributeSet +import android.view.MotionEvent import android.view.Surface import android.view.WindowManager import android.widget.RelativeLayout @@ -16,19 +17,29 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.launcher.R -import com.simplemobiletools.launcher.activities.SimpleActivity +import com.simplemobiletools.launcher.activities.MainActivity import com.simplemobiletools.launcher.adapters.LaunchersAdapter import com.simplemobiletools.launcher.extensions.getColumnCount import com.simplemobiletools.launcher.models.AppLauncher import kotlinx.android.synthetic.main.all_apps_fragment.view.* class AllAppsFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { - private var activity: SimpleActivity? = null + private var touchDownY = -1 + private var activity: MainActivity? = null - fun setupFragment(activity: SimpleActivity) { + @SuppressLint("ClickableViewAccessibility") + fun setupFragment(activity: MainActivity) { this.activity = activity getLaunchers() setBackgroundColor(activity.getProperBackgroundColor()) + + all_apps_grid.setOnTouchListener { v, event -> + if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) { + touchDownY = -1 + } + + return@setOnTouchListener false + } } fun onConfigurationChanged() { @@ -42,6 +53,22 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : RelativeLa setupAdapter(launchers) } + override fun onInterceptTouchEvent(event: MotionEvent): Boolean { + var shouldIntercept = false + if (touchDownY != -1) { + shouldIntercept = touchDownY - event.y < 0 && all_apps_grid.computeVerticalScrollOffset() == 0 + if (shouldIntercept) { + activity?.mTouchDownY = touchDownY + activity?.mCurrentFragmentY = y.toInt() + touchDownY = -1 + } + } else { + touchDownY = event.y.toInt() + } + + return shouldIntercept + } + @SuppressLint("WrongConstant") private fun getLaunchers() { val allApps = ArrayList()