Merge branch 'swipe_sensitivity' into 'master'

Make it a bit harder to swipe between tabs

Closes #285

See merge request pixeldroid/PixelDroid!341
This commit is contained in:
Matthieu 2021-05-14 08:12:44 +00:00
commit c3ba4f41fd
2 changed files with 26 additions and 3 deletions

View File

@ -11,10 +11,13 @@ import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.core.view.NestedScrollingChild
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.paging.ExperimentalPagingApi
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.google.android.material.tabs.TabLayoutMediator
import org.pixeldroid.app.databinding.ActivityMainBinding
@ -40,6 +43,7 @@ import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
import com.mikepenz.materialdrawer.util.DrawerImageLoader
import com.mikepenz.materialdrawer.widget.AccountHeaderView
import org.ligi.tracedroid.sending.sendTraceDroidStackTracesIfExist
import org.pixeldroid.app.posts.NestedScrollableHost
import retrofit2.HttpException
import java.io.IOException
@ -268,8 +272,22 @@ class MainActivity : BaseActivity() {
header.setActiveProfile(account.toLong())
}
/**
* Use reflection to make it a bit harder to swipe between tabs
*/
private fun ViewPager2.reduceDragSensitivity() {
val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
recyclerViewField.isAccessible = true
val recyclerView = recyclerViewField.get(this) as RecyclerView
val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
touchSlopField.isAccessible = true
val touchSlop = touchSlopField.get(recyclerView) as Int
touchSlopField.set(recyclerView, touchSlop*NestedScrollableHost.touchSlopModifier)
}
private fun setupTabs(tab_array: List<() -> Fragment>){
binding.viewPager.reduceDragSensitivity()
binding.viewPager.adapter = object : FragmentStateAdapter(this) {
override fun createFragment(position: Int): Fragment {
return tab_array[position]()

View File

@ -94,11 +94,12 @@ class NestedScrollableHost : ConstraintLayout {
val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
// assuming ViewPager2 touch-slop is 2x touch-slop of child
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f/ touchSlopModifier else 1f
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f/touchSlopModifier
if(dx.absoluteValue * .5f > touchSlop || scaledDy > touchSlop) doubleTapCallback?.invoke(false)
if (scaledDx > touchSlop || scaledDy > touchSlop) {
doubleTapCallback?.invoke(false)
if (isVpHorizontal == (scaledDy > scaledDx)) {
// Gesture is perpendicular, allow all parents to intercept
@ -116,4 +117,8 @@ class NestedScrollableHost : ConstraintLayout {
}
}
}
companion object {
const val touchSlopModifier = 2
}
}