8000 Swipe to refresh is too sensitive (#1108) · wertgit/Android@f5e4d01 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5e4d01

Browse files
Swipe to refresh is too sensitive (duckduckgo#1108)
1 parent 1533cff commit f5e4d01

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,9 @@ class BrowserTabFragment :
10121012
}
10131013

10141014
private fun configureSwipeRefresh() {
1015+
val metrics = resources.displayMetrics
1016+
val distanceToTrigger = (DEFAULT_CIRCLE_TARGET_TIMES_1_5 * metrics.density).toInt()
1017+
swipeRefreshContainer.setDistanceToTriggerSync(distanceToTrigger)
10151018
swipeRefreshContainer.setColorSchemeColors(ContextCompat.getColor(requireContext(), R.color.cornflowerBlue))
10161019

10171020
swipeRefreshContainer.setOnRefreshListener {
@@ -1417,6 +1420,8 @@ class BrowserTabFragment :
14171420
private const val TRACKERS_INI_DELAY = 500L
14181421
private const val TRACKERS_SECONDARY_DELAY = 200L
14191422

1423+
private const val DEFAULT_CIRCLE_TARGET_TIMES_1_5 = 96
1424+
14201425
fun newInstance(tabId: String, query: String? = null, skipHome: Boolean): BrowserTabFragment {
14211426
val fragment = BrowserTabFragment()
14221427
val args = Bundle()

app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt

Lines changed: 22 additions & 7 deletions
89
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ import androidx.core.view.ViewCompat
3535
* Originally based on https://github.com/takahirom/webview-in-coordinatorlayout for scrolling behaviour
3636
*/
3737
class DuckDuckGoWebView : WebView, NestedScrollingChild {
38-
private var lastClampedTopY: Boolean = false
38+
private var lastClampedTopY: Boolean = true // when created we are always at the top
3939
private var contentAllowsSwipeToRefresh: Boolean = true
4040
private var enableSwipeRefreshCallback: ((Boolean) -> Unit)? = null
41+
private var hasGestureFinished = true
42+
private var canSwipeToRefresh = true
4143

4244
private var lastY: Int = 0
4345
private var lastDeltaY: Int = 0
@@ -76,12 +78,15 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
7678
event.offsetLocation(0f, nestedOffsetY.toFloat())
7779

7880
when (action) {
81+
MotionEvent.ACTION_UP -> {
82+
hasGestureFinished = true
83+
returnValue = super.onTouchEvent(event)
84+
stopNestedScroll()
85+
}
7986
MotionEvent.ACTION_MOVE -> {
8087
var deltaY = lastY - eventY
8188

82-
if (deltaY > 0) {
83-
lastClampedTopY = false
84-
}
+
lastClampedTopY = deltaY <= 0
8590

8691
if (dispatchNestedPreScroll(0, deltaY, scrollConsumed, scrollOffset)) {
8792
deltaY -= scrollConsumed[1]
@@ -98,15 +103,16 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
98103
lastY -= scrollOffset[1]
99104
}
100105

101-
if (scrollY == 0 && lastClampedTopY && nestedOffsetY == 0) {
102-
// we have reached the top, are clamped vertically and nestedScrollY is done too -> enable swipeRefresh (by default always disabled)
106+
if (canSwipeToRefresh && scrollY == 0 && lastClampedTopY && nestedOffsetY == 0) {
107+
// we are on a new gesture, have reached the top, are clamped vertically and nestedScrollY is done too -> enable swipeRefresh (by default always disabled)
103108
enableSwipeRefresh(true)
104109
}
105110

106111
lastDeltaY = deltaY
107112
}
108113

109114
MotionEvent.ACTION_DOWN -> {
115+
hasGestureFinished = false
110116
// disable swipeRefresh until we can be sure it should be enabled
111117
enableSwipeRefresh(false)
112118

@@ -153,7 +159,16 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
153159
override fun onOverScrolled(scrollX: Int, scrollY: Int, clampedX: Boolean, clampedY: Boolean) {
154160
// taking into account lastDeltaY since we are only interested whether we clamped at the top
155161
lastClampedTopY = clampedY && lastDeltaY <= 0
156-
enableSwipeRefresh(clampedY && scrollY == 0 && (lastDeltaY <= 0 || nestedOffsetY == 0))
162+
163+
if (!lastClampedTopY) {
164+
canSwipeToRefresh = false // disable because user scrolled down so we need a new gesture
165+
}
166+
167+
if (lastClampedTopY && hasGestureFinished) {
168+
canSwipeToRefresh = true // only enable if at the top and gestured finished
169+
}
170+
171+
enableSwipeRefresh(canSwipeToRefresh && clampedY && scrollY == 0 && (lastDeltaY <= 0 || nestedOffsetY == 0))
157172
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY)
158173
}
159174

0 commit comments

Comments
 (0)
0