Skip to content

Commit

Permalink
Swipe-to-refresh fixes for draggable content
Browse files Browse the repository at this point in the history
- Added callback to allow WebView control over SwipeRefreshLayout
- Added logic to disable swipe refresh when WebView is not clampedY before
  • Loading branch information
Usiel Riedl committed Jun 20, 2020
1 parent 110a525 commit 4ec4ffd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Expand Up @@ -234,7 +234,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
private val menuButton: ViewGroup?
get() = appBarLayout.browserMenu

private var webView: WebView? = null
private var webView: DuckDuckGoWebView? = null

private val errorSnackbar: Snackbar by lazy {
Snackbar.make(browserLayout, R.string.crashedWebViewErrorMessage, Snackbar.LENGTH_INDEFINITE)
Expand Down Expand Up @@ -800,7 +800,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
R.layout.include_duckduckgo_browser_webview,
webViewContainer,
true
).findViewById(R.id.browserWebView) as WebView
).findViewById(R.id.browserWebView) as DuckDuckGoWebView

webView?.let {
userAgentProvider = UserAgentProvider(it.settings.userAgentString, deviceInfo)
Expand Down Expand Up @@ -833,6 +833,10 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
false
}

it.setEnableSwipeRefreshCallback { enable ->
swipeRefreshContainer?.isEnabled = enable
}

registerForContextMenu(it)

it.setFindListener(this)
Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/com/duckduckgo/app/browser/DuckDuckGoWebView.kt
Expand Up @@ -35,6 +35,9 @@ import androidx.core.view.ViewCompat
* Originally based on https://github.com/takahirom/webview-in-coordinatorlayout for scrolling behaviour
*/
class DuckDuckGoWebView : WebView, NestedScrollingChild {
private var lastClampedY: Boolean = false
private var enableSwipeRefreshCallback: ((Boolean) -> Unit)? = null

private var lastY: Int = 0
private val scrollOffset = IntArray(2)
private val scrollConsumed = IntArray(2)
Expand Down Expand Up @@ -74,6 +77,10 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
MotionEvent.ACTION_MOVE -> {
var deltaY = lastY - eventY

if (deltaY > 0) {
lastClampedY = false
}

if (dispatchNestedPreScroll(0, deltaY, scrollConsumed, scrollOffset)) {
deltaY -= scrollConsumed[1]
lastY = eventY - scrollOffset[1]
Expand All @@ -83,8 +90,9 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {

returnValue = super.onTouchEvent(event)

if (scrollY == 0) {
// Give parents control (required for SwipeRefreshLayout to work)
if (scrollY == 0 && lastClampedY) {
// we have reached the top and are clamped -> enable swipeRefresh (by default always disabled)
enableSwipeRefresh(true)
stopNestedScroll()
} else if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) {
event.offsetLocation(0f, scrollOffset[1].toFloat())
Expand All @@ -94,6 +102,9 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
}

MotionEvent.ACTION_DOWN -> {
// disable swipeRefresh until we can be sure it should be enabled
enableSwipeRefresh(false)

returnValue = super.onTouchEvent(event)
lastY = eventY
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
Expand Down Expand Up @@ -134,6 +145,20 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {
override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean =
nestedScrollHelper.dispatchNestedPreFling(velocityX, velocityY)

override fun onOverScrolled(scrollX: Int, scrollY: Int, clampedX: Boolean, clampedY: Boolean) {
lastClampedY = clampedY
enableSwipeRefresh(clampedY && scrollY == 0)
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY)
}

private fun enableSwipeRefresh(enable: Boolean) {
enableSwipeRefreshCallback?.invoke(enable)
}

fun setEnableSwipeRefreshCallback(callback: (Boolean) -> Unit) {
enableSwipeRefreshCallback = callback
}

companion object {

/*
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/fragment_browser_tab.xml
Expand Up @@ -74,7 +74,8 @@
<com.duckduckgo.app.browser.ui.ScrollAwareRefreshLayout
android:id="@+id/swipeRefreshContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:enabled="false">

<FrameLayout
android:id="@+id/webViewContainer"
Expand Down

0 comments on commit 4ec4ffd

Please sign in to comment.